update: 完成了账号添加和刷新容量

This commit is contained in:
ctwj
2025-09-04 01:02:07 +08:00
parent 074058ac5c
commit f2ee574fae
2 changed files with 39 additions and 29 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -213,21 +214,20 @@ func (x *XunleiPanService) getCaptchaToken() (string, error) {
return "", fmt.Errorf("获取 captcha_token 请求失败: %v", err) return "", fmt.Errorf("获取 captcha_token 请求失败: %v", err)
} }
if resp["code"] != 0 || resp["data"] == nil { if resp["captcha_token"] != nil && resp["captcha_token"] != "" {
//
} else {
return "", fmt.Errorf("获取 captcha_token 失败: %v", resp) return "", fmt.Errorf("获取 captcha_token 失败: %v", resp)
} }
data := resp["data"].(map[string]interface{})
captchaToken := data["captcha_token"].(string)
// 计算过期时间(当前时间 + expires_in - 10 秒缓冲) // 计算过期时间(当前时间 + expires_in - 10 秒缓冲)
expiresAt := currentTime + int64(data["expires_in"].(float64)) - 10 expiresAt := currentTime + int64(resp["expires_in"].(float64)) - 10
// 更新 extra 数据 // 更新 extra 数据
if x.extra.Captcha == nil { if x.extra.Captcha == nil {
x.extra.Captcha = &CaptchaData{} x.extra.Captcha = &CaptchaData{}
} }
x.extra.Captcha.CaptchaToken = captchaToken x.extra.Captcha.CaptchaToken = resp["captcha_token"].(string)
x.extra.Captcha.ExpiresAt = expiresAt x.extra.Captcha.ExpiresAt = expiresAt
// 保存到数据库 // 保存到数据库
@@ -240,7 +240,7 @@ func (x *XunleiPanService) getCaptchaToken() (string, error) {
return "", fmt.Errorf("保存 captcha_token 到数据库失败: %v", err) return "", fmt.Errorf("保存 captcha_token 到数据库失败: %v", err)
} }
return captchaToken, nil return resp["captcha_token"].(string), nil
} }
// requestXunleiApi 迅雷 API 通用请求方法 - 使用 BasePanService 方法 // requestXunleiApi 迅雷 API 通用请求方法 - 使用 BasePanService 方法
@@ -709,37 +709,47 @@ func (x *XunleiPanService) DeleteFiles(fileList []string) (*TransferResult, erro
// GetUserInfo 获取用户信息 - 实现 PanService 接口cookie 参数为 refresh_token先获取 access_token 再访问 API // GetUserInfo 获取用户信息 - 实现 PanService 接口cookie 参数为 refresh_token先获取 access_token 再访问 API
func (x *XunleiPanService) GetUserInfo(cookie *string) (*UserInfo, error) { func (x *XunleiPanService) GetUserInfo(cookie *string) (*UserInfo, error) {
log.Printf("开始获取迅雷网盘用户信息cookie 为 refresh_token") userInfo := &UserInfo{}
accessToken, err := x.getAccessToken()
// 使用 refresh_token 获取 access_token
accessTokenData, err := x.GetAccessTokenByRefreshToken(*cookie)
if err != nil { if err != nil {
return nil, fmt.Errorf("获取 access_token 失败: %v", err) return nil, err
} }
// 设置 Authorization header 为 Bearer token captchaToken, err := x.getCaptchaToken()
x.SetHeader("Authorization", "Bearer "+accessTokenData.AccessToken) if err != nil {
return nil, err
}
headers := make(map[string]string)
for k, v := range x.headers {
headers[k] = v
}
headers["Authorization"] = "Bearer " + accessToken
headers["x-captcha-token"] = captchaToken
resp, err := x.requestXunleiApi("https://api-pan.xunlei.com/drive/v1/about", "GET", nil, nil, headers)
if err != nil {
return nil, fmt.Errorf("获取用户信息失败: %v", err)
}
limit := resp["quota"].(map[string]interface{})["limit"].(string)
limitInt, _ := strconv.ParseInt(limit, 10, 64)
used := resp["quota"].(map[string]interface{})["usage"].(string)
usedInt, _ := strconv.ParseInt(used, 10, 64)
userInfo.TotalSpace = limitInt
userInfo.UsedSpace = usedInt
// 获取用户信息 // 获取用户信息
respData, err := x.HTTPGet(x.apiHost("user")+"/v1/user/me", nil) respData, err := x.requestXunleiApi(x.apiHost("user")+"/v1/user/me", "GET", nil, nil, headers)
if err != nil { if err != nil {
return nil, fmt.Errorf("获取用户信息失败: %v", err) return nil, fmt.Errorf("获取用户信息失败: %v", err)
} }
var response struct { vipInfo := respData["vip_info"].([]interface{})
Username string `json:"name"` isVip := vipInfo[0].(map[string]interface{})["is_vip"].(string) != "0"
}
if err := json.Unmarshal(respData, &response); err != nil {
return nil, fmt.Errorf("解析用户信息失败: %v", err)
}
var userInfo *UserInfo
userInfo = &UserInfo{
Username: response.Username,
ServiceType: "xunlei",
}
userInfo.Username = respData["name"].(string)
userInfo.ServiceType = x.GetServiceType().String()
userInfo.VIPStatus = isVip
return userInfo, nil return userInfo, nil
} }

View File

@@ -365,7 +365,7 @@ func RefreshCapacity(c *gin.Context) {
cks.Space = userInfo.TotalSpace cks.Space = userInfo.TotalSpace
cks.LeftSpace = leftSpaceBytes cks.LeftSpace = leftSpaceBytes
cks.UsedSpace = userInfo.UsedSpace cks.UsedSpace = userInfo.UsedSpace
cks.IsValid = userInfo.VIPStatus // 根据VIP状态更新有效性 // cks.IsValid = userInfo.VIPStatus // 根据VIP状态更新有效性
err = repoManager.CksRepository.UpdateWithAllFields(cks) err = repoManager.CksRepository.UpdateWithAllFields(cks)
if err != nil { if err != nil {