fix(semi-auto): 换绑未生效原因与 7 天冷却提示
This commit is contained in:
@@ -102,6 +102,11 @@ export type ConnectorBindingSession = {
|
||||
bindGameChannel: string
|
||||
hasExistingBinding?: boolean
|
||||
rebindRequested?: boolean
|
||||
rebindUnchanged?: boolean
|
||||
rebindCooldownDays?: number
|
||||
nextRebindAt?: string
|
||||
platformChangeBindDay?: number
|
||||
platformChangeBindTime?: number
|
||||
message: string
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
const REBIND_WINDOW_DAYS = 7
|
||||
const DAY_MS = 24 * 60 * 60 * 1000
|
||||
const BROWSER_TICKET_TTL_MS = 2 * 60 * 1000
|
||||
const MAX_SESSIONS = 500
|
||||
const NATIVE_TICKET_PREFIX = 'native-ticket-'
|
||||
@@ -73,6 +75,11 @@ type NativeBindState = {
|
||||
hasExistingBinding: boolean
|
||||
rebindRequested: boolean
|
||||
rebindScanned: boolean
|
||||
rebindUnchanged: boolean
|
||||
rebindCooldownDays: number
|
||||
nextRebindAt: string
|
||||
changeBindTime: number
|
||||
changeBindDay: number
|
||||
baselineRoleKey: string
|
||||
message: string
|
||||
}
|
||||
@@ -148,6 +155,11 @@ function defaultBindingState(): NativeBindState {
|
||||
hasExistingBinding: false,
|
||||
rebindRequested: false,
|
||||
rebindScanned: false,
|
||||
rebindUnchanged: false,
|
||||
rebindCooldownDays: 0,
|
||||
nextRebindAt: '',
|
||||
changeBindTime: 0,
|
||||
changeBindDay: 0,
|
||||
baselineRoleKey: '',
|
||||
message: '',
|
||||
}
|
||||
@@ -157,6 +169,78 @@ function iso(value: number): string {
|
||||
return new Date(value).toISOString()
|
||||
}
|
||||
|
||||
function rebindDateLabel(value: string): string {
|
||||
if (!value) return ''
|
||||
const date = new Date(value)
|
||||
if (Number.isNaN(date.getTime())) return ''
|
||||
return new Intl.DateTimeFormat('zh-CN', {
|
||||
timeZone: 'Asia/Shanghai',
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
}).format(date)
|
||||
}
|
||||
|
||||
export type RebindCooldownInput = {
|
||||
status?: number
|
||||
changeBindDay?: number
|
||||
accountData?: { changeBindTime?: number } | null
|
||||
}
|
||||
|
||||
/**
|
||||
* 虎牙活动限制:每 7 个自然日允许换绑 1 次。
|
||||
* changeBindTime 为上次绑定/换绑时间(秒级 Unix 时间戳),changeBindDay 作为兜底剩余天数。
|
||||
*/
|
||||
export function resolveRebindCooldown(
|
||||
response: RebindCooldownInput | null | undefined,
|
||||
now = Date.now(),
|
||||
): { days: number; nextRebindAt: string } {
|
||||
if (!response || (response.status !== undefined && response.status !== 200)) {
|
||||
return { days: 0, nextRebindAt: '' }
|
||||
}
|
||||
const changeBindTime = Number(response.accountData?.changeBindTime || 0)
|
||||
if (Number.isFinite(changeBindTime) && changeBindTime > 0) {
|
||||
const changedAtMs = changeBindTime > 1e12 ? changeBindTime : changeBindTime * 1000
|
||||
const nextRebindMs = changedAtMs + REBIND_WINDOW_DAYS * DAY_MS
|
||||
if (nextRebindMs > now) {
|
||||
return {
|
||||
days: Math.ceil((nextRebindMs - now) / DAY_MS),
|
||||
nextRebindAt: iso(nextRebindMs),
|
||||
}
|
||||
}
|
||||
}
|
||||
const changeBindDay = Number(response.changeBindDay || 0)
|
||||
if (Number.isFinite(changeBindDay) && changeBindDay > 0 && changeBindDay <= 30) {
|
||||
const days = Math.round(changeBindDay)
|
||||
return { days, nextRebindAt: iso(now + days * DAY_MS) }
|
||||
}
|
||||
return { days: 0, nextRebindAt: '' }
|
||||
}
|
||||
|
||||
function applyRebindMeta(
|
||||
session: NativeSession,
|
||||
response: HuyaBindResponse | null,
|
||||
now: number,
|
||||
): void {
|
||||
const cooldown = resolveRebindCooldown(response, now)
|
||||
session.binding.rebindCooldownDays = cooldown.days
|
||||
session.binding.nextRebindAt = cooldown.nextRebindAt
|
||||
session.binding.changeBindTime = Number(response?.accountData?.changeBindTime || 0)
|
||||
session.binding.changeBindDay = Number(response?.changeBindDay || 0)
|
||||
}
|
||||
|
||||
function rebindUnchangedMessage(
|
||||
roleName: string,
|
||||
cooldownDays: number,
|
||||
nextRebindAt: string,
|
||||
): string {
|
||||
const next = rebindDateLabel(nextRebindAt)
|
||||
if (cooldownDays > 0) {
|
||||
return `扫码后仍识别到原绑定角色「${roleName}」;本活动 7 天内只能换绑 1 次${next ? `,预计下次换绑:${next}` : ''}`
|
||||
}
|
||||
return `扫码后仍识别到原绑定角色「${roleName}」,请确认微信中选择的角色或重新换绑`
|
||||
}
|
||||
|
||||
function extractInt(value: unknown): number {
|
||||
const parsed = Number.parseInt(String(value ?? '').trim(), 10)
|
||||
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0
|
||||
@@ -674,10 +758,17 @@ export class HuyaNativeConnector {
|
||||
const role = parseRole(roleResponse)
|
||||
const hasExistingBinding = hasExistingHuyaBinding(roleResponse)
|
||||
session.binding.hasExistingBinding = hasExistingBinding
|
||||
applyRebindMeta(session, roleResponse, this.now())
|
||||
if (role.role && !options.forceRebind) {
|
||||
this.markRoleReady(session, role, hasExistingBinding && !session.binding.rebindRequested)
|
||||
if (session.binding.rebindRequested) {
|
||||
session.binding.message = '已识别待换绑角色,请确认换绑'
|
||||
session.binding.message = session.binding.rebindUnchanged
|
||||
? rebindUnchangedMessage(
|
||||
role.role,
|
||||
session.binding.rebindCooldownDays,
|
||||
session.binding.nextRebindAt,
|
||||
)
|
||||
: '已识别待换绑角色,请确认换绑'
|
||||
}
|
||||
return this.bindingView(session)
|
||||
}
|
||||
@@ -700,11 +791,20 @@ export class HuyaNativeConnector {
|
||||
errorCode: 'huya_livelink_param_failed',
|
||||
})
|
||||
}
|
||||
const profile = await rpc.getUserProfile({
|
||||
uid: session.uid,
|
||||
cookie: session.cookie,
|
||||
targetUid: session.uid,
|
||||
})
|
||||
let profile: Awaited<ReturnType<typeof rpc.getUserProfile>> = null
|
||||
try {
|
||||
profile = await rpc.getUserProfile({
|
||||
uid: session.uid,
|
||||
cookie: session.cookie,
|
||||
targetUid: session.uid,
|
||||
})
|
||||
} catch (error) {
|
||||
// 资料接口结构可能随虎牙升级变化,失败时回退登录会话里的昵称/头像,不能阻断换绑二维码。
|
||||
logWarn('huya/bind/profile', '获取虎牙账号资料失败,回退登录昵称', {
|
||||
uid: session.uid,
|
||||
error: errorMessage(error),
|
||||
})
|
||||
}
|
||||
const nickname = profile?.nick || profile?.passport || session.nickname
|
||||
session.nickname = nickname
|
||||
const avatar = profile?.avatar || ''
|
||||
@@ -732,6 +832,7 @@ export class HuyaNativeConnector {
|
||||
if (options.forceRebind) {
|
||||
session.binding.rebindRequested = true
|
||||
session.binding.rebindScanned = false
|
||||
session.binding.rebindUnchanged = false
|
||||
session.binding.baselineRoleKey = bindingRoleKey(role)
|
||||
}
|
||||
session.binding.status = 'qr_pending'
|
||||
@@ -790,24 +891,30 @@ export class HuyaNativeConnector {
|
||||
const role = parseRole(response)
|
||||
const hasExistingBinding = hasExistingHuyaBinding(response)
|
||||
session.binding.hasExistingBinding = hasExistingBinding
|
||||
applyRebindMeta(session, response, this.now())
|
||||
if (role.role) {
|
||||
const rebindRequested = session.binding.rebindRequested
|
||||
const roleKey = bindingRoleKey(role)
|
||||
const changedRole =
|
||||
!session.binding.baselineRoleKey || roleKey !== session.binding.baselineRoleKey
|
||||
if (rebindRequested && !changedRole) {
|
||||
if (qrCompleted) {
|
||||
session.binding.rebindRequested = false
|
||||
session.binding.rebindScanned = false
|
||||
session.binding.baselineRoleKey = ''
|
||||
if (qrCompleted || session.binding.rebindUnchanged) {
|
||||
// 扫码完成但虎牙仍返回原角色:保留 rebind 状态,明确告知未换绑及冷却原因,
|
||||
// 不能清空 rebindRequested,否则 confirmBinding 会误判为“使用已有绑定”。
|
||||
session.binding.rebindUnchanged = true
|
||||
this.markRoleReady(session, role, hasExistingBinding)
|
||||
session.binding.message = '扫码后仍识别到原绑定角色,请确认使用或重新换绑'
|
||||
session.binding.message = rebindUnchangedMessage(
|
||||
role.role,
|
||||
session.binding.rebindCooldownDays,
|
||||
session.binding.nextRebindAt,
|
||||
)
|
||||
} else {
|
||||
session.binding.status = 'scanned'
|
||||
session.binding.message = '已扫码,正在等待换绑角色生效'
|
||||
}
|
||||
return this.bindingView(session)
|
||||
}
|
||||
if (rebindRequested) session.binding.rebindUnchanged = false
|
||||
this.markRoleReady(session, role, hasExistingBinding && !rebindRequested)
|
||||
if (rebindRequested) {
|
||||
session.binding.message = '已识别换绑角色,请确认换绑'
|
||||
@@ -839,15 +946,24 @@ export class HuyaNativeConnector {
|
||||
})
|
||||
}
|
||||
const rebindRequested = session.binding.rebindRequested
|
||||
applyRebindMeta(session, response, this.now())
|
||||
if (
|
||||
rebindRequested &&
|
||||
session.binding.baselineRoleKey &&
|
||||
bindingRoleKey(role) === session.binding.baselineRoleKey
|
||||
) {
|
||||
throw createHttpError('换绑角色尚未生效,请稍后重试', {
|
||||
statusCode: 409,
|
||||
errorCode: 'huya_bind_rebind_not_ready',
|
||||
})
|
||||
const cooldownDays = session.binding.rebindCooldownDays
|
||||
const next = rebindDateLabel(session.binding.nextRebindAt)
|
||||
throw createHttpError(
|
||||
cooldownDays > 0
|
||||
? `换绑未生效:平台仍返回原绑定角色「${role.role}」;本活动 7 天内只能换绑 1 次${next ? `,预计下次换绑:${next}` : ''}`
|
||||
: `换绑未生效:平台仍返回原绑定角色「${role.role}」,请重新扫码选择正确角色`,
|
||||
{
|
||||
statusCode: 409,
|
||||
errorCode:
|
||||
cooldownDays > 0 ? 'huya_bind_rebind_cooldown' : 'huya_bind_rebind_not_ready',
|
||||
},
|
||||
)
|
||||
}
|
||||
const useExistingBinding = hasExistingHuyaBinding(response) && !rebindRequested
|
||||
if (!useExistingBinding) {
|
||||
@@ -869,6 +985,9 @@ export class HuyaNativeConnector {
|
||||
session.binding.hasExistingBinding = true
|
||||
session.binding.rebindRequested = false
|
||||
session.binding.rebindScanned = false
|
||||
session.binding.rebindUnchanged = false
|
||||
session.binding.rebindCooldownDays = 0
|
||||
session.binding.nextRebindAt = ''
|
||||
session.binding.baselineRoleKey = ''
|
||||
session.binding.message = useExistingBinding
|
||||
? '当前虎牙账号已有绑定角色,已确认直接使用'
|
||||
@@ -1156,6 +1275,11 @@ export class HuyaNativeConnector {
|
||||
bindGameChannel: binding.channel,
|
||||
hasExistingBinding: binding.hasExistingBinding,
|
||||
rebindRequested: binding.rebindRequested,
|
||||
rebindUnchanged: binding.rebindUnchanged,
|
||||
rebindCooldownDays: binding.rebindCooldownDays,
|
||||
nextRebindAt: binding.nextRebindAt,
|
||||
platformChangeBindDay: binding.changeBindDay,
|
||||
platformChangeBindTime: binding.changeBindTime,
|
||||
message: binding.message,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,6 +401,27 @@ function parseLiveLinkResponse(reader: TafReader): HuyaLiveLinkResponse {
|
||||
}
|
||||
}
|
||||
|
||||
function isProfileIntType(dataType: TafTypeValue): boolean {
|
||||
return (
|
||||
dataType === TAF_TYPE.INT8 ||
|
||||
dataType === TAF_TYPE.INT16 ||
|
||||
dataType === TAF_TYPE.INT32 ||
|
||||
dataType === TAF_TYPE.INT64 ||
|
||||
dataType === TAF_TYPE.ZERO
|
||||
)
|
||||
}
|
||||
|
||||
function readProfileString(reader: TafReader, dataType: TafTypeValue): string {
|
||||
if (dataType === TAF_TYPE.STRING1 || dataType === TAF_TYPE.STRING4) {
|
||||
return reader.readStringValue(dataType)
|
||||
}
|
||||
|
||||
if (dataType === TAF_TYPE.ZERO) return ''
|
||||
|
||||
reader.skipValue(dataType)
|
||||
return ''
|
||||
}
|
||||
|
||||
function parseProfile(reader: TafReader): HuyaUserProfile {
|
||||
let uid = 0
|
||||
let nick = ''
|
||||
@@ -412,10 +433,20 @@ function parseProfile(reader: TafReader): HuyaUserProfile {
|
||||
break
|
||||
}
|
||||
reader.readHead()
|
||||
if (tag === 0) uid = reader.readIntValue(dataType)
|
||||
else if (tag === 1) nick = reader.readStringValue(dataType)
|
||||
else if (tag === 2) avatar = reader.readStringValue(dataType)
|
||||
else if (tag === 17) passport = reader.readStringValue(dataType)
|
||||
if (dataType === TAF_TYPE.STRUCT_BEGIN) {
|
||||
// 新版虎牙把资料包在 tag0 的嵌套结构里,直接解析内层结构。
|
||||
if (tag === 0) {
|
||||
return reader.readStructBody((nested) => parseProfile(nested))
|
||||
}
|
||||
reader.skipValue(dataType)
|
||||
continue
|
||||
}
|
||||
if (tag === 0) {
|
||||
if (isProfileIntType(dataType)) uid = reader.readIntValue(dataType)
|
||||
else reader.skipValue(dataType)
|
||||
} else if (tag === 1) nick = readProfileString(reader, dataType)
|
||||
else if (tag === 2) avatar = readProfileString(reader, dataType)
|
||||
else if (tag === 17) passport = readProfileString(reader, dataType)
|
||||
else reader.skipValue(dataType)
|
||||
}
|
||||
return { uid, nick, avatar, passport }
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import { resolveRebindCooldown } from './huya-native-connector.js'
|
||||
|
||||
// 2026-11-16 12:12:24 +08:00,对应任务 34 的初次绑定时间。
|
||||
const NOW = Date.UTC(2026, 10, 16, 4, 12, 24)
|
||||
const DAY_MS = 24 * 60 * 60 * 1000
|
||||
|
||||
test('换绑冷却:changeBindTime 为初次绑定时间时,下次换绑为 7 天后', () => {
|
||||
const result = resolveRebindCooldown(
|
||||
{
|
||||
status: 200,
|
||||
changeBindDay: 0,
|
||||
accountData: { changeBindTime: Math.floor(NOW / 1000) },
|
||||
},
|
||||
NOW,
|
||||
)
|
||||
assert.equal(result.days, 7)
|
||||
assert.equal(result.nextRebindAt.slice(0, 10), '2026-11-23')
|
||||
})
|
||||
|
||||
test('换绑冷却:changeBindTime 已超过 7 天时不再限制', () => {
|
||||
const result = resolveRebindCooldown(
|
||||
{
|
||||
status: 200,
|
||||
changeBindDay: 0,
|
||||
accountData: { changeBindTime: Math.floor((NOW - 8 * DAY_MS) / 1000) },
|
||||
},
|
||||
NOW,
|
||||
)
|
||||
assert.equal(result.days, 0)
|
||||
assert.equal(result.nextRebindAt, '')
|
||||
})
|
||||
|
||||
test('换绑冷却:changeBindTime 缺失时用 changeBindDay 兜底', () => {
|
||||
const result = resolveRebindCooldown({ status: 200, changeBindDay: 3, accountData: {} }, NOW)
|
||||
assert.equal(result.days, 3)
|
||||
assert.equal(result.nextRebindAt.slice(0, 10), '2026-11-19')
|
||||
})
|
||||
|
||||
test('换绑冷却:字段为空时不限制', () => {
|
||||
const result = resolveRebindCooldown({ status: 200, changeBindDay: 0, accountData: {} }, NOW)
|
||||
assert.deepEqual(result, { days: 0, nextRebindAt: '' })
|
||||
})
|
||||
@@ -2,11 +2,16 @@ import assert from 'node:assert/strict'
|
||||
import { describe, it } from 'node:test'
|
||||
|
||||
import { resolveBindingRoleId } from './binding-role-id.js'
|
||||
import { compareBindingRole, normalizeCustomerProfile } from './semi-auto-task-service.js'
|
||||
import {
|
||||
compareBindingRole,
|
||||
detectServerZone,
|
||||
normalizeCustomerProfile,
|
||||
} from './semi-auto-task-service.js'
|
||||
|
||||
const task = (id: string, name: string, huyaUid = '') => ({
|
||||
const task = (id: string, name: string, huyaUid = '', zone = '') => ({
|
||||
customer_game_id: id,
|
||||
customer_game_role: name,
|
||||
customer_game_server_zone: zone,
|
||||
huya_uid: huyaUid,
|
||||
})
|
||||
|
||||
@@ -53,7 +58,75 @@ describe('待确认角色 UID 取数(huyaUid)', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('区服解析', () => {
|
||||
it('安卓 + 微信识别为安卓V区', () => {
|
||||
assert.equal(detectServerZone('安卓(android) · 微信'), '安卓V区')
|
||||
assert.equal(detectServerZone('微信用户 · 安卓(android) · 微信'), '安卓V区')
|
||||
})
|
||||
|
||||
it('苹果 + QQ识别为苹果Q区', () => {
|
||||
assert.equal(detectServerZone('苹果(iOS) · QQ'), '苹果Q区')
|
||||
})
|
||||
|
||||
it('无法识别时返回空串', () => {
|
||||
assert.equal(detectServerZone(''), '')
|
||||
assert.equal(detectServerZone('微信用户'), '')
|
||||
})
|
||||
})
|
||||
|
||||
describe('客户填写角色与平台识别角色比对', () => {
|
||||
it('区服、角色名称、角色 ID 都一致时允许确认', () => {
|
||||
const result = compareBindingRole(
|
||||
task('1199608910400', '大鲨鱼伊塞53', '1199608910400', '安卓V区'),
|
||||
{
|
||||
bindGameRole: '大鲨鱼伊塞53',
|
||||
bindGameRoleId: '1199608910400',
|
||||
bindGameChannel: '安卓(android) · 微信',
|
||||
},
|
||||
)
|
||||
assert.equal(result.verdict, 'match')
|
||||
assert.equal(result.customerZone, '安卓V区')
|
||||
assert.equal(result.boundZone, '安卓V区')
|
||||
assert.match(result.message, /区服/)
|
||||
})
|
||||
|
||||
it('角色 ID 一致但角色名称不一致时禁止确认', () => {
|
||||
const result = compareBindingRole(task('1199608910400', '123', '1199608910400', '安卓V区'), {
|
||||
bindGameRole: '大鲨鱼伊塞53',
|
||||
bindGameRoleId: '1199608910400',
|
||||
bindGameChannel: '安卓(android) · 微信',
|
||||
})
|
||||
assert.equal(result.verdict, 'mismatch')
|
||||
assert.match(result.message, /名称/)
|
||||
})
|
||||
|
||||
it('角色名称与 ID 一致但区服不一致时禁止确认', () => {
|
||||
const result = compareBindingRole(
|
||||
task('1199608910400', '大鲨鱼伊塞53', '1199608910400', '苹果Q区'),
|
||||
{
|
||||
bindGameRole: '大鲨鱼伊塞53',
|
||||
bindGameRoleId: '1199608910400',
|
||||
bindGameChannel: '安卓(android) · 微信',
|
||||
},
|
||||
)
|
||||
assert.equal(result.verdict, 'mismatch')
|
||||
assert.match(result.message, /区服/)
|
||||
assert.equal(result.boundZone, '安卓V区')
|
||||
})
|
||||
|
||||
it('渠道解析不出区服时跳过区服比对', () => {
|
||||
const result = compareBindingRole(
|
||||
task('1199608910400', '大鲨鱼伊塞53', '1199608910400', '苹果Q区'),
|
||||
{
|
||||
bindGameRole: '大鲨鱼伊塞53',
|
||||
bindGameRoleId: '1199608910400',
|
||||
bindGameChannel: '微信用户',
|
||||
},
|
||||
)
|
||||
assert.equal(result.verdict, 'match')
|
||||
assert.equal(result.boundZone, '')
|
||||
})
|
||||
|
||||
it('两侧角色 ID 一致时允许确认', () => {
|
||||
const result = compareBindingRole(task('1192083858', '巫师基尔斯84'), {
|
||||
bindGameRole: '巫师基尔斯84',
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
import {
|
||||
createHuyaBrowserTicket,
|
||||
createHuyaLoginSession,
|
||||
confirmHuyaBinding,
|
||||
exportHuyaSessionCredential,
|
||||
getHuyaBinding,
|
||||
getHuyaLoginSession,
|
||||
@@ -954,6 +955,11 @@ function mapBindingUpdate(session: ConnectorBindingSession) {
|
||||
bindGameChannel: String(session.bindGameChannel || ''),
|
||||
hasExistingBinding: session.hasExistingBinding === true,
|
||||
rebindRequested: session.rebindRequested === true,
|
||||
rebindUnchanged: session.rebindUnchanged === true,
|
||||
rebindCooldownDays: Number(session.rebindCooldownDays || 0),
|
||||
nextRebindAt: String(session.nextRebindAt || ''),
|
||||
platformChangeBindDay: Number(session.platformChangeBindDay || 0),
|
||||
platformChangeBindTime: Number(session.platformChangeBindTime || 0),
|
||||
message: String(session.message || ''),
|
||||
}
|
||||
}
|
||||
@@ -962,8 +968,10 @@ export type BindingRoleComparison = {
|
||||
verdict: 'match' | 'mismatch' | 'unknown'
|
||||
customerRoleName: string
|
||||
customerRoleId: string
|
||||
customerZone: string
|
||||
boundRoleName: string
|
||||
boundRoleId: string
|
||||
boundZone: string
|
||||
message: string
|
||||
}
|
||||
|
||||
@@ -973,18 +981,53 @@ function normalizeRoleText(value: unknown): string {
|
||||
.toLowerCase()
|
||||
}
|
||||
|
||||
function normalizeServerZone(value: unknown): string {
|
||||
const text = String(value ?? '').trim()
|
||||
|
||||
if (!text) return ''
|
||||
|
||||
return /区$/.test(text) ? text : `${text}区`
|
||||
}
|
||||
|
||||
/**
|
||||
* 从虎牙渠道串解析区服,例如「安卓(android) · 微信」→ 安卓V区。
|
||||
* 解析不出平台或登录方式时返回空串,调用方跳过区服比对。
|
||||
*/
|
||||
export function detectServerZone(channel: unknown): string {
|
||||
const text = String(channel ?? '')
|
||||
.replace(/\s+/g, '')
|
||||
.toLowerCase()
|
||||
|
||||
if (!text) return ''
|
||||
|
||||
const platform = /苹果|ios/.test(text) ? '苹果' : /安卓|android/.test(text) ? '安卓' : ''
|
||||
|
||||
if (!platform) return ''
|
||||
|
||||
const login = /qq|q区/.test(text) ? 'Q' : /微信|wechat|v区/.test(text) ? 'V' : ''
|
||||
|
||||
return login ? `${platform}${login}区` : ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户填写角色与平台识别角色必须一致才允许确认绑定。
|
||||
*
|
||||
* 两侧都有角色 ID 时以 ID 为准(截图里的“两侧角色 ID 不一致”);
|
||||
* 任一侧缺 ID 时退回角色名比对;两边都取不到才判 unknown,转客服核对。
|
||||
* 区服、角色名、角色 ID 三项都要一致:
|
||||
* ① 两侧都有 ID 时 ID 必须相等;
|
||||
* ② 两侧都有角色名时名称必须相等(不能因为 ID 相同就放行改名);
|
||||
* ③ 平台渠道能解析出区服且客户有填时,区服必须相等。
|
||||
* 任一侧缺某字段时跳过该字段比对;三项都取不到才判 unknown,转客服核对。
|
||||
*/
|
||||
export function compareBindingRole(
|
||||
task: Pick<TaskRow, 'customer_game_id' | 'customer_game_role' | 'huya_uid'>,
|
||||
binding: { bindGameRole?: string; bindGameRoleId?: string },
|
||||
task: Pick<
|
||||
TaskRow,
|
||||
'customer_game_id' | 'customer_game_role' | 'customer_game_server_zone' | 'huya_uid'
|
||||
>,
|
||||
binding: { bindGameRole?: string; bindGameRoleId?: string; bindGameChannel?: string },
|
||||
): BindingRoleComparison {
|
||||
const customerRoleName = String(task.customer_game_role || '').trim()
|
||||
const customerRoleId = String(task.customer_game_id || '').trim()
|
||||
const customerZone = normalizeServerZone(task.customer_game_server_zone)
|
||||
const boundRoleName = String(binding.bindGameRole || '').trim()
|
||||
const rawBoundRoleId = String(binding.bindGameRoleId || '').trim()
|
||||
// 虎牙 gameRole.roleId 是 40 位加密串,客户填写的是数值游戏编号;
|
||||
@@ -995,17 +1038,46 @@ export function compareBindingRole(
|
||||
const boundRoleId = /^\d+$/.test(rawBoundRoleId)
|
||||
? rawBoundRoleId
|
||||
: numericHuyaUid || rawBoundRoleId
|
||||
const base = { customerRoleName, customerRoleId, boundRoleName, boundRoleId }
|
||||
if (customerRoleId && boundRoleId) {
|
||||
return customerRoleId === boundRoleId
|
||||
? { ...base, verdict: 'match', message: '两侧角色 ID 一致,可确认绑定' }
|
||||
: { ...base, verdict: 'mismatch', message: '两侧角色 ID 不一致,无法确认绑定' }
|
||||
const boundZone = detectServerZone(binding.bindGameChannel)
|
||||
const base = {
|
||||
customerRoleName,
|
||||
customerRoleId,
|
||||
customerZone,
|
||||
boundRoleName,
|
||||
boundRoleId,
|
||||
boundZone,
|
||||
}
|
||||
if (customerRoleName && boundRoleName) {
|
||||
return normalizeRoleText(customerRoleName) === normalizeRoleText(boundRoleName)
|
||||
? { ...base, verdict: 'match', message: '两侧角色名称一致,可确认绑定' }
|
||||
: { ...base, verdict: 'mismatch', message: '两侧角色名称不一致,无法确认绑定' }
|
||||
|
||||
if (customerRoleId && boundRoleId && customerRoleId !== boundRoleId) {
|
||||
return { ...base, verdict: 'mismatch', message: '两侧角色 ID 不一致,无法确认绑定' }
|
||||
}
|
||||
|
||||
if (
|
||||
customerRoleName &&
|
||||
boundRoleName &&
|
||||
normalizeRoleText(customerRoleName) !== normalizeRoleText(boundRoleName)
|
||||
) {
|
||||
return { ...base, verdict: 'mismatch', message: '两侧角色名称不一致,无法确认绑定' }
|
||||
}
|
||||
|
||||
if (customerZone && boundZone && customerZone !== boundZone) {
|
||||
return { ...base, verdict: 'mismatch', message: '两侧区服不一致,无法确认绑定' }
|
||||
}
|
||||
|
||||
const matchedFields = [
|
||||
customerRoleId && boundRoleId ? '角色 ID' : '',
|
||||
customerRoleName && boundRoleName ? '角色名称' : '',
|
||||
customerZone && boundZone ? '区服' : '',
|
||||
].filter(Boolean)
|
||||
|
||||
if (matchedFields.length > 0) {
|
||||
return {
|
||||
...base,
|
||||
verdict: 'match',
|
||||
message: `两侧${matchedFields.join('、')}一致,可确认绑定`,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...base,
|
||||
verdict: 'unknown',
|
||||
@@ -1065,6 +1137,12 @@ async function updateBindingTask(
|
||||
gameRoleId: mapped.bindGameRoleId,
|
||||
gameChannel: mapped.bindGameChannel,
|
||||
verdict: comparison.verdict,
|
||||
rebindRequested: mapped.rebindRequested,
|
||||
rebindUnchanged: mapped.rebindUnchanged,
|
||||
rebindCooldownDays: mapped.rebindCooldownDays,
|
||||
nextRebindAt: mapped.nextRebindAt,
|
||||
changeBindDay: mapped.platformChangeBindDay,
|
||||
changeBindTime: mapped.platformChangeBindTime,
|
||||
message: mapped.message,
|
||||
},
|
||||
})
|
||||
@@ -1080,6 +1158,11 @@ async function updateBindingTask(
|
||||
bindGameChannel: mapped.bindGameChannel,
|
||||
hasExistingBinding: mapped.hasExistingBinding,
|
||||
rebindRequested: mapped.rebindRequested,
|
||||
rebindUnchanged: mapped.rebindUnchanged,
|
||||
rebindCooldownDays: mapped.rebindCooldownDays,
|
||||
nextRebindAt: mapped.nextRebindAt,
|
||||
platformChangeBindDay: mapped.platformChangeBindDay,
|
||||
platformChangeBindTime: mapped.platformChangeBindTime,
|
||||
message: mapped.message,
|
||||
comparison,
|
||||
}
|
||||
@@ -1271,6 +1354,15 @@ export async function confirmPublicBinding(orderNoInput: unknown, taskIdInput: u
|
||||
})
|
||||
}
|
||||
const session = await getBindingForTask(task, task.connector_session_id)
|
||||
if (task.platform === 'huya' && session.rebindUnchanged === true) {
|
||||
throw createHttpError(session.message || '换绑未生效:平台仍返回原绑定角色,请重新扫码', {
|
||||
statusCode: 409,
|
||||
errorCode:
|
||||
Number(session.rebindCooldownDays || 0) > 0
|
||||
? 'semi_auto_binding_rebind_cooldown'
|
||||
: 'semi_auto_binding_rebind_unchanged',
|
||||
})
|
||||
}
|
||||
const comparison = compareBindingRole(task, session)
|
||||
if (comparison.verdict === 'mismatch') {
|
||||
throw createHttpError(comparison.message, {
|
||||
@@ -1278,7 +1370,12 @@ export async function confirmPublicBinding(orderNoInput: unknown, taskIdInput: u
|
||||
errorCode: 'semi_auto_binding_role_mismatch',
|
||||
})
|
||||
}
|
||||
const binding = await updateBindingTask(task, session, 'customer')
|
||||
// 必须调用 Connector 完成确认,否则会话仍是 role_ready,数据库也会停在待确认。
|
||||
const confirmedSession =
|
||||
task.platform === 'huya'
|
||||
? await confirmHuyaBinding(task.connector_session_id, task.connector_mode)
|
||||
: session
|
||||
const binding = await updateBindingTask(task, confirmedSession, 'customer')
|
||||
const updated = await loadTask(task.id)
|
||||
return { task: mapTask(updated), binding }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user