修复(半自动履约): 收口虎牙绑定为单一状态机
This commit is contained in:
@@ -17,6 +17,7 @@ import {
|
||||
consumeHuyaBrowserTicket,
|
||||
createHuyaBrowserTicket,
|
||||
revokeHuyaSession,
|
||||
type ConnectorBindingSession,
|
||||
type ConnectorInjectionPayload,
|
||||
type SemiAutoConnectorMode,
|
||||
} from './huya-connector-client.js'
|
||||
@@ -35,21 +36,23 @@ import { isHuyaRoleToken } from './binding-role-id.js'
|
||||
import { getAccountBindingPolicy } from '../account-binding/platform-policy.js'
|
||||
import {
|
||||
appendTaskEvent,
|
||||
compareBindingRole,
|
||||
ensureConnectorSessionForTask,
|
||||
ensureSemiAutoTasksForAdminList,
|
||||
getBindingForTask,
|
||||
getLoginSessionForTask,
|
||||
isTaskBindingConfirmed,
|
||||
loadTask,
|
||||
loadTaskForUpdateWithClient,
|
||||
mapTask,
|
||||
normalizeId,
|
||||
normalizeOrderNo,
|
||||
publishSemiAutoTaskRealtimeChange,
|
||||
resolveTaskHuyaBindingDecision,
|
||||
resetTaskForCustomerLogin,
|
||||
revokeSessionForTask,
|
||||
TASK_SELECT,
|
||||
updateBindingTask,
|
||||
updateBindingTaskWithClient,
|
||||
type BindingStatus,
|
||||
type ExecutionStatus,
|
||||
type LoginStatus,
|
||||
@@ -125,6 +128,7 @@ export async function listAdminSemiAutoTasks(input: Record<string, unknown> = {}
|
||||
|
||||
export async function getAdminSemiAutoTask(taskIdInput: unknown) {
|
||||
const task = await loadTask(normalizeId(taskIdInput))
|
||||
const bindingDecision = resolveTaskHuyaBindingDecision(task)
|
||||
const events = await query<{
|
||||
id: number
|
||||
event_type: string
|
||||
@@ -143,12 +147,7 @@ export async function getAdminSemiAutoTask(taskIdInput: unknown) {
|
||||
return {
|
||||
task: mapTask(task),
|
||||
// 管理端详情直接复用与客户页相同的角色核对口径。
|
||||
comparison: compareBindingRole(task, {
|
||||
bindGameRole: task.pending_bind_game_role || task.bind_game_role,
|
||||
bindGameRoleToken: task.pending_bind_game_role_token || task.bind_game_role_token,
|
||||
bindGameChannel: task.pending_bind_game_channel || task.bind_game_channel,
|
||||
platformGameRoleUid: task.platform_game_role_uid,
|
||||
}),
|
||||
comparison: bindingDecision?.comparison || null,
|
||||
events: events.rows.map((event) => ({
|
||||
id: Number(event.id),
|
||||
eventType: event.event_type,
|
||||
@@ -324,6 +323,14 @@ export async function syncAdminSemiAutoTask(taskIdInput: unknown) {
|
||||
await updateLoginTask(task, login, 'connector')
|
||||
task = await loadTask(taskId)
|
||||
}
|
||||
if (
|
||||
task.platform === 'huya' &&
|
||||
task.login_status === 'authenticated' &&
|
||||
!isTaskBindingConfirmed(task)
|
||||
) {
|
||||
const binding = await getBindingForTask(task, task.connector_session_id)
|
||||
await updateBindingTask(task, binding, 'connector')
|
||||
}
|
||||
return getAdminSemiAutoTask(taskId)
|
||||
}
|
||||
|
||||
@@ -408,6 +415,12 @@ const ROLE_IDENTITY_SOURCES = new Set([
|
||||
'manual',
|
||||
])
|
||||
const VERIFICATION_BASES = new Set(['', 'role_name_zone', 'platform_numeric_uid', 'manual'])
|
||||
const CONFIRM_STATES = new Set<'idle' | 'submitting' | 'accepted' | 'rejected'>([
|
||||
'idle',
|
||||
'submitting',
|
||||
'accepted',
|
||||
'rejected',
|
||||
])
|
||||
|
||||
function parseSnapshotStatus<T extends string>(value: unknown, allowed: Set<T>, label: string): T {
|
||||
const status = String(value || '').trim() as T
|
||||
@@ -420,6 +433,21 @@ function parseSnapshotStatus<T extends string>(value: unknown, allowed: Set<T>,
|
||||
return status
|
||||
}
|
||||
|
||||
function parseSnapshotBoolean(value: unknown): boolean | undefined {
|
||||
if (value === undefined || value === null || value === '') return undefined
|
||||
if (typeof value === 'boolean') return value
|
||||
if (typeof value === 'number') return value !== 0
|
||||
const text = String(value).trim().toLowerCase()
|
||||
if (['true', '1', 'yes'].includes(text)) return true
|
||||
if (['false', '0', 'no'].includes(text)) return false
|
||||
return undefined
|
||||
}
|
||||
|
||||
function parseSnapshotNumber(value: unknown): number {
|
||||
const number = Number(value)
|
||||
return Number.isFinite(number) && number > 0 ? number : 0
|
||||
}
|
||||
|
||||
/**
|
||||
* 外部 Connector 的幂等状态快照入口。
|
||||
*
|
||||
@@ -471,11 +499,6 @@ export async function applySemiAutoTaskStateSnapshot(
|
||||
return { applied: false, terminal: true }
|
||||
}
|
||||
|
||||
const executionStatus: ExecutionStatus =
|
||||
loginStatus === 'authenticated' &&
|
||||
(task.platform === 'weibo' || bindingStatus === 'confirmed')
|
||||
? 'waiting_operator'
|
||||
: 'waiting_customer'
|
||||
const uid = String(input.huyaUid ?? input.huya_uid ?? '').trim()
|
||||
const nickname = String(input.platformNickname ?? input.platform_nickname ?? '').trim()
|
||||
const bindGameAccount = String(input.bindGameAccount ?? input.bind_game_account ?? '').trim()
|
||||
@@ -548,72 +571,131 @@ export async function applySemiAutoTaskStateSnapshot(
|
||||
const message = String(input.message ?? input.lastError ?? input.last_error ?? '')
|
||||
.trim()
|
||||
.slice(0, 1000)
|
||||
const isBindAccount = parseSnapshotBoolean(input.isBindAccount ?? input.is_bind_account)
|
||||
const isBindRole = parseSnapshotBoolean(input.isBindRole ?? input.is_bind_role)
|
||||
const accountChangeBindTime = parseSnapshotNumber(
|
||||
input.accountChangeBindTime ?? input.account_change_bind_time,
|
||||
)
|
||||
const accountChangeBindDay = parseSnapshotNumber(
|
||||
input.accountChangeBindDay ?? input.account_change_bind_day,
|
||||
)
|
||||
const pendingChangeBindTime = parseSnapshotNumber(
|
||||
input.pendingChangeBindTime ??
|
||||
input.pending_bind_game_change_bind_time ??
|
||||
input.pendingBindGameChangeBindTime,
|
||||
)
|
||||
const pendingChangeBindDay = parseSnapshotNumber(
|
||||
input.pendingChangeBindDay ??
|
||||
input.pending_bind_game_change_bind_day ??
|
||||
input.pendingBindGameChangeBindDay,
|
||||
)
|
||||
const rebindConfirmState = parseSnapshotStatus(
|
||||
input.rebindConfirmState ?? input.rebind_confirm_state ?? 'idle',
|
||||
CONFIRM_STATES,
|
||||
'换绑确认状态',
|
||||
)
|
||||
const bindConfirmState = parseSnapshotStatus(
|
||||
input.bindConfirmState ?? input.bind_confirm_state ?? 'idle',
|
||||
CONFIRM_STATES,
|
||||
'绑定确认状态',
|
||||
)
|
||||
const accountFactsObserved =
|
||||
bindGameRole ||
|
||||
bindGameRoleToken ||
|
||||
platformGameRoleUid ||
|
||||
isBindAccount !== undefined ||
|
||||
isBindRole !== undefined
|
||||
const currentRole = accountFactsObserved
|
||||
? {
|
||||
isBindAccount: isBindAccount ?? Boolean(bindGameRole),
|
||||
isBindRole: isBindRole ?? Boolean(bindGameRole),
|
||||
roleName: bindGameRole,
|
||||
roleToken: platformGameRoleUid || bindGameRoleToken,
|
||||
channel: bindGameChannel,
|
||||
changeBindTime: accountChangeBindTime,
|
||||
changeBindDay: accountChangeBindDay,
|
||||
}
|
||||
: null
|
||||
const pendingRole = pendingBindGameRole
|
||||
? {
|
||||
isBindAccount: false,
|
||||
isBindRole: false,
|
||||
roleName: pendingBindGameRole,
|
||||
roleToken: pendingBindGameRoleToken,
|
||||
channel: pendingBindGameChannel,
|
||||
changeBindTime: pendingChangeBindTime,
|
||||
changeBindDay: pendingChangeBindDay,
|
||||
}
|
||||
: null
|
||||
const bindingSession: ConnectorBindingSession = {
|
||||
sessionId: task.connector_session_id,
|
||||
status: bindingStatus,
|
||||
qrImage: '',
|
||||
qrExpiresAt: '',
|
||||
qrUrl: '',
|
||||
bindGameAccount,
|
||||
bindGameRole,
|
||||
bindGameRoleId: legacyRoleId,
|
||||
bindGameRoleToken,
|
||||
platformGameRoleUid,
|
||||
bindGameChannel,
|
||||
...(accountFactsObserved && currentRole ? { currentRole } : {}),
|
||||
pendingRole,
|
||||
pendingBindGameAccount,
|
||||
pendingBindGameRole,
|
||||
pendingBindGameRoleToken,
|
||||
pendingBindGameChannel,
|
||||
pendingChangeBindTime,
|
||||
pendingChangeBindDay,
|
||||
roleIdentitySource: roleIdentitySource as NonNullable<
|
||||
ConnectorBindingSession['roleIdentitySource']
|
||||
>,
|
||||
verificationBasis: verificationBasis as NonNullable<
|
||||
ConnectorBindingSession['verificationBasis']
|
||||
>,
|
||||
...(accountFactsObserved ? { hasExistingBinding: currentRole?.isBindAccount === true } : {}),
|
||||
rebindRequested:
|
||||
parseSnapshotBoolean(input.rebindRequested ?? input.rebind_requested) === true,
|
||||
rebindUnchanged:
|
||||
parseSnapshotBoolean(input.rebindUnchanged ?? input.rebind_unchanged) === true,
|
||||
rebindConfirmState,
|
||||
rebindConfirmMessage: String(
|
||||
input.rebindConfirmMessage ?? input.rebind_confirm_message ?? '',
|
||||
).trim(),
|
||||
bindConfirmState,
|
||||
bindConfirmMessage: String(
|
||||
input.bindConfirmMessage ?? input.bind_confirm_message ?? '',
|
||||
).trim(),
|
||||
bindEffectDeadlineAt: String(
|
||||
input.bindEffectDeadlineAt ?? input.binding_effect_deadline_at ?? '',
|
||||
).trim(),
|
||||
message,
|
||||
}
|
||||
|
||||
await client.query(
|
||||
`UPDATE semi_auto_tasks
|
||||
SET login_status = $2,
|
||||
binding_status = $3,
|
||||
execution_status = $4,
|
||||
huya_uid = CASE WHEN $5 <> '' THEN $5 ELSE huya_uid END,
|
||||
platform_nickname = CASE WHEN $6 <> '' THEN $6 ELSE platform_nickname END,
|
||||
bind_game_account = CASE WHEN $7 <> '' THEN $7 ELSE bind_game_account END,
|
||||
bind_game_role = CASE WHEN $8 <> '' THEN $8 ELSE bind_game_role END,
|
||||
bind_game_role_token = CASE WHEN $9 <> '' THEN $9 ELSE bind_game_role_token END,
|
||||
bind_game_channel = CASE WHEN $10 <> '' THEN $10 ELSE bind_game_channel END,
|
||||
pending_bind_game_account = $12,
|
||||
pending_bind_game_role = $13,
|
||||
pending_bind_game_role_token = $14,
|
||||
pending_bind_game_channel = $15,
|
||||
platform_game_role_uid = $18,
|
||||
role_identity_source = CASE WHEN $16 <> '' THEN $16 ELSE role_identity_source END,
|
||||
verification_basis = CASE WHEN $17 <> '' THEN $17 ELSE verification_basis END,
|
||||
huya_uid = CASE WHEN $3 <> '' THEN $3 ELSE huya_uid END,
|
||||
platform_nickname = CASE WHEN $4 <> '' THEN $4 ELSE platform_nickname END,
|
||||
authenticated_at = CASE
|
||||
WHEN $2 = 'authenticated' THEN COALESCE(authenticated_at, NOW())
|
||||
ELSE authenticated_at
|
||||
END,
|
||||
bound_at = CASE
|
||||
WHEN $3 IN ('role_ready', 'confirmed') THEN COALESCE(bound_at, NOW())
|
||||
ELSE bound_at
|
||||
END,
|
||||
last_error = CASE WHEN $2 IN ('failed', 'expired') OR $3 IN ('failed', 'expired')
|
||||
THEN $11 ELSE '' END,
|
||||
last_error = CASE WHEN $2 IN ('failed', 'expired') THEN $5 ELSE last_error END,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1`,
|
||||
[
|
||||
task.id,
|
||||
loginStatus,
|
||||
bindingStatus,
|
||||
executionStatus,
|
||||
uid,
|
||||
nickname,
|
||||
bindGameAccount,
|
||||
bindGameRole,
|
||||
bindGameRoleToken,
|
||||
bindGameChannel,
|
||||
message,
|
||||
pendingBindGameAccount,
|
||||
pendingBindGameRole,
|
||||
pendingBindGameRoleToken,
|
||||
pendingBindGameChannel,
|
||||
roleIdentitySource,
|
||||
verificationBasis,
|
||||
platformGameRoleUid,
|
||||
],
|
||||
[task.id, loginStatus, uid, nickname, message],
|
||||
)
|
||||
if (
|
||||
loginStatus !== task.login_status ||
|
||||
bindingStatus !== task.binding_status ||
|
||||
executionStatus !== task.execution_status
|
||||
uid !== task.huya_uid ||
|
||||
nickname !== task.platform_nickname
|
||||
) {
|
||||
await appendTaskEvent(client, task.id, {
|
||||
eventType: 'connector_state_synced',
|
||||
eventType: 'connector_login_state_synced',
|
||||
actorType: 'connector',
|
||||
fromLoginStatus: task.login_status,
|
||||
toLoginStatus: loginStatus,
|
||||
fromBindingStatus: task.binding_status,
|
||||
toBindingStatus: bindingStatus,
|
||||
fromExecutionStatus: task.execution_status,
|
||||
toExecutionStatus: executionStatus,
|
||||
detail: {
|
||||
eventId,
|
||||
source: 'semi_auto_connector',
|
||||
@@ -621,11 +703,22 @@ export async function applySemiAutoTaskStateSnapshot(
|
||||
},
|
||||
})
|
||||
}
|
||||
const bindingResult = await updateBindingTaskWithClient(
|
||||
client,
|
||||
task.id,
|
||||
bindingSession,
|
||||
'connector',
|
||||
{
|
||||
eventType: 'connector_state_synced',
|
||||
eventDetail: { eventId, source: 'semi_auto_connector', message },
|
||||
},
|
||||
)
|
||||
return {
|
||||
applied: true,
|
||||
terminal: false,
|
||||
taskId: task.id,
|
||||
orderId: task.order_id,
|
||||
executionStatus: bindingResult.executionStatus,
|
||||
}
|
||||
})
|
||||
if (result.applied && result.taskId && result.orderId) {
|
||||
@@ -849,6 +942,19 @@ export async function completeAdminSemiAutoTask(
|
||||
errorCode: 'semi_auto_credential_required',
|
||||
})
|
||||
}
|
||||
const bindingDecision = resolveTaskHuyaBindingDecision(task)
|
||||
if (task.platform === 'huya' && !bindingDecision?.canCompleteTask) {
|
||||
throw createHttpError('虎牙角色绑定尚未确认,不能完成履约', {
|
||||
statusCode: 409,
|
||||
errorCode: 'huya_binding_not_confirmed',
|
||||
})
|
||||
}
|
||||
if (task.platform === 'weibo' && !isTaskBindingConfirmed(task)) {
|
||||
throw createHttpError('微博角色绑定尚未确认,不能完成履约', {
|
||||
statusCode: 409,
|
||||
errorCode: 'weibo_binding_not_confirmed',
|
||||
})
|
||||
}
|
||||
if (task.assigned_admin_user_id && Number(task.assigned_admin_user_id) !== adminUserId) {
|
||||
throw createHttpError('该任务已被其他客服领取', {
|
||||
statusCode: 409,
|
||||
|
||||
Reference in New Issue
Block a user