parent
a3ec04a3ce
commit
31b131f1db
|
|
@ -23,6 +23,13 @@ coze:
|
|||
base_url: "https://api.coze.cn"
|
||||
api_secret: "sat_AqvFcdNgesP8megy1ItTscWFXRcsHRzmM4NJ1KNavfcdT0EPwYuCPkDqGhItpx13"
|
||||
|
||||
lsxd:
|
||||
# 统一登录
|
||||
login_url: "http://api.test.user.1688sup.com/v1/login/phone"
|
||||
phone: "0zmINhJBwsDJYTmeaxXK4A=="
|
||||
password: "LSSGoWhc63NpZc1rq3LT8g=="
|
||||
check_token_url: "http://api.test.user.1688sup.com/v1/user/welcome"
|
||||
|
||||
|
||||
sys:
|
||||
session_len: 6
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ type Config struct {
|
|||
Ollama OllamaConfig `mapstructure:"ollama"`
|
||||
Vllm VllmConfig `mapstructure:"vllm"`
|
||||
Coze CozeConfig `mapstructure:"coze"`
|
||||
LSXD LSXDConfig `mapstructure:"lsxd"`
|
||||
Sys SysConfig `mapstructure:"sys"`
|
||||
Tools ToolsConfig `mapstructure:"tools"`
|
||||
EinoTools EinoToolsConfig `mapstructure:"eino_tools"`
|
||||
|
|
@ -116,6 +117,14 @@ type CozeConfig struct {
|
|||
ApiSecret string `mapstructure:"api_secret"`
|
||||
}
|
||||
|
||||
// LSXDConfig 统一登录配置
|
||||
type LSXDConfig struct {
|
||||
LoginURL string `mapstructure:"login_url"`
|
||||
Phone string `mapstructure:"phone"`
|
||||
Password string `mapstructure:"password"`
|
||||
CheckTokenURL string `mapstructure:"check_token_url"`
|
||||
}
|
||||
|
||||
type Redis struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Type string `mapstructure:"type"`
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
package constants
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
CACHE_KEY_LSXD_TOKEN = "ai_scheduler:lsxd_token"
|
||||
EXPIRE_LSXD_TOKEN = time.Hour * 2 // 2小时
|
||||
)
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
package lsxd
|
||||
|
||||
import (
|
||||
"ai_scheduler/internal/config"
|
||||
"ai_scheduler/internal/data/constants"
|
||||
"ai_scheduler/internal/pkg/l_request"
|
||||
"ai_scheduler/utils"
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type Login struct {
|
||||
config *config.Config
|
||||
redisCli *redis.Client
|
||||
}
|
||||
|
||||
func NewLogin(config *config.Config, rdb *utils.Rdb) *Login {
|
||||
return &Login{
|
||||
config: config,
|
||||
redisCli: rdb.Rdb,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Login) GetToken() string {
|
||||
ctx := context.Background()
|
||||
// 1.取缓存
|
||||
token, err := l.redisCli.Get(ctx, constants.CACHE_KEY_LSXD_TOKEN).Result()
|
||||
if err != nil {
|
||||
log.Errorf("lsxd get token from redis failed, err: %v", err)
|
||||
}
|
||||
|
||||
// 2.缓存存在
|
||||
if token != "" {
|
||||
// 3.缓存有效直接输出
|
||||
if l.checkTokenValid(token) {
|
||||
return token
|
||||
}
|
||||
}
|
||||
|
||||
// 4.缓存不存在或缓存无效,调用登录接口获取token
|
||||
token, err = l.login()
|
||||
if err != nil {
|
||||
log.Errorf("lsxd login failed, err: %v", err)
|
||||
return ""
|
||||
}
|
||||
// 5.缓存token
|
||||
l.redisCli.Set(ctx, constants.CACHE_KEY_LSXD_TOKEN, token, constants.EXPIRE_LSXD_TOKEN)
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
// 校验token是否有效
|
||||
func (l *Login) checkTokenValid(token string) bool {
|
||||
// 欢迎页校验token有效
|
||||
checkTokenURL := l.config.LSXD.CheckTokenURL
|
||||
// 调用欢迎页校验token有效
|
||||
r := l_request.Request{
|
||||
Url: checkTokenURL,
|
||||
Headers: map[string]string{
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": token,
|
||||
},
|
||||
Method: "GET",
|
||||
}
|
||||
res, err := r.Send()
|
||||
if err != nil {
|
||||
log.Errorf("lsxd check token valid failed, err: %v", err)
|
||||
return false
|
||||
}
|
||||
if res.StatusCode != 200 {
|
||||
log.Errorf("lsxd check token valid failed, status code: %d", res.StatusCode)
|
||||
return false
|
||||
}
|
||||
|
||||
log.Info("lsxd check token valid success")
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// 调用登录接口获取token
|
||||
func (l *Login) login() (string, error) {
|
||||
// 1.获取配置
|
||||
loginURL := l.config.LSXD.LoginURL
|
||||
phone := l.config.LSXD.Phone
|
||||
password := l.config.LSXD.Password
|
||||
|
||||
// 2.调用登录接口获取token
|
||||
r := l_request.Request{
|
||||
Url: loginURL,
|
||||
Headers: map[string]string{
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
Method: "POST",
|
||||
Json: map[string]any{
|
||||
"phone": phone,
|
||||
"password": password,
|
||||
"code": "123456",
|
||||
},
|
||||
}
|
||||
res, err := r.Send()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 3.解析token
|
||||
var resp struct {
|
||||
Token string `json:"accessToken"`
|
||||
}
|
||||
err = json.Unmarshal(res.Content, &resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
token := resp.Token
|
||||
|
||||
// 4.返回token
|
||||
return token, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package lsxd
|
||||
|
||||
import (
|
||||
"ai_scheduler/internal/config"
|
||||
"ai_scheduler/utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetToken(t *testing.T) {
|
||||
|
||||
rdb := utils.NewRdb(&config.Config{
|
||||
Redis: config.Redis{
|
||||
Host: "47.97.27.195:6379",
|
||||
Pass: "lansexiongdi@666",
|
||||
Key: "report-api-test",
|
||||
},
|
||||
})
|
||||
|
||||
login := NewLogin(&config.Config{
|
||||
LSXD: config.LSXDConfig{
|
||||
LoginURL: "http://api.test.user.1688sup.com/v1/login/phone",
|
||||
Phone: "0zmINhJBwsDJYTmeaxXK4A==",
|
||||
Password: "LSSGoWhc63NpZc1rq3LT8g==",
|
||||
CheckTokenURL: "http://api.test.user.1688sup.com/v1/user/welcome",
|
||||
},
|
||||
}, rdb)
|
||||
token := login.GetToken()
|
||||
if token == "" {
|
||||
t.Errorf("token is empty")
|
||||
}
|
||||
|
||||
t.Logf("token: %s", token)
|
||||
}
|
||||
Loading…
Reference in New Issue