Files
Ydisks-Xianyu-Helper/internal/xianyu/mtop/order_detail.go
T

141 lines
4.6 KiB
Go

// Package mtop: 订单详情域 — mtop.idle.web.trade.order.detail 调用与重试。
package mtop
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"xianyu-go/internal/xianyu/protocol"
)
// OrderDetailResult 是订单详情接口中自动发货需要的字段。
type OrderDetailResult struct {
Quantity string
SpecName string
SpecValue string
OrderStatus string
Amount string
UpdatedCookies string
}
// FetchOrderDetail 获取订单真实成交价、数量、状态和规格;token 过期时自动重签重试。
func (c *ClientImpl) FetchOrderDetail(ctx context.Context, cookiesStr, orderID string) (*OrderDetailResult, error) {
currentCookies := cookiesStr
if session := cookieSessionFromContext(ctx); session != nil {
currentCookies, _, _ = session.State()
}
var lastRet []string
for attempt := 0; attempt < 4; attempt++ {
previousCookies := currentCookies
result, ret, updated, err := c.fetchOrderDetailOnce(ctx, currentCookies, orderID)
if err != nil {
return nil, err
}
lastRet = ret
if updated != "" {
currentCookies = updated
}
if result != nil {
result.UpdatedCookies = currentCookies
return result, nil
}
if isSessionExpiredRet(ret) {
return nil, sessionExpiredError("订单详情接口", ret)
}
if !isTokenExpiredRet(ret) {
return nil, fmt.Errorf("订单详情接口返回非成功: ret=%v", ret)
}
if attempt == 3 {
break
}
if currentCookies == previousCookies {
refreshed, refreshErr := c.RefreshTokenContext(ctx, currentCookies)
if refreshErr != nil {
return nil, fmt.Errorf("订单详情 token 刷新失败: %w", refreshErr)
}
currentCookies = refreshed.UpdatedCookies
}
if err := sleepCtx(ctx, MTopRetryGap); err != nil {
return nil, err
}
}
return nil, fmt.Errorf("订单详情 token 重试失败: ret=%v", lastRet)
}
func (c *ClientImpl) fetchOrderDetailOnce(ctx context.Context, cookiesStr, orderID string) (*OrderDetailResult, []string, string, error) {
hc := c.httpClient()
endpoint := c.OrderDetailURL
if endpoint == "" {
endpoint = OrderDetailAPI
}
documentURL := "https://www.goofish.com/order-detail?orderId=" + url.QueryEscape(orderID) + "&role=seller"
signingCookies, requestCookies := mtopRequestCookies(ctx, cookiesStr, documentURL, endpoint)
t := strconv.FormatInt(time.Now().UnixMilli(), 10)
dataVal := `{"tid":"` + orderID + `"}`
sign := protocol.GenerateSign(t, protocol.SignToken(signingCookies), dataVal)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint+"?"+buildOrderDetailQuery(t, sign), strings.NewReader("data="+url.QueryEscape(dataVal)))
if err != nil {
return nil, nil, cookiesStr, err
}
setCommonHeaders(req, requestCookies)
req.Header.Set("Referer", documentURL)
resp, err := hc.Do(req)
if err != nil {
return nil, nil, cookiesStr, fmt.Errorf("订单详情请求失败: %w", err)
}
defer resp.Body.Close()
updated := absorbMTopResponseCookies(ctx, cookiesStr, resp)
raw, err := readMTopBody(resp)
if err != nil {
return nil, nil, updated, err
}
var decoded struct {
Ret []string `json:"ret"`
Data map[string]any `json:"data"`
}
if err := json.Unmarshal(raw, &decoded); err != nil {
return nil, nil, updated, fmt.Errorf("解析订单详情响应失败: %w (body=%s)", err, truncate(string(raw), 300))
}
if !hasMTopSuccess(decoded.Ret) {
return nil, decoded.Ret, updated, nil
}
result := &OrderDetailResult{Quantity: "1"}
if utArgs, ok := decoded.Data["utArgs"].(map[string]any); ok {
result.OrderStatus = mtopString(utArgs["orderStatus"])
}
components, _ := decoded.Data["components"].([]any)
for _, component := range components {
cm, _ := component.(map[string]any)
if cm["render"] != "orderInfoVO" {
continue
}
componentData, _ := cm["data"].(map[string]any)
if itemInfo, ok := componentData["itemInfo"].(map[string]any); ok {
if value := mtopString(itemInfo["buyAmount"]); value != "" {
result.Quantity = value
}
result.SpecName = mtopString(itemInfo["specName"])
result.SpecValue = mtopString(itemInfo["specValue"])
}
if priceInfo, ok := componentData["priceInfo"].(map[string]any); ok {
if amount, ok := priceInfo["amount"].(map[string]any); ok {
result.Amount = mtopString(amount["value"])
}
}
}
return result, decoded.Ret, updated, nil
}
func buildOrderDetailQuery(t, sign string) string {
return "jsv=2.7.2&appKey=" + protocol.SignAppKey +
"&t=" + t + "&sign=" + sign +
"&v=1.0&type=originaljson&accountSite=xianyu&dataType=json&timeout=20000" +
"&api=mtop.idle.web.trade.order.detail&sessionOption=AutoLoginOnly&valueType=string"
}