fix(rock): 按链接签名时间提前刷新二维码,扫码查询改为 5 秒
腾讯活动链接大约 10 分钟后签名过期。从链接 timestamp 起满 8 分钟就换新码,避免买家扫到失效二维码。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,6 +2,8 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -13,13 +15,51 @@ type rockRefreshCacheItem struct {
|
||||
cachedAt time.Time
|
||||
}
|
||||
|
||||
// 腾讯活动链接按生成时间签名。同一提货号过期后重取链接,不另发新号。
|
||||
// 腾讯「查询绑定」按链接里的 timestamp 验签,大约 10 分钟后返回「签名已过期」。
|
||||
// 从签名时间起满 8 分钟就换一条,留出余量,买家不会扫到已经失效的码。
|
||||
const rockBindLinkMaxAge = 8 * time.Minute
|
||||
|
||||
// 上游若仍返回同一条旧签名,不要每个轮询都再打一次。
|
||||
const rockBindLinkRetryGap = 15 * time.Second
|
||||
|
||||
type rockBindLinkCacheItem struct {
|
||||
bindURL string
|
||||
qrURL string
|
||||
cachedAt time.Time
|
||||
bindURL string
|
||||
qrURL string
|
||||
cachedAt time.Time // 链接上没有 timestamp 时,用写入时间代替签名时间
|
||||
fetchedAt time.Time
|
||||
}
|
||||
|
||||
func rockBindLinkIssuedAt(bindURL string) (time.Time, bool) {
|
||||
parsed, err := url.Parse(strings.TrimSpace(bindURL))
|
||||
if err != nil {
|
||||
return time.Time{}, false
|
||||
}
|
||||
raw := strings.TrimSpace(parsed.Query().Get("timestamp"))
|
||||
if raw == "" {
|
||||
return time.Time{}, false
|
||||
}
|
||||
value, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || value <= 0 {
|
||||
return time.Time{}, false
|
||||
}
|
||||
if value >= 1_000_000_000_000 {
|
||||
return time.UnixMilli(value), true
|
||||
}
|
||||
return time.Unix(value, 0), true
|
||||
}
|
||||
|
||||
func rockBindLinkStillFresh(item rockBindLinkCacheItem, now time.Time) bool {
|
||||
if strings.TrimSpace(item.bindURL) == "" {
|
||||
return false
|
||||
}
|
||||
issued, ok := rockBindLinkIssuedAt(item.bindURL)
|
||||
if !ok {
|
||||
issued = item.cachedAt
|
||||
}
|
||||
if issued.IsZero() {
|
||||
return false
|
||||
}
|
||||
return now.Sub(issued) < rockBindLinkMaxAge
|
||||
}
|
||||
|
||||
func (s *RockService) rememberBindLink(tokenID uint, bindURL, qrURL string) {
|
||||
@@ -31,10 +71,12 @@ func (s *RockService) rememberBindLink(tokenID uint, bindURL, qrURL string) {
|
||||
if qrURL == "" {
|
||||
qrURL = qrCodeURL(bindURL)
|
||||
}
|
||||
now := time.Now()
|
||||
s.bindLinkCache.Store(tokenID, rockBindLinkCacheItem{
|
||||
bindURL: bindURL,
|
||||
qrURL: qrURL,
|
||||
cachedAt: time.Now(),
|
||||
bindURL: bindURL,
|
||||
qrURL: qrURL,
|
||||
cachedAt: now,
|
||||
fetchedAt: now,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -55,7 +97,10 @@ func (s *RockService) freshBindLink(ctx context.Context, tokenID uint, orderNo s
|
||||
}
|
||||
if item, ok := s.bindLinkCache.Load(token.ID); ok {
|
||||
cached := item.(rockBindLinkCacheItem)
|
||||
if cached.bindURL != "" && time.Since(cached.cachedAt) < rockBindLinkMaxAge {
|
||||
if rockBindLinkStillFresh(cached, time.Now()) {
|
||||
return cached.bindURL, cached.qrURL
|
||||
}
|
||||
if !cached.fetchedAt.IsZero() && time.Since(cached.fetchedAt) < rockBindLinkRetryGap {
|
||||
return cached.bindURL, cached.qrURL
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user