新增:半自动履约工作台与虎牙客户授权流程

- 新增 153 半自动履约迁移、后台任务接口与客户履约接口
- 接入虎牙 Connector 登录会话、绑定与一次性 CK 注入票据
- 新增后台半自动履约页面和客户履约入口
- 完善手工履约配置、成本确认事务与运行时连接器配置
- 同步权威文档、环境示例、部署脚本和迁移展示逻辑
This commit is contained in:
yml2213
2026-11-09 15:29:20 +08:00
parent 4159a435d5
commit cb9538a775
38 changed files with 3697 additions and 30 deletions
@@ -0,0 +1,425 @@
import {
CheckCircleFilled,
LoadingOutlined,
QrcodeOutlined,
SafetyCertificateOutlined,
} from '@ant-design/icons'
import {
Alert,
App,
Button,
Card,
Col,
Image,
Result,
Row,
Space,
Spin,
Steps,
Tag,
Typography,
} from 'antd'
import QRCode from 'qrcode'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useParams } from 'react-router'
import {
confirmCustomerBinding,
fetchCustomerFulfillment,
markCustomerFulfillmentOpened,
pollCustomerBinding,
pollCustomerLogin,
startCustomerBinding,
startCustomerLogin,
} from '@/services/customer-fulfillment'
import type {
CustomerBindingInfo,
CustomerLoginInfo,
CustomerSemiAutoTask,
} from '@/types/customer-fulfillment'
import { formatAdminDateTime } from '@/utils/admin-time'
const { Text, Title, Paragraph } = Typography
const POLL_INTERVAL_MS = 2000
export default function CustomerFulfillmentPage() {
const { message } = App.useApp()
const { orderNo: routeOrderNo } = useParams<{ orderNo?: string }>()
const orderNo = String(routeOrderNo || '').trim()
const [loading, setLoading] = useState(true)
const [loadError, setLoadError] = useState('')
const [tasks, setTasks] = useState<CustomerSemiAutoTask[]>([])
const [selectedTaskId, setSelectedTaskId] = useState(0)
const [loginInfo, setLoginInfo] = useState<CustomerLoginInfo | null>(null)
const [bindingInfo, setBindingInfo] = useState<CustomerBindingInfo | null>(null)
const [loginQrImage, setLoginQrImage] = useState('')
const startingRef = useRef(false)
const selectedTask = useMemo(
() => tasks.find((task) => task.id === selectedTaskId) || tasks[0] || null,
[selectedTaskId, tasks],
)
const load = useCallback(
async (silent = false) => {
if (!orderNo) {
setLoadError('订单号无效,请确认订单号是否正确')
setLoading(false)
return
}
if (!silent) setLoading(true)
try {
const response = await fetchCustomerFulfillment(orderNo)
const nextTasks = response.data || []
setTasks(nextTasks)
setLoadError('')
setSelectedTaskId((current) => {
if (nextTasks.some((task) => task.id === current)) return current
return nextTasks[0]?.id || 0
})
} catch (error) {
if (!silent) {
setTasks([])
setLoadError(error instanceof Error ? error.message : '读取订单履约信息失败')
}
} finally {
if (!silent) setLoading(false)
}
},
[orderNo],
)
useEffect(() => {
void load()
}, [load])
useEffect(() => {
if (!selectedTask) return
void markCustomerFulfillmentOpened(orderNo, selectedTask.id).catch(() => undefined)
}, [orderNo, selectedTask?.id])
useEffect(() => {
if (!loginInfo?.qrContent) {
setLoginQrImage('')
return
}
void QRCode.toDataURL(loginInfo.qrContent, {
width: 300,
margin: 2,
color: { dark: '#111827', light: '#ffffff' },
}).then(setLoginQrImage)
}, [loginInfo?.qrContent])
useEffect(() => {
if (!selectedTask || !orderNo) return
if (!['qr_pending', 'scanned'].includes(selectedTask.loginStatus)) return
const tick = async () => {
try {
const response = await pollCustomerLogin(orderNo, selectedTask.id)
if (response.data.task) {
setTasks((current) =>
current.map((task) => (task.id === response.data.task.id ? response.data.task : task)),
)
}
if (response.data.login) setLoginInfo(response.data.login)
} catch {
// 短暂轮询失败不打断客户页面,下一轮继续。
}
}
const timer = window.setInterval(() => {
void tick()
}, POLL_INTERVAL_MS)
return () => window.clearInterval(timer)
}, [orderNo, selectedTask?.id, selectedTask?.loginStatus])
useEffect(() => {
if (!selectedTask || !orderNo) return
if (
selectedTask.loginStatus !== 'authenticated' ||
selectedTask.bindingStatus === 'confirmed'
) {
return
}
if (selectedTask.bindingStatus === 'not_started' && !startingRef.current) {
startingRef.current = true
void startCustomerBinding(orderNo, selectedTask.id)
.then((response) => {
setTasks((current) =>
current.map((task) => (task.id === response.data.task.id ? response.data.task : task)),
)
setBindingInfo(response.data.binding)
})
.catch((error) => {
message.error(error instanceof Error ? error.message : '创建绑定二维码失败')
})
.finally(() => {
startingRef.current = false
})
return
}
if (!['qr_pending', 'scanned'].includes(selectedTask.bindingStatus)) return
const tick = async () => {
try {
const response = await pollCustomerBinding(orderNo, selectedTask.id)
if (response.data.task) {
setTasks((current) =>
current.map((task) => (task.id === response.data.task.id ? response.data.task : task)),
)
}
if (response.data.binding) setBindingInfo(response.data.binding)
} catch {
// 同上,下一轮继续。
}
}
const timer = window.setInterval(() => {
void tick()
}, POLL_INTERVAL_MS)
return () => window.clearInterval(timer)
}, [message, orderNo, selectedTask?.bindingStatus, selectedTask?.id, selectedTask?.loginStatus])
async function handleStartLogin() {
if (!selectedTask) return
setLoading(true)
try {
const response = await startCustomerLogin(orderNo, selectedTask.id)
setTasks((current) =>
current.map((task) => (task.id === response.data.task.id ? response.data.task : task)),
)
setLoginInfo(response.data.login)
setBindingInfo(null)
} catch (error) {
message.error(error instanceof Error ? error.message : '创建登录二维码失败')
} finally {
setLoading(false)
}
}
async function handleConfirmBinding() {
if (!selectedTask) return
try {
const response = await confirmCustomerBinding(orderNo, selectedTask.id)
setTasks((current) =>
current.map((task) => (task.id === response.data.task.id ? response.data.task : task)),
)
setBindingInfo(response.data.binding)
message.success('绑定已确认,等待客服处理')
} catch (error) {
message.error(error instanceof Error ? error.message : '确认绑定失败')
}
}
if (loading) {
return (
<div className="customer-fulfillment-page customer-fulfillment-centered">
<Spin indicator={<LoadingOutlined spin />} tip="正在读取订单履约信息..." />
</div>
)
}
if (loadError || tasks.length === 0) {
return (
<div className="customer-fulfillment-page customer-fulfillment-centered">
<Result
status="warning"
title="暂时无法打开订单"
subTitle={loadError || '该订单没有需要客户授权或绑定的半自动履约任务。'}
/>
</div>
)
}
if (selectedTask?.executionStatus === 'completed') {
return (
<div className="customer-fulfillment-page">
<Result
status="success"
icon={<CheckCircleFilled />}
title="履约已完成"
subTitle={
selectedTask.completionNote
? selectedTask.completionNote
: '客服已完成本订单的充值或赠送操作,请到平台账号中确认。'
}
extra={
selectedTask.completedAt
? `完成时间:${formatAdminDateTime(selectedTask.completedAt)}`
: undefined
}
/>
</div>
)
}
const config = selectedTask?.semiAutoConfig || {}
const title = config.pageTitle || '账号授权与角色绑定'
const description =
config.pageDescription || '请按页面指引完成平台登录和游戏角色绑定,客服随后会处理您的订单。'
const currentStep = resolveStep(selectedTask, bindingInfo)
const loginQrVisible =
selectedTask && ['qr_pending', 'scanned'].includes(selectedTask.loginStatus)
const bindingQrVisible =
selectedTask?.loginStatus === 'authenticated' &&
['qr_pending', 'scanned', 'role_ready'].includes(selectedTask.bindingStatus)
return (
<div className="customer-fulfillment-page">
<Space direction="vertical" size={18} style={{ width: '100%' }}>
<div>
<Title level={2} style={{ marginBottom: 4 }}>
{title}
</Title>
<Paragraph type="secondary" style={{ marginBottom: 0 }}>
{description}
</Paragraph>
</div>
{tasks.length > 1 ? (
<Card size="small" title="请选择需要处理的产品">
<Space wrap>
{tasks.map((task) => (
<Button
key={task.id}
type={task.id === selectedTask?.id ? 'primary' : 'default'}
onClick={() => {
setSelectedTaskId(task.id)
setLoginInfo(null)
setBindingInfo(null)
}}
>
{task.productName || task.skuName} × {task.quantity}
</Button>
))}
</Space>
</Card>
) : null}
<Card>
<Row gutter={[24, 20]}>
<Col xs={24} lg={8}>
<Space direction="vertical" size={10}>
<Text type="secondary">订单号</Text>
<Text strong style={{ fontSize: 18 }}>
{selectedTask?.orderNo}
</Text>
<Text type="secondary">商品</Text>
<Text strong>{selectedTask?.productName || selectedTask?.skuName}</Text>
<Tag color="purple">{selectedTask?.platformLabel || selectedTask?.platform}</Tag>
<Tag color="blue">{selectedTask?.displayStatus.label}</Tag>
</Space>
</Col>
<Col xs={24} lg={16}>
<Steps
current={currentStep}
items={[
{ title: '登录平台', icon: <QrcodeOutlined /> },
{ title: '扫码绑定', icon: <SafetyCertificateOutlined /> },
{ title: '客服处理', icon: <LoadingOutlined /> },
{ title: '完成' },
]}
/>
</Col>
</Row>
</Card>
{selectedTask && selectedTask.executionStatus === 'failed' ? (
<Alert
type="error"
showIcon
message="当前流程需要客服处理"
description={selectedTask.displayStatus.label}
/>
) : null}
{selectedTask?.loginStatus === 'waiting_login' ||
selectedTask?.loginStatus === 'failed' ||
selectedTask?.loginStatus === 'expired' ? (
<Card title="第一步:登录平台账号">
<Space direction="vertical" size={14} style={{ width: '100%' }}>
<Alert
type="info"
showIcon
message="请使用本人账号登录"
description={
config.loginInstructions ||
'点击下方按钮后使用虎牙 App 扫码登录。登录仅用于本次订单履约,不会在页面展示账号 Cookie。'
}
/>
<Button type="primary" size="large" onClick={() => void handleStartLogin()}>
获取登录二维码
</Button>
</Space>
</Card>
) : null}
{loginQrVisible ? (
<Card title="使用虎牙 App 扫码登录">
<Space direction="vertical" align="center" size={14} style={{ width: '100%' }}>
{loginQrImage ? (
<Image src={loginQrImage} alt="虎牙登录二维码" width={260} preview={false} />
) : (
<Spin tip="生成二维码..." />
)}
<Text type="secondary">{loginInfo?.message || '等待扫码'}</Text>
{selectedTask.loginStatus === 'scanned' ? (
<Tag color="cyan">已扫码,请在 App 确认</Tag>
) : null}
<Button onClick={() => void handleStartLogin()}>重新获取二维码</Button>
</Space>
</Card>
) : null}
{bindingQrVisible ? (
<Card title="第二步:扫码绑定游戏角色">
<Space direction="vertical" align="center" size={14} style={{ width: '100%' }}>
{bindingInfo?.qrImage ? (
<Image src={bindingInfo.qrImage} alt="游戏角色绑定二维码" width={260} />
) : (
<Spin tip="生成绑定二维码..." />
)}
<Text type="secondary">
{bindingInfo?.message ||
config.bindingInstructions ||
'请使用虎牙 App 扫码完成游戏角色绑定。'}
</Text>
{bindingInfo?.bindGameRole ? (
<Alert
type="success"
showIcon
message={`已识别角色:${bindingInfo.bindGameRole}`}
description={[bindingInfo.bindGameAccount, bindingInfo.bindGameChannel]
.filter(Boolean)
.join(' · ')}
/>
) : null}
{selectedTask.bindingStatus === 'role_ready' ? (
<Button type="primary" onClick={() => void handleConfirmBinding()}>
确认绑定角色
</Button>
) : null}
</Space>
</Card>
) : null}
{selectedTask?.bindingStatus === 'confirmed' &&
selectedTask.executionStatus !== 'completed' ? (
<Card>
<Result
status="info"
title="账号已绑定,客服正在处理"
subTitle="客服会使用本次授权完成充值或赠送,请稍后刷新页面查看结果。"
/>
</Card>
) : null}
</Space>
</div>
)
}
function resolveStep(task: CustomerSemiAutoTask | null, binding: CustomerBindingInfo | null) {
if (!task) return 0
if (task.executionStatus === 'completed') return 3
if (task.bindingStatus === 'confirmed') return 2
if (task.loginStatus === 'authenticated' || binding) return 1
return 0
}