重构: 账号管理抽离、强类型返回、消除反射、删除死字段

- 抽离账号管理为独立 AccountStore,Scheduler 只持有引用,
  职责单一化(scheduler.go 减少 ~110 行)
- Wails 绑定方法返回结构体替代 map[string]any(新增 responses.go),
  前端获得强类型,字段名保持兼容无需改前端
- ExchangeRecord 移至 types 包,消除 types↔client 循环依赖
- extractAPIStatus 用 apiResponder 接口替代 reflect 逐字段查找,
  types.APIResponse 与 ExchangeListResponse 实现 APIStatus 方法
- 删除 types.Account.Password 死字段(鉴权仅用 Cookie)
- 重新生成 Wails 绑定,前端 vue-tsc 类型检查通过
This commit is contained in:
yml2213
2026-07-29 12:47:01 +08:00
parent 3ae9cb4a42
commit 7c2c5a61a6
7 changed files with 450 additions and 287 deletions
+18 -41
View File
@@ -7,7 +7,6 @@ import (
"io"
"mime/multipart"
"net/http"
"reflect"
"strconv"
"strings"
"time"
@@ -498,29 +497,23 @@ func (c *DouyuClient) RoleName(cookie string) (string, error) {
return result.Data.GameRole.RoleName, nil
}
// ExchangeRecord 兑换记录。
type ExchangeRecord struct {
CommodityID string `json:"commodityId"`
CommodityName string `json:"commodityName"`
NickName string `json:"nickName"`
ShowName string `json:"showName"`
ExchangeNum int `json:"exchangeNum"`
Time string `json:"time"`
Score string `json:"score"`
}
// ExchangeListResponse 兑换列表响应。
type ExchangeListResponse struct {
Error int `json:"error"`
Msg string `json:"msg"`
Data struct {
List []ExchangeRecord `json:"list"`
Count int `json:"count"`
List []types.ExchangeRecord `json:"list"`
Count int `json:"count"`
} `json:"data"`
}
// APIStatus 返回斗鱼 API 的 error/msg 状态,实现 apiResponder 接口。
func (r ExchangeListResponse) APIStatus() (int, string, bool) {
return r.Error, r.Msg, true
}
// ExchangeList 查询兑换记录列表。
func (c *DouyuClient) ExchangeList(cookie string) ([]ExchangeRecord, error) {
func (c *DouyuClient) ExchangeList(cookie string) ([]types.ExchangeRecord, error) {
var result ExchangeListResponse
if err := c.getJSON(c.Activity.ExchangeListURL, cookie, "兑换记录", &result); err != nil {
return nil, err
@@ -675,36 +668,20 @@ func extractAPIStatus(out any) (int, string, bool) {
if out == nil {
return 0, "", false
}
v := reflect.ValueOf(out)
if v.Kind() == reflect.Pointer {
if v.IsNil() {
return 0, "", false
}
v = v.Elem()
}
if !v.IsValid() {
return 0, "", false
}
if v.Type() == reflect.TypeOf(types.APIResponse{}) {
api := v.Interface().(types.APIResponse)
return api.Error, api.Msg, true
}
if v.Kind() != reflect.Struct {
return 0, "", false
}
if field := v.FieldByName("APIResponse"); field.IsValid() && field.CanInterface() {
if api, ok := field.Interface().(types.APIResponse); ok {
return api.Error, api.Msg, true
}
}
errorField := v.FieldByName("Error")
msgField := v.FieldByName("Msg")
if errorField.IsValid() && msgField.IsValid() && errorField.CanInt() && msgField.Kind() == reflect.String {
return int(errorField.Int()), msgField.String(), true
if r, ok := out.(apiResponder); ok {
return r.APIStatus()
}
return 0, "", false
}
// apiResponder 返回斗鱼 API 的 error/msg 状态。
// types.APIResponse 通过其方法满足该接口;
// 未嵌入 APIResponse 但自带 Error/Msg 字段的结构体(如 ExchangeListResponse)
// 通过实现 APIStatus 方法满足该接口,避免使用反射逐字段查找。
type apiResponder interface {
APIStatus() (int, string, bool)
}
func (c *DouyuClient) setBrowserHeaders(req *http.Request, cookie string) {
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en-US;q=0.6,en;q=0.5")