feat(rock): 同一提货号过期后重取二维码
轮询绑定时向 0666 换一条新的活动链接,提货号不变。下游只展示返回的链接。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -20,12 +20,22 @@ type rockRefreshCacheItem struct {
|
||||
cachedAt time.Time
|
||||
}
|
||||
|
||||
// 腾讯活动链接按生成时间签名。同一提货号过期后重取链接,不另发新号。
|
||||
const rockBindLinkMaxAge = 8 * time.Minute
|
||||
|
||||
type rockBindLinkCacheItem struct {
|
||||
bindURL string
|
||||
qrURL string
|
||||
cachedAt time.Time
|
||||
}
|
||||
|
||||
// RockService 负责管理 Rock 平台的配置、库存池、采购、UID强一致风控与状态机
|
||||
type RockService struct {
|
||||
db *gorm.DB
|
||||
client *RockClient
|
||||
mu sync.Mutex
|
||||
refreshCache sync.Map // key: upstreamTokenID string, value: rockRefreshCacheItem
|
||||
db *gorm.DB
|
||||
client *RockClient
|
||||
mu sync.Mutex
|
||||
refreshCache sync.Map // key: upstreamTokenID string, value: rockRefreshCacheItem
|
||||
bindLinkCache sync.Map // key: token ID uint, value: rockBindLinkCacheItem
|
||||
}
|
||||
|
||||
func NewRockService(db *gorm.DB, client *RockClient) *RockService {
|
||||
@@ -35,6 +45,60 @@ func NewRockService(db *gorm.DB, client *RockClient) *RockService {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *RockService) rememberBindLink(tokenID uint, bindURL, qrURL string) {
|
||||
bindURL = strings.TrimSpace(bindURL)
|
||||
if s == nil || tokenID == 0 || bindURL == "" {
|
||||
return
|
||||
}
|
||||
qrURL = strings.TrimSpace(qrURL)
|
||||
if qrURL == "" {
|
||||
qrURL = qrCodeURL(bindURL)
|
||||
}
|
||||
s.bindLinkCache.Store(tokenID, rockBindLinkCacheItem{
|
||||
bindURL: bindURL,
|
||||
qrURL: qrURL,
|
||||
cachedAt: time.Now(),
|
||||
})
|
||||
}
|
||||
|
||||
// freshBindLink 只为仍在扫码的同一提货号重取活动链接。过期前复用缓存,避免每轮轮询都打 0666。
|
||||
func (s *RockService) freshBindLink(ctx context.Context, tokenID uint, orderNo string) (string, string) {
|
||||
if s == nil || s.client == nil || tokenID == 0 {
|
||||
return "", ""
|
||||
}
|
||||
var token model.RockToken
|
||||
if err := s.db.WithContext(ctx).First(&token, tokenID).Error; err != nil {
|
||||
return "", ""
|
||||
}
|
||||
if token.Status != model.RockStatusUsing || strings.TrimSpace(token.RedeemCode) == "" {
|
||||
return "", ""
|
||||
}
|
||||
if orderNo = strings.TrimSpace(orderNo); orderNo != "" && token.AssignedOrderNo != "" && token.AssignedOrderNo != orderNo {
|
||||
return "", ""
|
||||
}
|
||||
if item, ok := s.bindLinkCache.Load(token.ID); ok {
|
||||
cached := item.(rockBindLinkCacheItem)
|
||||
if cached.bindURL != "" && time.Since(cached.cachedAt) < rockBindLinkMaxAge {
|
||||
return cached.bindURL, cached.qrURL
|
||||
}
|
||||
}
|
||||
detail, err := s.client.GetRedeemDetail(ctx, token.RedeemCode)
|
||||
if err != nil || detail == nil || strings.TrimSpace(detail.BindURL) == "" {
|
||||
if item, ok := s.bindLinkCache.Load(token.ID); ok {
|
||||
cached := item.(rockBindLinkCacheItem)
|
||||
return cached.bindURL, cached.qrURL
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
bindURL := strings.TrimSpace(detail.BindURL)
|
||||
qrURL := strings.TrimSpace(detail.QRURL)
|
||||
s.rememberBindLink(token.ID, bindURL, qrURL)
|
||||
if qrURL == "" {
|
||||
qrURL = qrCodeURL(bindURL)
|
||||
}
|
||||
return bindURL, qrURL
|
||||
}
|
||||
|
||||
// RockStatsResponse 看板与库存统计返回
|
||||
type RockStatsResponse struct {
|
||||
AvailableStock int64 `json:"available_stock"`
|
||||
@@ -435,6 +499,7 @@ func (s *RockService) AssignTokenForOrder(ctx context.Context, orderNo, expected
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("获取上游活动绑定链接失败: %w", err)
|
||||
}
|
||||
s.rememberBindLink(token.ID, detail.BindURL, detail.QRURL)
|
||||
|
||||
// 4. 首次分配记录上游初始绑定快照(防范 Ghost-Binding 历史残留数据)
|
||||
if token.InitialBoundSnapshot == "" && cfg.BearerToken != "" {
|
||||
|
||||
Reference in New Issue
Block a user