httplib_ucenter_service_gateway.go 2.1 KB
package serviceGateway

import (
	"crypto/sha1"
	"fmt"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/serviceGateway/reply"
)

type HttpLibUCenterApiServiceGateway struct {
	httpLibBaseServiceGateway
}

func (serviceGateway *HttpLibUCenterApiServiceGateway) buildHeaders() map[string]string {
	nowTime := fmt.Sprint(time.Now().Unix())
	str := fmt.Sprintf("%s%s%s", nowTime, constant.UCENTER_SECRET, constant.UCENTER_CHECK_ALT)
	bt := sha1.Sum([]byte(str))
	checksum := fmt.Sprintf("%x", bt)
	return map[string]string{
		"appKey":   constant.UCENTER_APP_KEY,
		"nonce":    "",
		"curTime":  nowTime,
		"checkSum": checksum,
	}
}

// AuthCode PC端登录, 为后台单点登录
func (serviceGateway *HttpLibUCenterApiServiceGateway) AuthCode(code string) (*reply.UCenterAuthCode, error) {
	authCodeReply := &reply.UCenterAuthCode{}
	serviceGateway.CreateRequest(http.MethodPost, "/auth/serverLogin")
	serviceGateway.SetBody(map[string]interface{}{
		"type":   3,
		"secret": url.QueryEscape(code),
	})
	serviceGateway.SetHeaders(serviceGateway.buildHeaders())
	err := serviceGateway.ToJson(authCodeReply)
	return authCodeReply, err
}

// AuthCode 手机应用端登录, 为token登录,app登录
func (serviceGateway *HttpLibUCenterApiServiceGateway) AppAuthCode(tokenCode string, uid int, companyId int) (*reply.UCenterAuthCode, error) {
	authCodeReply := &reply.UCenterAuthCode{}
	serviceGateway.CreateRequest(http.MethodPost, "/auth/serverLogin")
	serviceGateway.SetBody(map[string]interface{}{
		"type":      2,
		"token":     strings.TrimSpace(tokenCode),
		"uid":       strconv.Itoa(uid),
		"companyId": strconv.Itoa(companyId),
	})
	serviceGateway.SetHeaders(serviceGateway.buildHeaders())
	err := serviceGateway.ToJson(authCodeReply)
	return authCodeReply, err
}

func NewHttpLibUCenterApiServiceGateway() *HttpLibUCenterApiServiceGateway {
	return &HttpLibUCenterApiServiceGateway{
		httpLibBaseServiceGateway{baseURL: constant.UCENTER_SERVICE_HOST},
	}
}