feat(半自动履约): 补齐微博游戏角色绑定流程

This commit is contained in:
yml
2026-11-18 15:45:13 +08:00
parent f76b9c452d
commit 623a4ffa05
21 changed files with 1366 additions and 156 deletions
@@ -0,0 +1,61 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { buildServerZone, compareBindingIdentity, resolveServerZone } from './binding-identity.js'
import { getAccountBindingPolicy } from './platform-policy.js'
test('平台能力声明统一描述角色查询、确认与换绑方式', () => {
const huya = getAccountBindingPolicy('huya')
assert.equal(huya.bindingKind, 'game_role')
assert.equal(huya.queryOwner, 'order_site')
assert.equal(huya.confirmationMode, 'customer_match')
assert.equal(huya.rebindMode, 'platform_confirm')
const douyu = getAccountBindingPolicy('douyu')
assert.equal(douyu.bindingKind, 'game_role')
assert.equal(douyu.queryOwner, 'live_hub')
assert.deepEqual(douyu.requiredFields, ['roleName', 'serverZone'])
const weibo = getAccountBindingPolicy('weibo')
assert.equal(weibo.bindingKind, 'game_role')
assert.equal(weibo.bindingRequired, true)
assert.equal(weibo.requiresCustomerProfile, false)
assert.equal(weibo.confirmationMode, 'platform_verified')
assert.equal(weibo.rebindMode, 'rescan_qr')
assert.deepEqual(weibo.requiredFields, ['roleName'])
})
test('区服规范化为同一口径', () => {
assert.equal(resolveServerZone('苹果(iOS) · QQ'), '苹果Q区')
assert.equal(resolveServerZone('微信 安卓(android)'), '安卓V区')
assert.equal(buildServerZone('ios', 'wechat'), '苹果V区')
assert.equal(buildServerZone('android', 'qq'), '安卓Q区')
})
test('公共身份比对明确区分 match、mismatch 与 unknown', () => {
const fields = ['roleName', 'roleId', 'serverZone'] as const
assert.equal(
compareBindingIdentity({
requiredFields: fields,
customer: { roleName: '角色 A', roleId: '1001', serverZone: '安卓Q区' },
platform: { roleName: '角色A', roleId: '1001', serverZone: '安卓Q区' },
}).verdict,
'match',
)
assert.equal(
compareBindingIdentity({
requiredFields: fields,
customer: { roleName: '角色A', roleId: '1001', serverZone: '安卓Q区' },
platform: { roleName: '角色B', roleId: '1001', serverZone: '安卓Q区' },
}).verdict,
'mismatch',
)
assert.equal(
compareBindingIdentity({
requiredFields: fields,
customer: { roleName: '角色A', roleId: '', serverZone: '安卓Q区' },
platform: { roleName: '角色A', roleId: '1001', serverZone: '安卓Q区' },
}).verdict,
'unknown',
)
})
@@ -0,0 +1,68 @@
import type { AccountBindingField } from './platform-policy.js'
export type BindingFieldValues = Partial<Record<AccountBindingField, string>>
export type BindingIdentityComparison = {
verdict: 'match' | 'mismatch' | 'unknown'
missing: AccountBindingField[]
mismatched: AccountBindingField[]
customer: BindingFieldValues
platform: BindingFieldValues
}
export function normalizeBindingRoleName(value: unknown) {
return String(value ?? '')
.replace(/\s+/g, '')
.toLowerCase()
}
export function normalizeBindingField(field: AccountBindingField, value: unknown): string {
const text = String(value ?? '').trim()
if (field === 'roleName') return normalizeBindingRoleName(text)
return text.toLowerCase()
}
export function resolveServerZone(input: unknown) {
const text = String(input ?? '')
.replace(/\s+/g, '')
.toLowerCase()
if (!text) return ''
const system = /苹果|ios/.test(text) ? '苹果' : /安卓|android/.test(text) ? '安卓' : ''
const login = /qq|q区|手q/.test(text) ? 'Q' : /微信|wechat|v区/.test(text) ? 'V' : ''
return system && login ? `${system}${login}区` : ''
}
export function buildServerZone(system: unknown, loginChannel: unknown) {
const systemText = String(system ?? '')
.trim()
.toLowerCase()
const loginText = String(loginChannel ?? '')
.trim()
.toLowerCase()
return resolveServerZone(`${systemText} ${loginText}`)
}
export function compareBindingIdentity(input: {
requiredFields: readonly AccountBindingField[]
customer: BindingFieldValues
platform: BindingFieldValues
}): BindingIdentityComparison {
const missing: AccountBindingField[] = []
const mismatched: AccountBindingField[] = []
for (const field of input.requiredFields) {
const customer = normalizeBindingField(field, input.customer[field])
const platform = normalizeBindingField(field, input.platform[field])
if (!customer || !platform) {
missing.push(field)
continue
}
if (customer !== platform) mismatched.push(field)
}
return {
verdict: missing.length > 0 ? 'unknown' : mismatched.length > 0 ? 'mismatch' : 'match',
missing,
mismatched,
customer: input.customer,
platform: input.platform,
}
}
@@ -0,0 +1,66 @@
export type AccountBindingPlatform = 'huya' | 'douyu' | 'weibo'
export type AccountBindingQueryOwner = 'order_site' | 'live_hub'
export type AccountBindingKind = 'game_role' | 'account_authorization'
export type AccountBindingField = 'roleName' | 'roleId' | 'serverZone' | 'loginChannel'
export type AccountBindingConfirmationMode = 'customer_match' | 'platform_verified'
export type AccountBindingRebindMode = 'platform_confirm' | 'rescan_qr'
export type AccountBindingPolicy = {
platform: AccountBindingPlatform
platformLabel: string
bindingKind: AccountBindingKind
bindingRequired: boolean
requiresCustomerProfile: boolean
supportsRebind: boolean
queryOwner: AccountBindingQueryOwner
requiredFields: readonly AccountBindingField[]
confirmationMode: AccountBindingConfirmationMode
rebindMode: AccountBindingRebindMode
defaultTargetUrl: string
}
const POLICIES: Record<AccountBindingPlatform, AccountBindingPolicy> = {
huya: {
platform: 'huya',
platformLabel: '虎牙',
bindingKind: 'game_role',
bindingRequired: true,
requiresCustomerProfile: true,
supportsRebind: true,
queryOwner: 'order_site',
requiredFields: ['roleName', 'roleId', 'serverZone'],
confirmationMode: 'customer_match',
rebindMode: 'platform_confirm',
defaultTargetUrl: 'https://www.huya.com/',
},
douyu: {
platform: 'douyu',
platformLabel: '斗鱼',
bindingKind: 'game_role',
bindingRequired: true,
requiresCustomerProfile: true,
supportsRebind: true,
queryOwner: 'live_hub',
requiredFields: ['roleName', 'serverZone'],
confirmationMode: 'customer_match',
rebindMode: 'platform_confirm',
defaultTargetUrl: 'https://www.douyu.com/',
},
weibo: {
platform: 'weibo',
platformLabel: '微博',
bindingKind: 'game_role',
bindingRequired: true,
requiresCustomerProfile: false,
supportsRebind: true,
queryOwner: 'order_site',
requiredFields: ['roleName'],
confirmationMode: 'platform_verified',
rebindMode: 'rescan_qr',
defaultTargetUrl: 'https://weibo.com/',
},
}
export function getAccountBindingPolicy(platform: AccountBindingPlatform) {
return POLICIES[platform]
}