Skip to content

Commit

Permalink
更换登录接口
Browse files Browse the repository at this point in the history
  • Loading branch information
IJNKAWAKAZE committed Sep 20, 2024
1 parent 8cde51e commit 1c76ec3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 42 deletions.
68 changes: 28 additions & 40 deletions src/plugins/skland/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
Expand All @@ -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有效性
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/skland/player_gacha.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/skland/player_redeem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/skland/skland_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
)
Expand Down

0 comments on commit 1c76ec3

Please sign in to comment.