feat(seckill): 增加只读预演并强化代理风控切换
This commit is contained in:
+11
-10
@@ -1207,7 +1207,7 @@ func (c *DouyuClient) generateCSRF(cookie string, forceRefresh bool) (string, er
|
||||
traceErr = api.Msg
|
||||
c.Trace.RecordResponse("生成CSRF", req, resp, resp.StatusCode, time.Since(start), reqBody, body, traceErr, api.Error, api.Msg)
|
||||
c.logNetworkAPIResult("生成CSRF", req, time.Since(start), api.Error, api.Msg)
|
||||
return "", fmt.Errorf("CSRF 接口失败: %s", api.Msg)
|
||||
return "", &OrderError{Kind: "CSRF接口失败", Code: api.Error, Msg: api.Msg}
|
||||
}
|
||||
for _, ck := range resp.Cookies() {
|
||||
if ck.Name == "cvl_csrf_token" && ck.Value != "" {
|
||||
@@ -1306,7 +1306,7 @@ func (c *DouyuClient) BatchExchangeLimit(cookie, commodityID string, csrf *strin
|
||||
return 0, err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
return 0, fmt.Errorf("批量限制接口失败: %s", result.Msg)
|
||||
return 0, &OrderError{Kind: "批量限制接口失败", Code: result.Error, Msg: result.Msg}
|
||||
}
|
||||
var data struct {
|
||||
Limit string `json:"limit"`
|
||||
@@ -1344,7 +1344,7 @@ func (c *DouyuClient) PreExchange(cookie, commodityID string, csrf *string) erro
|
||||
return err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
return fmt.Errorf("预兑换失败: %s", result.Msg)
|
||||
return &OrderError{Kind: "预兑换失败", Code: result.Error, Msg: result.Msg}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1362,10 +1362,14 @@ type OrderError struct {
|
||||
}
|
||||
|
||||
func (e *OrderError) Error() string {
|
||||
if strings.TrimSpace(e.Msg) == "" {
|
||||
return e.Kind
|
||||
kind := e.Kind
|
||||
if e.Code != 0 {
|
||||
kind += fmt.Sprintf("(error=%d)", e.Code)
|
||||
}
|
||||
return e.Kind + ": " + e.Msg
|
||||
if strings.TrimSpace(e.Msg) == "" {
|
||||
return kind
|
||||
}
|
||||
return kind + ": " + e.Msg
|
||||
}
|
||||
|
||||
// LockedExchangeOrder 是精英手册锁单结果。
|
||||
@@ -1581,10 +1585,7 @@ func (c *DouyuClient) Exchange(cookie, commodityID string, csrf *string) (string
|
||||
}
|
||||
}
|
||||
if result.Error != 0 {
|
||||
if strings.TrimSpace(result.Msg) == "" {
|
||||
return "", errors.New("兑换失败,接口未返回原因")
|
||||
}
|
||||
return "", fmt.Errorf("兑换失败: %s", result.Msg)
|
||||
return "", &OrderError{Kind: "兑换失败", Code: result.Error, Msg: result.Msg}
|
||||
}
|
||||
var data struct {
|
||||
ExchangeID int `json:"exchangeId"`
|
||||
|
||||
@@ -16,6 +16,18 @@ import (
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
func TestOrderErrorIncludesCode(t *testing.T) {
|
||||
err := (&OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: 1644400007,
|
||||
Msg: "活动火爆,请稍后再试",
|
||||
}).Error()
|
||||
want := "锁定兑换商品失败(error=1644400007): 活动火爆,请稍后再试"
|
||||
if err != want {
|
||||
t.Fatalf("OrderError.Error()=%q, want %q", err, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateCSRFUseCTNReusesCookie(t *testing.T) {
|
||||
var called atomic.Bool
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user