feat:自动补货/手动采购逻辑修改:批量获取-》打包-》获取Token列表
This commit is contained in:
@@ -249,6 +249,178 @@ type RockExchangeRecordsResponse struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// RockPackage 是上游打包记录(/api/user/packages)的列表项。
|
||||
type RockPackage struct {
|
||||
ID int64 `json:"id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
TokenCount int `json:"token_count"`
|
||||
ShareCode string `json:"share_code"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Note string `json:"note"`
|
||||
}
|
||||
|
||||
// RockPackageToken 是打包详情(/api/package/{share_code})里的 Token 原始结构。
|
||||
// 字段命名与 0666.pro 返回保持一致,便于把整条 JSON 原样存入 raw_data。
|
||||
type RockPackageToken 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"`
|
||||
ClaimGroup string `json:"claim_group"`
|
||||
ClaimRound int `json:"claim_round"`
|
||||
ClaimNo int `json:"claim_no"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
type RockPackageDetailResponse struct {
|
||||
Package RockPackage `json:"package"`
|
||||
Owner RockPackageOwner `json:"owner"`
|
||||
Tokens []RockPackageToken `json:"tokens"`
|
||||
Config map[string]any `json:"config"`
|
||||
Settings map[string]any `json:"settings"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type RockPackageOwner struct {
|
||||
Username string `json:"username"`
|
||||
Phone string `json:"phone"`
|
||||
}
|
||||
|
||||
// RockPackageCreateResponse 是 POST /api/user/packages 的返回结构。
|
||||
// 上游示例:{"package":{"id":1512,"token_count":2,"share_code":"1448fffa85bb3d4f913c",...}}
|
||||
type RockPackageCreateResponse struct {
|
||||
Package *RockPackage `json:"package"`
|
||||
Packages []RockPackage `json:"packages"`
|
||||
ShareCode string `json:"share_code"`
|
||||
URL string `json:"url"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// ResolvePackage 兼容 package / packages / share_code / url 四种返回形态,返回包 ID 与 share_code。
|
||||
func (r *RockPackageCreateResponse) ResolvePackage() (int64, string) {
|
||||
if r == nil {
|
||||
return 0, ""
|
||||
}
|
||||
if r.Package != nil {
|
||||
if code := strings.TrimSpace(r.Package.ShareCode); code != "" {
|
||||
return r.Package.ID, code
|
||||
}
|
||||
}
|
||||
for _, p := range r.Packages {
|
||||
if code := strings.TrimSpace(p.ShareCode); code != "" {
|
||||
return p.ID, code
|
||||
}
|
||||
}
|
||||
if code := strings.TrimSpace(r.ShareCode); code != "" {
|
||||
return 0, code
|
||||
}
|
||||
if rawURL := strings.TrimSpace(r.URL); rawURL != "" {
|
||||
if idx := strings.LastIndex(rawURL, "/"); idx >= 0 && idx < len(rawURL)-1 {
|
||||
return 0, strings.TrimSpace(rawURL[idx+1:])
|
||||
}
|
||||
}
|
||||
return 0, ""
|
||||
}
|
||||
|
||||
// CreatePackage 把一批已提取 Token 打包成公开分享包(需 Bearer Token)。
|
||||
// 注意:上游未返回 share_code 时不会在这里报错,由调用方决定兜底策略。
|
||||
func (c *RockClient) CreatePackage(ctx context.Context, bearerToken string, ids []string, note string) (*RockPackageCreateResponse, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, fmt.Errorf("打包 ids 不能为空")
|
||||
}
|
||||
reqURL := fmt.Sprintf("%s/api/user/packages", c.BaseURL())
|
||||
payload := map[string]interface{}{
|
||||
"ids": ids,
|
||||
"note": note,
|
||||
}
|
||||
jsonBytes, _ := json.Marshal(payload)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewReader(jsonBytes))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strings.TrimSpace(bearerToken)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json, text/plain, */*")
|
||||
req.Header.Set("Origin", c.BaseURL())
|
||||
req.Header.Set("Referer", c.BaseURL()+"/")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求 0666 packages 创建接口失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取 packages 创建响应失败: %w", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("上游 packages 创建接口返回错误码 %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var res RockPackageCreateResponse
|
||||
if err := json.Unmarshal(body, &res); err != nil {
|
||||
return nil, fmt.Errorf("解析 packages 创建响应失败: %w", err)
|
||||
}
|
||||
if res.Error != "" {
|
||||
return nil, fmt.Errorf("上游 packages 创建业务错误: %s", res.Error)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// GetPackageDetail 按 share_code 查询打包详情(公开接口,无需 Bearer)。
|
||||
func (c *RockClient) GetPackageDetail(ctx context.Context, shareCode string) (*RockPackageDetailResponse, error) {
|
||||
shareCode = strings.TrimSpace(shareCode)
|
||||
if shareCode == "" {
|
||||
return nil, fmt.Errorf("share_code 不能为空")
|
||||
}
|
||||
reqURL := fmt.Sprintf("%s/api/package/%s", c.BaseURL(), url.PathEscape(shareCode))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求 0666 package 详情失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取 package 详情响应失败: %w", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("上游 package 详情接口返回错误码 %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
var res RockPackageDetailResponse
|
||||
if err := json.Unmarshal(body, &res); err != nil {
|
||||
return nil, fmt.Errorf("解析 package 详情失败: %w", err)
|
||||
}
|
||||
if res.Error != "" {
|
||||
return nil, fmt.Errorf("上游 package 详情业务错误: %s", res.Error)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// GetMe 获取商户个人信息及积分
|
||||
func (c *RockClient) GetMe(ctx context.Context, bearerToken string) (*RockMeResponse, error) {
|
||||
reqURL := fmt.Sprintf("%s/api/me", c.BaseURL())
|
||||
@@ -287,6 +459,82 @@ func (c *RockClient) GetMe(ctx context.Context, bearerToken string) (*RockMeResp
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// RockDashboardResponse 是 GET /api/user/dashboard 的返回结构。
|
||||
type RockDashboardResponse struct {
|
||||
Available struct {
|
||||
Black int `json:"black"`
|
||||
Blue int `json:"blue"`
|
||||
} `json:"available"`
|
||||
EffectiveTokenCosts struct {
|
||||
BlackTokenCost FlexibleFloat64 `json:"blackTokenCost"`
|
||||
BlueTokenCost FlexibleFloat64 `json:"blueTokenCost"`
|
||||
} `json:"effectiveTokenCosts"`
|
||||
Config struct {
|
||||
BlackTokenCost FlexibleFloat64 `json:"blackTokenCost"`
|
||||
BlueTokenCost FlexibleFloat64 `json:"blueTokenCost"`
|
||||
} `json:"config"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// ResolveBlackTokenCost 优先取 effectiveTokenCosts,其次 config。
|
||||
func (r *RockDashboardResponse) ResolveBlackTokenCost() float64 {
|
||||
if r == nil {
|
||||
return 0
|
||||
}
|
||||
if cost := float64(r.EffectiveTokenCosts.BlackTokenCost); cost > 0 {
|
||||
return cost
|
||||
}
|
||||
return float64(r.Config.BlackTokenCost)
|
||||
}
|
||||
|
||||
func (r *RockDashboardResponse) ResolveBlueTokenCost() float64 {
|
||||
if r == nil {
|
||||
return 0
|
||||
}
|
||||
if cost := float64(r.EffectiveTokenCosts.BlueTokenCost); cost > 0 {
|
||||
return cost
|
||||
}
|
||||
return float64(r.Config.BlueTokenCost)
|
||||
}
|
||||
|
||||
// GetDashboard 获取上游可用库存与实时采购单价(需 Bearer Token)。
|
||||
func (c *RockClient) GetDashboard(ctx context.Context, bearerToken string) (*RockDashboardResponse, error) {
|
||||
reqURL := fmt.Sprintf("%s/api/user/dashboard", c.BaseURL())
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strings.TrimSpace(bearerToken)))
|
||||
req.Header.Set("Accept", "application/json, text/plain, */*")
|
||||
req.Header.Set("Origin", c.BaseURL())
|
||||
req.Header.Set("Referer", c.BaseURL()+"/")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求 0666 dashboard 失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取 dashboard 响应失败: %w", err)
|
||||
}
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
return nil, fmt.Errorf("上游 Bearer Token 已失效或未授权 (401)")
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("上游 dashboard 接口返回错误码 %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
var res RockDashboardResponse
|
||||
if err := json.Unmarshal(body, &res); err != nil {
|
||||
return nil, fmt.Errorf("解析 dashboard 响应失败: %w", err)
|
||||
}
|
||||
if res.Error != "" {
|
||||
return nil, fmt.Errorf("上游 dashboard 业务错误: %s", res.Error)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// ClaimTokens 批量提取采购 Token
|
||||
func (c *RockClient) ClaimTokens(ctx context.Context, bearerToken string, color string, count int) ([]RockClaimedToken, error) {
|
||||
if count <= 0 {
|
||||
|
||||
Reference in New Issue
Block a user