fix(semi-auto): 刷新绑定二维码 5 秒内复用当前码
This commit is contained in:
@@ -52,6 +52,8 @@ const LOGIN_QR_TTL_MS = 5 * 60 * 1000
|
||||
const SMS_LOGIN_TTL_MS = 5 * 60 * 1000
|
||||
const CREDENTIAL_TTL_MS = 24 * 60 * 60 * 1000
|
||||
const BINDING_QR_TTL_MS = 5 * 60 * 1000
|
||||
// 刷新绑定二维码的软限流:5 秒内复用当前有效码,避免撞上虎牙重复生成窗口。
|
||||
const BINDING_QR_REFRESH_COOLDOWN_MS = 5_000
|
||||
const REBIND_WINDOW_DAYS = 7
|
||||
const DAY_MS = 24 * 60 * 60 * 1000
|
||||
// 提交换绑后给虎牙留的生效时间;期间前端继续轮询,不提前判失败。
|
||||
@@ -188,6 +190,23 @@ function iso(value: number): string {
|
||||
return new Date(value).toISOString()
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新绑定二维码的软限流:同一张有效二维码在冷却期内直接复用,
|
||||
* 避免短时间内重复请求虎牙小程序码接口触发上游限流(iRet=-1140603)。
|
||||
*/
|
||||
export function shouldReuseBindingQr(
|
||||
binding: { status: string; qrImage: string; expiresAtMs: number },
|
||||
now: number,
|
||||
forceRebind: boolean,
|
||||
): boolean {
|
||||
if (!forceRebind) return false
|
||||
if (binding.status !== 'qr_pending') return false
|
||||
if (!binding.qrImage) return false
|
||||
if (binding.expiresAtMs <= now) return false
|
||||
const createdAtMs = binding.expiresAtMs - BINDING_QR_TTL_MS
|
||||
return now - createdAtMs < BINDING_QR_REFRESH_COOLDOWN_MS
|
||||
}
|
||||
|
||||
function rebindDateLabel(value: string): string {
|
||||
if (!value) return ''
|
||||
const date = new Date(value)
|
||||
@@ -821,6 +840,11 @@ export class HuyaNativeConnector {
|
||||
): Promise<ConnectorBindingSession> {
|
||||
return this.withLock(sessionId, async () => {
|
||||
const session = this.requireAuthenticatedSession(sessionId)
|
||||
const now = this.now()
|
||||
if (shouldReuseBindingQr(session.binding, now, options.forceRebind === true)) {
|
||||
session.binding.message = '二维码仍在有效期内,请直接扫码;如需新码请稍后再试'
|
||||
return this.bindingView(session)
|
||||
}
|
||||
const { bindActId, outerActId, roomPid } = configValues()
|
||||
const rpc = new HuyaRpcClient({ fetchImpl: this.fetchImpl, timeoutMs: this.rpcTimeoutMs })
|
||||
const roleResponse = await rpc.checkUserBindGameAccount({
|
||||
|
||||
@@ -5,7 +5,7 @@ import test from 'node:test'
|
||||
import sharp from 'sharp'
|
||||
|
||||
import { buildHuyaLoginContext, ensureHuyaWebCookieIdentity, x64hash128 } from './cookie.js'
|
||||
import { HuyaNativeConnector } from './huya-native-connector.js'
|
||||
import { HuyaNativeConnector, shouldReuseBindingQr } from './huya-native-connector.js'
|
||||
import { detectHuyaSliderDistance } from './huya-sms-role.js'
|
||||
import { hasExistingHuyaBinding, parseBindStruct } from './huya-rpc.js'
|
||||
import { TAF_TYPE, TafWriter } from './taf.js'
|
||||
@@ -744,3 +744,21 @@ test('内置 Connector 支持客户完成虎牙点选和旋转验证', async ()
|
||||
await run('click')
|
||||
await run('rotate')
|
||||
})
|
||||
|
||||
test('绑定二维码刷新 5 秒内复用当前有效码,过期/已扫码/首次绑定不复用', () => {
|
||||
const now = 1_700_000_000_000
|
||||
const ttl = 5 * 60 * 1000
|
||||
const fresh = {
|
||||
status: 'qr_pending',
|
||||
qrImage: 'data:image/png;base64,x',
|
||||
expiresAtMs: now + ttl - 4000,
|
||||
}
|
||||
// 生成 4 秒:仍在上游重复生成拒绝窗口内,直接复用
|
||||
assert.equal(shouldReuseBindingQr(fresh, now, true), true)
|
||||
// 生成 6 秒:允许重新生成
|
||||
assert.equal(shouldReuseBindingQr({ ...fresh, expiresAtMs: now + ttl - 6000 }, now, true), false)
|
||||
assert.equal(shouldReuseBindingQr({ ...fresh, expiresAtMs: now - 1 }, now, true), false)
|
||||
assert.equal(shouldReuseBindingQr({ ...fresh, status: 'scanned' }, now, true), false)
|
||||
assert.equal(shouldReuseBindingQr({ ...fresh, qrImage: '' }, now, true), false)
|
||||
assert.equal(shouldReuseBindingQr(fresh, now, false), false)
|
||||
})
|
||||
|
||||
@@ -46,6 +46,8 @@ import { runWeiboYidunChallenge } from '@/utils/weibo-yidun'
|
||||
import HuyaChallengeModal from './HuyaChallengeModal'
|
||||
|
||||
const POLL_INTERVAL_MS = 2000
|
||||
// 刷新绑定二维码的冷却时间:避免连续点击频繁请求虎牙。
|
||||
const BINDING_REFRESH_COOLDOWN_MS = 3_000
|
||||
|
||||
const SERVER_ZONE_OPTIONS = ['苹果Q区', '苹果V区', '安卓Q区', '安卓V区']
|
||||
|
||||
@@ -88,6 +90,7 @@ export default function CustomerFulfillmentPage({
|
||||
const [profileSaving, setProfileSaving] = useState(false)
|
||||
const [profileEditing, setProfileEditing] = useState(false)
|
||||
const startingBindingRef = useRef(false)
|
||||
const lastBindingRefreshAtRef = useRef(0)
|
||||
const resettingBindingRef = useRef(false)
|
||||
const hydratingBindingRef = useRef(false)
|
||||
const autoOpenedRoleRef = useRef('')
|
||||
@@ -176,6 +179,7 @@ export default function CustomerFulfillmentPage({
|
||||
setBindingInfo(null)
|
||||
setBindingConfirmOpen(false)
|
||||
startingBindingRef.current = false
|
||||
lastBindingRefreshAtRef.current = 0
|
||||
hydratingBindingRef.current = false
|
||||
autoOpenedRoleRef.current = ''
|
||||
weiboBindingSyncRef.current = false
|
||||
@@ -608,6 +612,12 @@ export default function CustomerFulfillmentPage({
|
||||
|
||||
async function handleRefreshBinding(forceRebind: boolean) {
|
||||
if (!selectedTask) return
|
||||
const now = Date.now()
|
||||
if (now - lastBindingRefreshAtRef.current < BINDING_REFRESH_COOLDOWN_MS) {
|
||||
message.warning('操作频繁,请稍后再试')
|
||||
return
|
||||
}
|
||||
lastBindingRefreshAtRef.current = now
|
||||
setBindingLoading(true)
|
||||
try {
|
||||
const response = await startCustomerBinding(orderNo, selectedTask.id, { forceRebind })
|
||||
|
||||
@@ -56,3 +56,18 @@ test('上一步修改资料会清空换绑状态并保留已填资料', () => {
|
||||
// 重置失败不阻断客户修改资料。
|
||||
assert.ok(source.includes('重置绑定状态失败,请稍后重试'))
|
||||
})
|
||||
|
||||
test('刷新二维码有 3 秒冷却并提示操作频繁', () => {
|
||||
assert.ok(source.includes('BINDING_REFRESH_COOLDOWN_MS = 3_000'))
|
||||
assert.ok(source.includes('lastBindingRefreshAtRef'))
|
||||
assert.ok(source.includes("message.warning('操作频繁,请稍后再试')"))
|
||||
// 首次二维码出现不设置冷却,只有手动刷新才计时。
|
||||
assert.ok(!source.includes('lastBindingRefreshAtRef.current = Date.now()'))
|
||||
assert.ok(source.includes('lastBindingRefreshAtRef.current = now'))
|
||||
// 冷却判断必须在真正发起 startCustomerBinding 之前。
|
||||
const guardIndex = source.indexOf('操作频繁,请稍后再试')
|
||||
const startIndex = source.indexOf(
|
||||
'await startCustomerBinding(orderNo, selectedTask.id, { forceRebind })',
|
||||
)
|
||||
assert.ok(guardIndex > 0 && startIndex > guardIndex)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user