diff --git a/src/plugins/skland/auth.go b/src/plugins/skland/auth.go index d98e45c..db19fd2 100644 --- a/src/plugins/skland/auth.go +++ b/src/plugins/skland/auth.go @@ -47,6 +47,10 @@ type GenTokenByUidData struct { Token string `json:"token"` } +type User struct { + HgId string `json:"hgId"` +} + // Login 使用token登录 func Login(token string) (Account, error) { account := Account{} @@ -66,7 +70,8 @@ func Login(token string) (Account, error) { if err != nil { return account, fmt.Errorf("auth login by code error: %w", err) } - account.UserId = res1.UserId + u, _ := CheckToken(token) + account.UserId = u.HgId account.Skland.Cred = res1.Cred account.Skland.Token = res1.Token return account, nil @@ -80,60 +85,43 @@ func grantApp(token string, code string) (*GrantAppData, error) { // 获取Cred func authLoginByCode(code string) (*GenCredByCodeData, error) { - req := SKR().SetBody(gh.M{"kind": 1, "code": code}) - return SklandRequest[*GenCredByCodeData](req, "POST", "/api/v1/user/auth/generate_cred_by_code") + req := SKR().SetHeader("did", did).SetBody(gh.M{"kind": 1, "code": code}) + return SklandRequest[*GenCredByCodeData](req, "POST", "/web/v1/user/auth/generate_cred_by_code") } // RefreshToken 刷新 token func RefreshToken(account Account) (Account, error) { - b, err := CheckUser(account.Skland.Cred) - if err == nil { - res, err := authRefresh(account.Skland.Cred) + res, err := authRefresh(account.Skland.Cred) + if err != nil { + return account, fmt.Errorf("auth refresh error: %w", err) + } + account.Skland.Token = res.Token + // 检查cred是否有效 + _, err = listPlayer(account.Skland) + if err != nil { + log.Println("cred失效,尝试重新登录。") + _, err = CheckToken(account.Hypergryph.Token) if err != nil { - return account, fmt.Errorf("auth refresh error: %w", err) + return account, err } - account.Skland.Token = res.Token - } else { - // 是cred失效尝试重新登录 - if b { - log.Println("cred失效,尝试重新登录。") - err = CheckToken(account.Hypergryph.Token) - if err != nil { - return account, err - } - account, err = Login(account.Hypergryph.Token) - if err != nil { - return account, err - } - // 更新token - bot.DBEngine.Exec("update user_account set hypergryph_token = ?, skland_token = ?, skland_cred = ? where skland_id = ?", account.Hypergryph.Token, account.Skland.Token, account.Skland.Cred, account.UserId) - } else { + account, err = Login(account.Hypergryph.Token) + if err != nil { return account, err } + // 更新token + bot.DBEngine.Exec("update user_account set hypergryph_token = ?, skland_token = ?, skland_cred = ? where skland_id = ?", account.Hypergryph.Token, account.Skland.Token, account.Skland.Cred, account.UserId) } return account, nil } -// CheckUser 检查用户cred有效性 -func CheckUser(cred string) (bool, error) { - res, err := SKR().SetHeader("cred", cred).Get(sklandAddr + "/api/v1/user/check") - if err != nil { - return false, err - } - if res.StatusCode() == 401 { - return true, fmt.Errorf("无效的cred,请重新登录获取。") - } - return false, nil -} - // CheckToken 检查token有效性 -func CheckToken(token string) error { - req := SKR().SetQueryParam("token", token) - _, err := HypergryphRequest[any](req, "GET", "/user/info/v1/basic") +func CheckToken(token string) (*User, error) { + req := HR().SetQueryParam("token", token) + user, err := HypergryphRequest[*User](req, "GET", "/user/info/v1/basic") if err != nil { - return fmt.Errorf("token已失效请重新登录!") + return nil, fmt.Errorf("token已失效请重新登录!") } - return err + return user, err } // CheckBToken 检查BToken有效性 diff --git a/src/plugins/skland/player_gacha.go b/src/plugins/skland/player_gacha.go index 633b618..4ec5c4a 100644 --- a/src/plugins/skland/player_gacha.go +++ b/src/plugins/skland/player_gacha.go @@ -21,7 +21,7 @@ type Char struct { func GetPlayerGacha(token, channelId string) ([]Char, error) { var chars []Char if channelId == "1" { - err := CheckToken(token) + _, err := CheckToken(token) if err != nil { log.Println(err) return chars, err diff --git a/src/plugins/skland/player_redeem.go b/src/plugins/skland/player_redeem.go index e149982..f543a60 100644 --- a/src/plugins/skland/player_redeem.go +++ b/src/plugins/skland/player_redeem.go @@ -11,7 +11,7 @@ import ( // GetPlayerRedeem CDK兑换 func GetPlayerRedeem(token, cdk, channelId string) (string, error) { if channelId == "1" { - err := CheckToken(token) + _, err := CheckToken(token) if err != nil { log.Println(err) return err.Error(), err diff --git a/src/plugins/skland/skland_request.go b/src/plugins/skland/skland_request.go index 37a531f..08da676 100644 --- a/src/plugins/skland/skland_request.go +++ b/src/plugins/skland/skland_request.go @@ -9,6 +9,8 @@ import ( var sklandAddr = "https://zonai.skland.com" +var did = "BdkL3Yma2k0SGhC9gqjAFI1JBHhirLCFy999II1w7jPuI+X4JWl4m7nVeHGA9E5GD8fU0UlpmZQJIZckyN6WBNA==" + type SKBaseResp[T any] struct { Code *int `json:"code"` Message string `json:"message"` @@ -44,6 +46,10 @@ func SklandRequest[T any](r *resty.Request, method, path string, vs ...any) (t T log.Println(string(resp.Body())) return t, fmt.Errorf("服务器被墙了!") } + if resp.StatusCode() == 401 { + log.Println(string(resp.Body())) + return t, fmt.Errorf("cred无效!") + } res, err := resty.ParseResp[*SKBaseResp[any], *SKBaseResp[T]]( resp, respErr, )