httplib_business_admin_service_gateway.go 1.7 KB
package svr

import (
	"fmt"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
	"strconv"
	"strings"
	"time"
)

type HttplibBusinessAdminApiServiceGateway struct {
	httplibBaseServiceGateway
}

// 服务登录
func (serviceGateway *HttplibBusinessAdminApiServiceGateway) UserAuth(userId int64, platformId string) (int, error) {
	url := strings.Join([]string{serviceGateway.baseURL, "auth", "get-user-auth"}, "/")
	request := serviceGateway.createRequest(url, "post")
	request.Header("appKey", constant.UCENTER_APP_KEY)
	options := make(map[string]interface{})
	options["userId"] = fmt.Sprintf("%v", userId)
	options["platformId"] = fmt.Sprintf("%v", platformId)
	request.JSONBody(options)
	response := make(map[string]interface{})
	err := request.ToJSON(&response)
	if err != nil {
		log.Error("Service Gateway Fail:", err)
		return 0, err
	}
	return serviceGateway.handlerError(response)
}

func (serviceGateway *HttplibBusinessAdminApiServiceGateway) handlerError(in map[string]interface{}) (int, error) {
	var rspCode int
	var err error
	if code, ok := in["code"]; ok {
		rspCode, _ = strconv.Atoi(fmt.Sprintf("%v", code))
	} else {
		err = fmt.Errorf("网关解析错误")
	}
	if msg, ok := in["msg"]; ok {
		msg := msg.(string)
		if rspCode != 0 && len(msg) > 0 {
			err = fmt.Errorf(msg)
		}
	}
	return rspCode, err
}

func NewHttplibBusinessAdminApiServiceGateway() *HttplibBusinessAdminApiServiceGateway {
	return &HttplibBusinessAdminApiServiceGateway{
		httplibBaseServiceGateway: httplibBaseServiceGateway{
			baseURL:          constant.BUSINESS_ADMIN_SERVICE_HOST,
			connectTimeout:   100 * time.Second,
			readWriteTimeout: 30 * time.Second,
		},
	}
}