fix(rock): 修复兑换发货401未鉴权错误与买家角色名提取解析

1. 根因修复(兑换发货鉴权):对比原生平台发货抓包证实 POST /api/redeem/:id 必须携带 Authorization: Bearer <token>,Origin 及 Referer 头,补齐相关请求头彻底杜绝 401 报错;
2. 根因修复(角色昵称解析):逆向解构 0666.pro 响应报文,将角色名提取从错误的 binding.role_name 切换为 token.bound_account,消除提货页角色名显示为 '-' 的问题;
3. 协议对齐(响应结构重构):将 RockRedeemResponse 与真实上游返回对齐(包含 token 与 status 结构),剔除臆想的 delivery 字段;
4. 单元测试与方案文档:更新单测覆盖鉴权与字段映射断言,在方案文档 7.4 节录入实测抓包报文镜像比对表。
This commit is contained in:
yml2213
2026-10-05 16:30:18 +08:00
parent befe50b3e5
commit 83870843ed
5 changed files with 279 additions and 91 deletions
+83 -36
View File
@@ -99,15 +99,55 @@ type RockClaimResponse struct {
Error string `json:"error,omitempty"`
}
type RockTokenData struct {
ID int64 `json:"id"`
RedeemCode string `json:"redeem_code"`
Token string `json:"token"`
Color string `json:"color"`
Region string `json:"region"`
Phone string `json:"phone"`
Status string `json:"status"`
BoundAccount string `json:"bound_account"`
BoundAccountID string `json:"bound_account_id"`
PreviousBoundAccount string `json:"previous_bound_account"`
PreviousBoundAccountID string `json:"previous_bound_account_id"`
BindChangedAt string `json:"bind_changed_at"`
BindChangeType string `json:"bind_change_type"`
RedeemSkin string `json:"redeem_skin"`
RedeemCurrency string `json:"redeem_currency"`
RedeemedAt string `json:"redeemed_at"`
}
func (t *RockTokenData) IsBound() bool {
if t.Status == "bound" || t.Status == "redeemed" {
return true
}
return strings.TrimSpace(t.BoundAccountID) != ""
}
func (t *RockTokenData) GetRoleName() string {
return strings.TrimSpace(t.BoundAccount)
}
func (t *RockTokenData) GetRoleID() string {
return strings.TrimSpace(t.BoundAccountID)
}
func (t *RockTokenData) GetChannel() string {
return strings.TrimSpace(t.Region)
}
type RockRedeemDetailResponse struct {
ID int64 `json:"id"`
RedeemCode string `json:"redeem_code"`
Color string `json:"color"`
Status string `json:"status"`
BindURL string `json:"bindUrl"`
QRURL string `json:"qrUrl"`
ExpiresAt string `json:"expires_at"`
Error string `json:"error,omitempty"`
ID int64 `json:"id"`
RedeemCode string `json:"redeem_code"`
Color string `json:"color"`
Status string `json:"status"`
BindURL string `json:"bindUrl"`
BindError string `json:"bindError"`
QRURL string `json:"qrUrl"`
Token RockTokenData `json:"token"`
ExpiresAt string `json:"expires_at"`
Error string `json:"error,omitempty"`
}
type RockDeliveryItem struct {
@@ -124,35 +164,37 @@ type RockDeliveryItem struct {
}
type RockRefreshResponse struct {
Binding struct {
Bound bool `json:"bound"`
RoleName string `json:"roleName"`
RoleID string `json:"roleId"`
Channel string `json:"channel"`
} `json:"binding"`
Token struct {
ID int64 `json:"id"`
Status string `json:"status"`
BoundAccount string `json:"bound_account"`
BoundAccountID string `json:"bound_account_id"`
PreviousBoundAccount string `json:"previous_bound_account"`
BindChangedAt string `json:"bind_changed_at"`
BindChangeType string `json:"bind_change_type"`
} `json:"token"`
Token RockTokenData `json:"token"`
DeliveryList []RockDeliveryItem `json:"deliveryList"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
}
func (r *RockRefreshResponse) IsBound() bool {
return r.Token.IsBound()
}
func (r *RockRefreshResponse) GetRoleName() string {
return r.Token.GetRoleName()
}
func (r *RockRefreshResponse) GetRoleID() string {
return r.Token.GetRoleID()
}
func (r *RockRefreshResponse) GetChannel() string {
return r.Token.GetChannel()
}
type RockRedeemResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Delivery struct {
Skin string `json:"skin"`
Currency string `json:"currency"`
Quantity int `json:"quantity"`
FulfillID int64 `json:"fulfillId"`
} `json:"delivery"`
Error string `json:"error,omitempty"`
Token RockTokenData `json:"token"`
Status string `json:"status"`
Message string `json:"message"`
Error string `json:"error,omitempty"`
}
func (r *RockRedeemResponse) IsSuccess() bool {
return r.Status == "success" || r.Token.Status == "redeemed"
}
type RockExchangeRecordsResponse struct {
@@ -327,8 +369,8 @@ func (c *RockClient) RefreshToken(ctx context.Context, bearerToken string, upstr
return &res, nil
}
// RedeemSkin 执行最终皮肤兑换发货
func (c *RockClient) RedeemSkin(ctx context.Context, upstreamTokenID, skin, currency, account string) (*RockRedeemResponse, error) {
// RedeemSkin 执行最终皮肤兑换发货(需携带商户 Bearer Token 鉴权)
func (c *RockClient) RedeemSkin(ctx context.Context, bearerToken, upstreamTokenID, skin, currency, account string) (*RockRedeemResponse, error) {
reqURL := fmt.Sprintf("%s/api/redeem/%s", c.baseURL, url.PathEscape(upstreamTokenID))
payload := map[string]interface{}{
"skin": skin,
@@ -341,8 +383,13 @@ func (c *RockClient) RedeemSkin(ctx context.Context, upstreamTokenID, skin, curr
if err != nil {
return nil, err
}
if bearerToken != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strings.TrimSpace(bearerToken)))
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("Origin", "https://0666.pro")
req.Header.Set("Referer", "https://0666.pro/")
resp, err := c.httpClient.Do(req)
if err != nil {
@@ -363,7 +410,7 @@ func (c *RockClient) RedeemSkin(ctx context.Context, upstreamTokenID, skin, curr
if err := json.Unmarshal(body, &res); err != nil {
return nil, fmt.Errorf("解析兑换响应失败: %w", err)
}
if !res.Success && res.Error != "" {
if !res.IsSuccess() && res.Error != "" {
return nil, fmt.Errorf("上游兑换失败: %s", res.Error)
}
return &res, nil