api_auth_lib.go 1.6 KB
package authlib

import (
	"github.com/beego/beego/v2/core/logs"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api"
	"net/http"
	"time"
)

type ApiAuthLib struct {
	Token string
	api.BaseServiceGateway
}

func (gateway *ApiAuthLib) WithToken(token string) *ApiAuthLib {
	gateway.Token = token
	return gateway
}

func (gateway *ApiAuthLib) DefaultHeader() http.Header {
	var header = make(map[string][]string)
	header["x-mmm-accesstoken"] = []string{gateway.Token}
	return header
}

func NewApiAuthLib(host string) *ApiAuthLib {
	gt := api.NewBaseServiceGateway(host)
	gt.ConnectTimeout = 360 * time.Second
	gt.ReadWriteTimeout = 360 * time.Second
	gt.Interceptor = func(msg string) {
		//log.Logger.Info(msg)
		logs.Debug(msg)
	}
	gt.ServiceName = "【授权中心】"
	return &ApiAuthLib{
		BaseServiceGateway: gt,
	}
}

func (gateway *ApiAuthLib) MeInfo(param RequestUserMeQuery) (*DataUserMe, error) {
	url := gateway.Host() + "/v1/user/me"
	method := "get"
	var data DataUserMe
	err := gateway.FastDoRequest(url, method, param, &data, api.WithHeader(gateway.DefaultHeader()))
	if err != nil {
		return nil, err
	}
	return &data, nil
}

func (gateway *ApiAuthLib) LoginCheck(param RequestLoginCheck) (*DataLoginCheck, error) {
	url := gateway.Host() + "/v1/login/check?token=" + param.Token
	method := "get"
	var data DataLoginCheck
	err := gateway.FastDoRequest(url, method, param, &data, api.WithHeader(gateway.DefaultHeader()))
	if errCodeMsg, ok := err.(api.ErrCodeMsg); ok {
		return &DataLoginCheck{
			Code: errCodeMsg.Code,
			Msg:  errCodeMsg.Msg,
		}, nil
	}
	if err != nil {
		return nil, err
	}
	return &data, nil
}