pcm-ac/internal/logic/getactokenlogic.go

97 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package logic
import (
"context"
"encoding/json"
"github.com/go-resty/resty/v2"
"io"
"net/http"
"time"
"gitlink.org.cn/jcce-pcm/pcm-ac/hpcAC"
"gitlink.org.cn/jcce-pcm/pcm-ac/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type GetACTokenLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetACTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetACTokenLogic {
return &GetACTokenLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// GetACToken 曙光ac获取token
func (l *GetACTokenLogic) GetACToken(in *hpcAC.ACTokenReq) (*hpcAC.TokenResp, error) {
var respAC hpcAC.ACTokenResp
var resp hpcAC.TokenResp
token := l.svcCtx.Config.ShuguangConf.Token
httpClient := http.Client{Timeout: time.Duration(3) * time.Second}
req, _ := http.NewRequest("GET", l.svcCtx.Config.ShuguangConf.StateUrl, nil)
req.Header.Add("token", token)
respState, _ := httpClient.Do(req)
var res hpcAC.ACTokenState
body, _ := io.ReadAll(respState.Body)
err := json.Unmarshal(body, &res)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(respState.Body)
//获取状态返回0则token可用返回为10008或者token为空则重新读取
if res.Code == "0" {
var acDataToken hpcAC.ACTokenData
acDataToken.Token = token
resp.Data = &acDataToken
resp.Msg = res.Msg
resp.Code = "200"
} else if token == "" || res.Code == "10008" {
client := resty.New()
queryParams := map[string]string{
"user": l.svcCtx.Config.ShuguangConf.User,
"password": l.svcCtx.Config.ShuguangConf.Password,
"orgId": l.svcCtx.Config.ShuguangConf.OrgId,
}
_, err := client.R().
SetHeaders(queryParams).
SetResult(&respAC).
Post(l.svcCtx.Config.ShuguangConf.TokenUrl)
if err != nil {
logx.WithContext(l.ctx).Errorf("ACAuth err: %s", err.Error())
return nil, err
}
//这里固定使用clusterid 11112
for _, dt := range respAC.Data {
if dt.ClusterId == "11276" {
token = dt.Token
}
}
l.svcCtx.Config.ShuguangConf.Token = token
resp.Code = respAC.Code
resp.Msg = respAC.Msg
var acDataToken hpcAC.ACTokenData
acDataToken.Token = token
resp.Data = &acDataToken
}
return &resp, nil
}