fix(douyu): 修正绑定刷新与自动启动交互

This commit is contained in:
yml
2026-11-20 22:03:20 +08:00
parent 0982993a6d
commit 38155ca1c3
7 changed files with 128 additions and 38 deletions
@@ -13,6 +13,7 @@ import { useParams } from 'react-router'
import {
confirmDouyuBinding,
fetchDouyuFulfillment,
refreshDouyuBinding,
saveDouyuProfile,
startDouyuBinding,
} from '@/services/douyu-fulfillment'
@@ -79,7 +80,6 @@ export default function DouyuFulfillmentPage() {
const [qrImage, setQrImage] = useState('')
const [bindingQrExpanded, setBindingQrExpanded] = useState(false)
const [nowMs, setNowMs] = useState(() => Date.now())
const autoStartRef = useRef('')
const customerRoleRef = useRef('')
const roleNameIssue = useMemo(() => getDouyuRoleNameIssue(roleName), [roleName])
@@ -178,33 +178,6 @@ export default function DouyuFulfillmentPage() {
return () => window.clearInterval(timer)
}, [live?.qrExpiresAt])
useEffect(() => {
if (!task || !live || needsProfile || live.qrUrl || live.bindConfirmed) return
if (!live.accountReady) return
if (
task.status !== 'preparing' &&
task.status !== 'allocated' &&
task.status !== 'waiting_profile'
) {
return
}
if (autoStartRef.current === orderNo) return
autoStartRef.current = orderNo
let cancelled = false
setBindingLoading(true)
void startDouyuBinding(orderNo)
.then((response) => {
if (!cancelled) applyData(response.data)
})
.catch(() => undefined)
.finally(() => {
if (!cancelled) setBindingLoading(false)
})
return () => {
cancelled = true
}
}, [applyData, live, needsProfile, orderNo, task])
const expiresInSeconds = useMemo(() => {
const raw = live?.qrExpiresAt || ''
const expiresAt = raw ? Date.parse(raw) : 0
@@ -280,12 +253,18 @@ export default function DouyuFulfillmentPage() {
}
async function startBinding(forceRefresh = false) {
if (forceRefresh && !qrExpired) {
message.info('二维码有效期内无需刷新')
return
}
setBindingLoading(true)
try {
if (forceRefresh) {
const refreshed = await refreshDouyuBinding(orderNo)
applyData(refreshed.data)
const refreshedLive = refreshed.data.live
const expiresAt = refreshedLive.qrExpiresAt ? Date.parse(refreshedLive.qrExpiresAt) : 0
if (refreshedLive.qrUrl && expiresAt > Date.now()) {
message.info('二维码仍在有效期内,已复用当前二维码')
return
}
}
const response = await startDouyuBinding(orderNo, forceRefresh)
applyData(response.data)
message.success(forceRefresh ? '二维码已刷新' : '绑定流程已继续')
@@ -296,6 +275,19 @@ export default function DouyuFulfillmentPage() {
}
}
async function refreshStatus() {
setBindingLoading(true)
try {
const response = await refreshDouyuBinding(orderNo)
applyData(response.data)
message.success('绑定状态已刷新')
} catch (error) {
message.error(error instanceof Error ? error.message : '刷新绑定状态失败')
} finally {
setBindingLoading(false)
}
}
async function confirmBinding() {
setBindingLoading(true)
try {
@@ -501,9 +493,9 @@ export default function DouyuFulfillmentPage() {
size="large"
className="customer-douyu-preparing-button"
loading={bindingLoading}
onClick={() => void startBinding(false)}
onClick={() => void refreshStatus()}
>
继续准备
刷新状态
</Button>
</div>
) : null}
@@ -38,6 +38,13 @@ export function pollDouyuBinding(orderNo: string) {
)
}
export function refreshDouyuBinding(orderNo: string) {
return apiPost<DouyuCustomerFulfillment>(
`/api/v1/douyu-fulfillment/${encodeURIComponent(orderNo)}/binding/refresh`,
{},
)
}
export function confirmDouyuBinding(orderNo: string) {
return apiPost<DouyuCustomerFulfillment>(
`/api/v1/douyu-fulfillment/${encodeURIComponent(orderNo)}/binding/confirm`,
@@ -58,6 +58,10 @@ export type DouyuLiveBindingState = {
platName: string
bindReadyForConfirm: boolean
bindConfirmed: boolean
queueState?: string
retryAfterMs?: number
queueLane?: string
accepted?: boolean
customerProfileReset?: boolean
resetReason?: string
completedAt: string | null
@@ -0,0 +1,39 @@
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import test from 'node:test'
const root = dirname(fileURLToPath(import.meta.url))
const page = readFileSync(
join(root, '../src/pages/douyu-fulfillment/DouyuFulfillmentPage.tsx'),
'utf8',
)
const frontendService = readFileSync(join(root, '../src/services/douyu-fulfillment.ts'), 'utf8')
const backendRoute = readFileSync(
join(root, '../../backend/src/routes/douyu-fulfillment.ts'),
'utf8',
)
test('斗鱼准备阶段只刷新状态,不自动重复开始绑定', () => {
assert.ok(!page.includes('autoStartRef'))
assert.ok(!page.includes('void startDouyuBinding(orderNo)'))
assert.ok(page.includes('async function refreshStatus()'))
assert.ok(page.includes('onClick={() => void refreshStatus()}'))
assert.ok(page.includes('刷新状态'))
})
test('刷新二维码前先同步权威状态,有效二维码直接复用', () => {
const start = page.indexOf('async function startBinding(forceRefresh = false)')
const body = page.slice(start, start + 1200)
assert.ok(
body.indexOf('refreshDouyuBinding(orderNo)') < body.indexOf('startDouyuBinding(orderNo'),
)
assert.ok(body.includes('二维码仍在有效期内,已复用当前二维码'))
})
test('客户刷新状态走独立接口,不复用绑定启动接口', () => {
assert.ok(frontendService.includes('/binding/refresh'))
assert.ok(backendRoute.includes("'/:orderNo/binding/refresh'"))
assert.ok(backendRoute.includes('refreshDouyuCustomerBinding'))
})