fix(rock): 按链接签名时间提前刷新二维码,扫码查询改为 5 秒

腾讯活动链接大约 10 分钟后签名过期。从链接 timestamp 起满 8 分钟就换新码,避免买家扫到失效二维码。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yml2213
2026-10-07 09:49:26 +08:00
co-authored by Cursor
parent 8cc5d42d53
commit fcbd7715c7
3 changed files with 78 additions and 15 deletions
+24 -6
View File
@@ -867,12 +867,28 @@ func TestRockService_CorrectMismatchUIDKeepsAssignment(t *testing.T) {
}
}
func TestRockBindLinkStillFreshUsesURLTimestamp(t *testing.T) {
now := time.Now()
stale := fmt.Sprintf("https://gp.qq.com/cp/a20241128mobile/index.html?timestamp=%d", now.Add(-rockBindLinkMaxAge-time.Second).Unix())
fresh := fmt.Sprintf("https://gp.qq.com/cp/a20241128mobile/index.html?timestamp=%d", now.Add(-time.Minute).Unix())
if rockBindLinkStillFresh(rockBindLinkCacheItem{bindURL: stale, cachedAt: now}, now) {
t.Fatal("签名已超过刷新点时,不能因为刚写入缓存就继续展示")
}
if !rockBindLinkStillFresh(rockBindLinkCacheItem{bindURL: fresh, cachedAt: now.Add(-time.Hour)}, now) {
t.Fatal("签名仍在刷新点内就应该继续展示")
}
}
func TestRockProvider_CheckBindRefreshesSameTokenLink(t *testing.T) {
db := newRockTestDB(t)
var mu sync.Mutex
redeemHits := 0
currentURL := "https://gp.qq.com/cp/a20241128mobile/index.html?timestamp=1&nonce=old&sign=old"
staleTS := time.Now().Add(-rockBindLinkMaxAge - time.Second).Unix()
freshTS := time.Now().Unix()
oldURL := fmt.Sprintf("https://gp.qq.com/cp/a20241128mobile/index.html?timestamp=%d&nonce=old&sign=old", staleTS)
newURL := fmt.Sprintf("https://gp.qq.com/cp/a20241128mobile/index.html?timestamp=%d&nonce=new&sign=new", freshTS)
currentURL := oldURL
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if strings.HasPrefix(r.URL.Path, "/api/redeem/") {
@@ -915,13 +931,15 @@ func TestRockProvider_CheckBindRefreshesSameTokenLink(t *testing.T) {
ResultData: `{"expected_uid":"3763064651","provider":"rock"}`,
}
// 缓存刚写入,但链接上的签名时间已经超过刷新点,必须换新码。
rockSvc.bindLinkCache.Store(token.ID, rockBindLinkCacheItem{
bindURL: "https://gp.qq.com/cp/a20241128mobile/index.html?timestamp=1&nonce=old&sign=old",
qrURL: "https://example.test/old.png",
cachedAt: time.Now().Add(-rockBindLinkMaxAge - time.Second),
bindURL: oldURL,
qrURL: "https://example.test/old.png",
cachedAt: time.Now(),
fetchedAt: time.Now().Add(-rockBindLinkRetryGap - time.Second),
})
mu.Lock()
currentURL = "https://gp.qq.com/cp/a20241128mobile/index.html?timestamp=2&nonce=new&sign=new"
currentURL = newURL
mu.Unlock()
check, err := provider.CheckBind(context.Background(), order, fmt.Sprintf("%d", token.ID))
@@ -956,7 +974,7 @@ func TestRockProvider_CheckBindRefreshesSameTokenLink(t *testing.T) {
if err != nil {
t.Fatalf("cached check: %v", err)
}
if checkCached.BindURL != "https://gp.qq.com/cp/a20241128mobile/index.html?timestamp=2&nonce=new&sign=new" {
if checkCached.BindURL != newURL {
t.Fatalf("有效期内应复用刚取到的链接,实际: %s", checkCached.BindURL)
}
mu.Lock()