精英手册改为锁单支付并发布0.2.2
- 精英手册兑换改为 createExchangeOrder 锁单后 payExchangeOrder 支付,不再查询商品状态与预兑 - 电竞手册保留直接兑换,标准模式重新取鉴权,激进模式复用鉴权 - csrfToken 优先复用 Cookie,校验失败自动刷新后重试一次 - 锁单支付按频控/通用退避表重试,临近过期不再重试 - 定时复校发现抢购已开始则不再改写触发时刻 - 升级 wails 至 v2.15.0
This commit is contained in:
+178
-1
@@ -1018,8 +1018,13 @@ func (c *DouyuClient) Score(cookie string) (int, error) {
|
||||
return result.Data.Balance, nil
|
||||
}
|
||||
|
||||
// GenerateCSRF 生成当前活动需要的鉴权 token。
|
||||
// GenerateCSRF 获取当前活动需要的鉴权 token。
|
||||
// 精英手册优先复用 Cookie 里的 cvl_csrf_token,缺失时才请求生成接口。
|
||||
func (c *DouyuClient) GenerateCSRF(cookie string) (string, error) {
|
||||
return c.generateCSRF(cookie, false)
|
||||
}
|
||||
|
||||
func (c *DouyuClient) generateCSRF(cookie string, forceRefresh bool) (string, error) {
|
||||
if c.Activity.IsXiaodian {
|
||||
return "", nil
|
||||
}
|
||||
@@ -1030,6 +1035,11 @@ func (c *DouyuClient) GenerateCSRF(cookie string) (string, error) {
|
||||
}
|
||||
return combineAuthToken("", ctn), nil
|
||||
}
|
||||
if !forceRefresh {
|
||||
if token := cookieValue(cookie, "cvl_csrf_token"); token != "" {
|
||||
return token, nil
|
||||
}
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", c.Activity.CSRFURL, nil)
|
||||
if err != nil {
|
||||
@@ -1212,6 +1222,173 @@ func (c *DouyuClient) PreExchange(cookie, commodityID string, csrf *string) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// LockedExchangeOrder 是精英手册锁单结果。
|
||||
type LockedExchangeOrder struct {
|
||||
OrderID string
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// CreateExchangeOrder 锁定精英手册商品,不查询商品详情或预兑状态。
|
||||
func (c *DouyuClient) CreateExchangeOrder(cookie, commodityID string, num int, csrf *string) (LockedExchangeOrder, error) {
|
||||
if strings.TrimSpace(c.Activity.CreateOrderURL) == "" {
|
||||
return LockedExchangeOrder{}, errors.New("当前活动没有锁单接口")
|
||||
}
|
||||
if num < 1 {
|
||||
num = 1
|
||||
}
|
||||
result, err := c.postCSRFForm(c.Activity.CreateOrderURL, cookie, "锁定兑换商品", map[string]string{
|
||||
"manualID": c.Activity.ManualID,
|
||||
"rid": c.Activity.RoomID,
|
||||
"commodityID": commodityID,
|
||||
"num": strconv.Itoa(num),
|
||||
}, csrf)
|
||||
if err != nil {
|
||||
return LockedExchangeOrder{}, err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
if strings.TrimSpace(result.Msg) == "" {
|
||||
return LockedExchangeOrder{}, errors.New("锁定兑换商品失败")
|
||||
}
|
||||
return LockedExchangeOrder{}, fmt.Errorf("锁定兑换商品失败: %s", result.Msg)
|
||||
}
|
||||
var data struct {
|
||||
OrderID string `json:"orderId"`
|
||||
ExpireSeconds json.RawMessage `json:"expireSeconds"`
|
||||
}
|
||||
if err := json.Unmarshal(result.Data, &data); err != nil {
|
||||
return LockedExchangeOrder{}, fmt.Errorf("锁单数据解析失败: %w", err)
|
||||
}
|
||||
orderID := strings.TrimSpace(data.OrderID)
|
||||
if orderID == "" {
|
||||
return LockedExchangeOrder{}, errors.New("锁定兑换商品成功,但响应中没有 orderId")
|
||||
}
|
||||
lockedAt := time.Now()
|
||||
expiresAt := lockedAt.Add(300 * time.Second)
|
||||
if seconds := rawInt(data.ExpireSeconds); seconds > 0 {
|
||||
expiresAt = lockedAt.Add(time.Duration(seconds) * time.Second)
|
||||
}
|
||||
return LockedExchangeOrder{OrderID: orderID, ExpiresAt: expiresAt}, nil
|
||||
}
|
||||
|
||||
// PayExchangeOrder 支付一笔已锁定的精英手册订单。
|
||||
func (c *DouyuClient) PayExchangeOrder(cookie, orderID string, csrf *string) (string, error) {
|
||||
if strings.TrimSpace(c.Activity.PayOrderURL) == "" {
|
||||
return "", errors.New("当前活动没有锁单支付接口")
|
||||
}
|
||||
orderID = strings.TrimSpace(orderID)
|
||||
if orderID == "" {
|
||||
return "", errors.New("锁单订单号不能为空")
|
||||
}
|
||||
result, err := c.postCSRFForm(c.Activity.PayOrderURL, cookie, "支付锁单", map[string]string{
|
||||
"orderID": orderID,
|
||||
"manualID": c.Activity.ManualID,
|
||||
}, csrf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
if alreadyPaid(result.Msg) {
|
||||
return "orderId=" + orderID + ",alreadyPaid", nil
|
||||
}
|
||||
if strings.TrimSpace(result.Msg) == "" {
|
||||
return "", errors.New("支付锁单失败")
|
||||
}
|
||||
return "", fmt.Errorf("支付锁单失败: %s", result.Msg)
|
||||
}
|
||||
var data struct {
|
||||
ExchangeID json.RawMessage `json:"exchangeId"`
|
||||
ExchangeNum json.RawMessage `json:"exchangeNum"`
|
||||
}
|
||||
if err := json.Unmarshal(result.Data, &data); err != nil {
|
||||
return "", fmt.Errorf("支付数据解析失败: %w", err)
|
||||
}
|
||||
exchangeID := rawString(data.ExchangeID)
|
||||
exchangeNum := rawString(data.ExchangeNum)
|
||||
if exchangeID == "" && exchangeNum == "" {
|
||||
return "", errors.New("支付数据解析失败: 响应中没有兑换结果")
|
||||
}
|
||||
return fmt.Sprintf("exchangeId=%s,num=%s", exchangeID, exchangeNum), nil
|
||||
}
|
||||
|
||||
func alreadyPaid(message string) bool {
|
||||
return strings.Contains(message, "订单已支付") || strings.Contains(message, "请勿重复支付")
|
||||
}
|
||||
|
||||
func (c *DouyuClient) postCSRFForm(endpoint, cookie, label string, fields map[string]string, csrf *string) (types.APIResponse, error) {
|
||||
var result types.APIResponse
|
||||
token := ""
|
||||
if csrf != nil {
|
||||
token = strings.SplitN(*csrf, "\x00", 2)[0]
|
||||
}
|
||||
if token == "" {
|
||||
var err error
|
||||
token, err = c.GenerateCSRF(cookie)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if csrf != nil {
|
||||
*csrf = token
|
||||
}
|
||||
}
|
||||
fields["csrfToken"] = token
|
||||
if err := c.postMultipart(endpoint, cookie, csrf, label, fields, &result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if result.Error != 0 && csrfError(result.Msg) {
|
||||
logger.LogEvent("网络", "info", "%s: csrfToken 校验失败,刷新后重试一次", label)
|
||||
token, err := c.generateCSRF(cookie, true)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if csrf != nil {
|
||||
*csrf = token
|
||||
}
|
||||
fields["csrfToken"] = token
|
||||
result = types.APIResponse{}
|
||||
if err := c.postMultipart(endpoint, cookie, csrf, label, fields, &result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func csrfError(message string) bool {
|
||||
return strings.Contains(strings.ToLower(message), "csrf")
|
||||
}
|
||||
|
||||
func rawString(raw json.RawMessage) string {
|
||||
raw = bytesTrimJSON(raw)
|
||||
if len(raw) == 0 || string(raw) == "null" {
|
||||
return ""
|
||||
}
|
||||
var s string
|
||||
if err := json.Unmarshal(raw, &s); err == nil {
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
return string(raw)
|
||||
}
|
||||
|
||||
func rawInt(raw json.RawMessage) int {
|
||||
raw = bytesTrimJSON(raw)
|
||||
if len(raw) == 0 || string(raw) == "null" {
|
||||
return 0
|
||||
}
|
||||
var n int
|
||||
if err := json.Unmarshal(raw, &n); err == nil {
|
||||
return n
|
||||
}
|
||||
var s string
|
||||
if err := json.Unmarshal(raw, &s); err == nil {
|
||||
n, _ = strconv.Atoi(strings.TrimSpace(s))
|
||||
return n
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func bytesTrimJSON(raw json.RawMessage) json.RawMessage {
|
||||
return json.RawMessage(strings.TrimSpace(string(raw)))
|
||||
}
|
||||
|
||||
// Exchange 正式兑换一个商品。
|
||||
func (c *DouyuClient) Exchange(cookie, commodityID string, csrf *string) (string, error) {
|
||||
if c.Activity.IsXiaodian {
|
||||
|
||||
Reference in New Issue
Block a user