admin_backend/sdk/login/model/tuyou/tuyou.go
2026-04-15 14:48:13 +08:00

123 lines
3.0 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 tuyou
import (
"backend/Type"
"backend/middleware/feishu"
"backend/sdk/login/model/base"
"backend/util"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type TuyouModel struct {
}
/**
线上服
https://128-hwsfsdk-sdk-online01.qijihdhk.com
提审服
https://128-hwsfsdk-sdk-ts01.qijihdhk.com
测试服
https://128-hwsfsdk-sdk-test01.qijihdhk.com
仿真服
https://128-hwsfsdk-sdk-sim01.qijihdhk.com
*/
var UrlList = map[int]string{
0: "https://128-hwsfsdk-sdk-online01.qijihdhk.com",
1: "https://128-hwsfsdk-sdk-test01.qijihdhk.com",
2: "https://128-hwsfsdk-sdk-sim01.qijihdhk.com",
}
func Login(c *gin.Context) {
var p base.Param
// read parameters from query string for GET requests
p.Uid = c.Query("uid")
p.Token = c.Query("token")
p.AppId = util.Int(c.Query("appId"))
p.AreaCode = util.Int(c.Query("areaCode"))
p.Version = c.Query("version")
if p.Uid == "" || p.Token == "" {
c.JSON(400, gin.H{"error": "missing uid or token"})
return
}
if err := (&TuyouModel{}).VerifyToken(p.AppId, p.Uid, p.Token); err != nil {
feishu.SendNotifyMsg(&Type.NotifyData{
NotifyMsg: "登录验证失败",
Host: "途游",
EventName: "invalid token " + err.Error(),
Severity: "High",
AlarmTime: time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05"),
})
c.JSON(401, gin.H{"error": "invalid token"})
return
}
util.LoginResponse(c, p.AppId, p.AreaCode, p.Version)
}
func (t *TuyouModel) VerifyToken(AppId int, Uid string, Token string) error {
now := time.Now().UnixMilli()
client := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12},
},
}
Url := UrlList[AppId]
if Url == "" {
return fmt.Errorf("unsupported AppId: %d", AppId)
}
req, err := http.NewRequest("GET", fmt.Sprint(Url, "/open/v4/user/verifyAuthorCode"), nil)
if err != nil {
return err
}
q := req.URL.Query()
q.Add("userId", Uid)
q.Add("authorCode", Token)
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("verify failed: status=%d body=%s", resp.StatusCode, string(body))
}
var respObj struct {
Track string `json:"track"`
Result struct {
Verify string `json:"verify"`
UserId int64 `json:"userId"`
CityCode int `json:"city_code"`
CityName string `json:"city_name"`
DevId string `json:"devId"`
IP string `json:"ip"`
} `json:"result"`
}
if err := json.Unmarshal(body, &respObj); err != nil {
return fmt.Errorf("invalid verify response: %w; body=%s", err, string(body))
}
if respObj.Result.Verify != "ok" {
return fmt.Errorf("verify failed: verify=%s body=%s", respObj.Result.Verify, string(body))
}
log.Printf("Tuyou verify success: cost time %dms\n", time.Now().UnixMilli()-now)
return nil
}