fix(semi-auto): 刷新绑定二维码 5 秒内复用当前码

This commit is contained in:
2026-11-19 12:34:55 +08:00
parent 87c7cc267c
commit 7f74772dab
4 changed files with 68 additions and 1 deletions
@@ -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)
})