feat(semi-auto): 客户链接按平台严格绑定,注入后统一由前端打开指定页
This commit is contained in:
@@ -27,7 +27,7 @@ import {
|
||||
} from 'antd'
|
||||
import type { TableColumnsType } from 'antd'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { useEffect, useMemo, useRef, useState } from 'react'
|
||||
|
||||
import ImageUpload from '@/components/files/ImageUpload'
|
||||
import PageHeader from '@/components/admin/PageHeader'
|
||||
@@ -47,6 +47,8 @@ import {
|
||||
import type { SemiAutoTask } from '@/types/admin/semi-auto'
|
||||
import type { UploadedFile } from '@/types/worker-platform'
|
||||
import { formatAdminDateTime } from '@/utils/admin-time'
|
||||
import { buildCustomerFulfillmentPath } from '@/utils/customer-fulfillment-link'
|
||||
import { resolvePostInjectionUrl } from '@/utils/semi-auto-injection'
|
||||
|
||||
const { Text } = Typography
|
||||
|
||||
@@ -87,6 +89,15 @@ type CompleteFormValues = {
|
||||
costYuan?: number
|
||||
}
|
||||
|
||||
const INJECTION_FOLLOW_UP_TIMEOUT_MS = 30_000
|
||||
|
||||
type PendingInjection = {
|
||||
taskId: number
|
||||
platform: SemiAutoTask['platform']
|
||||
popup: Window | null
|
||||
timeoutId: number
|
||||
}
|
||||
|
||||
export default function AdminSemiAutoTasksPage() {
|
||||
const { message, modal } = App.useApp()
|
||||
const queryClient = useQueryClient()
|
||||
@@ -100,6 +111,7 @@ export default function AdminSemiAutoTasksPage() {
|
||||
const [completeTask, setCompleteTask] = useState<SemiAutoTask | null>(null)
|
||||
const [completeFiles, setCompleteFiles] = useState<UploadedFile[]>([])
|
||||
const [completeForm] = Form.useForm<CompleteFormValues>()
|
||||
const pendingInjectionRef = useRef<PendingInjection | null>(null)
|
||||
|
||||
const params = useMemo(
|
||||
() => ({
|
||||
@@ -137,25 +149,78 @@ export default function AdminSemiAutoTasksPage() {
|
||||
const handleResult = (event: Event) => {
|
||||
const detail = (event as CustomEvent<string>).detail
|
||||
if (typeof detail !== 'string') return
|
||||
let result: { ok?: boolean; written?: number; error?: string }
|
||||
try {
|
||||
const result = JSON.parse(detail) as {
|
||||
ok?: boolean
|
||||
written?: number
|
||||
error?: string
|
||||
}
|
||||
if (!result || typeof result !== 'object') return
|
||||
if (result.ok) {
|
||||
message.success(`浏览器登录态已注入(${Number(result.written || 0)} 个 Cookie)`)
|
||||
} else {
|
||||
message.error(result.error || '浏览器登录态注入失败')
|
||||
}
|
||||
result = JSON.parse(detail) as { ok?: boolean; written?: number; error?: string }
|
||||
} catch {
|
||||
// 忽略其他页面事件
|
||||
return
|
||||
}
|
||||
if (!result || typeof result !== 'object') return
|
||||
const pending = pendingInjectionRef.current
|
||||
if (result.ok) {
|
||||
message.success(`浏览器登录态已注入(${Number(result.written || 0)} 个 Cookie)`)
|
||||
if (!pending) return
|
||||
window.clearTimeout(pending.timeoutId)
|
||||
pendingInjectionRef.current = null
|
||||
const targetUrl = resolvePostInjectionUrl(pending.platform)
|
||||
const popup = pending.popup
|
||||
if (!targetUrl) {
|
||||
if (popup && !popup.closed) popup.close()
|
||||
return
|
||||
}
|
||||
if (popup && !popup.closed) {
|
||||
try {
|
||||
popup.location.href = targetUrl
|
||||
popup.focus()
|
||||
return
|
||||
} catch {
|
||||
// 占位页已被关闭或无法导航:先关掉残留标签,再用 window.open 兜底。
|
||||
try {
|
||||
popup.close()
|
||||
} catch {
|
||||
// 关闭失败不影响继续打开目标页。
|
||||
}
|
||||
}
|
||||
}
|
||||
const opened = window.open(targetUrl, '_blank')
|
||||
if (opened) {
|
||||
opened.focus()
|
||||
return
|
||||
}
|
||||
modal.info({
|
||||
title: '注入成功',
|
||||
content: (
|
||||
<span>
|
||||
浏览器拦截了自动打开的新标签页,请
|
||||
<a href={targetUrl} target="_blank" rel="noreferrer">
|
||||
点击这里打开平台页面
|
||||
</a>
|
||||
。
|
||||
</span>
|
||||
),
|
||||
})
|
||||
return
|
||||
}
|
||||
message.error(result.error || '浏览器登录态注入失败')
|
||||
if (!pending) return
|
||||
window.clearTimeout(pending.timeoutId)
|
||||
pendingInjectionRef.current = null
|
||||
if (pending.popup && !pending.popup.closed) pending.popup.close()
|
||||
}
|
||||
window.addEventListener('huya-exchange-result', handleResult)
|
||||
return () => window.removeEventListener('huya-exchange-result', handleResult)
|
||||
}, [message])
|
||||
}, [message, modal])
|
||||
|
||||
// 页面卸载时清理未完成的占位页,避免留下空白标签。
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
const pending = pendingInjectionRef.current
|
||||
if (!pending) return
|
||||
window.clearTimeout(pending.timeoutId)
|
||||
if (pending.popup && !pending.popup.closed) pending.popup.close()
|
||||
pendingInjectionRef.current = null
|
||||
}
|
||||
}, [])
|
||||
|
||||
const syncMutation = useMutation({
|
||||
mutationFn: (taskId: number) => syncAdminSemiAutoTask(taskId),
|
||||
@@ -175,12 +240,33 @@ export default function AdminSemiAutoTasksPage() {
|
||||
onError: (error) => message.error(error instanceof Error ? error.message : '领取失败'),
|
||||
})
|
||||
|
||||
function cancelPendingInjection() {
|
||||
const pending = pendingInjectionRef.current
|
||||
if (!pending) return
|
||||
window.clearTimeout(pending.timeoutId)
|
||||
if (pending.popup && !pending.popup.closed) pending.popup.close()
|
||||
pendingInjectionRef.current = null
|
||||
}
|
||||
|
||||
function openInjectionPlaceholder() {
|
||||
const popup = window.open('about:blank', '_blank')
|
||||
if (!popup) return null
|
||||
try {
|
||||
popup.document.title = '正在注入浏览器登录态'
|
||||
popup.document.body.textContent = '正在注入浏览器登录态,完成后将自动打开平台页面…'
|
||||
} catch {
|
||||
// 占位内容写入失败不影响后续跳转。
|
||||
}
|
||||
return popup
|
||||
}
|
||||
|
||||
const injectionMutation = useMutation({
|
||||
mutationFn: (task: SemiAutoTask) =>
|
||||
createAdminSemiAutoBrowserSession(task.id, firstTargetUrl(task)),
|
||||
onSuccess: (response) => {
|
||||
const ticket = String(response.data?.ticket || '').trim()
|
||||
if (!ticket) {
|
||||
cancelPendingInjection()
|
||||
message.error('注入票据生成失败')
|
||||
return
|
||||
}
|
||||
@@ -188,10 +274,39 @@ export default function AdminSemiAutoTasksPage() {
|
||||
message.info('正在注入浏览器登录态,请稍候')
|
||||
void refreshAll()
|
||||
},
|
||||
onError: (error) =>
|
||||
message.error(error instanceof Error ? error.message : '打开浏览器登录态失败'),
|
||||
onError: (error) => {
|
||||
cancelPendingInjection()
|
||||
message.error(error instanceof Error ? error.message : '打开浏览器登录态失败')
|
||||
},
|
||||
})
|
||||
|
||||
function startInjection(task: SemiAutoTask) {
|
||||
if (pendingInjectionRef.current) {
|
||||
message.warning('上一次注入尚未完成,请稍候')
|
||||
return
|
||||
}
|
||||
// 扩展内容脚本会在白名单页面打标记;没有标记时直接提示,避免空等 30 秒。
|
||||
if (document.documentElement.dataset.huyaExchangeHelper !== '1') {
|
||||
message.warning('未检测到浏览器扩展,请启用「虎牙兑换助手」后刷新本页')
|
||||
return
|
||||
}
|
||||
const popup = openInjectionPlaceholder()
|
||||
const timeoutId = window.setTimeout(() => {
|
||||
const pending = pendingInjectionRef.current
|
||||
if (!pending) return
|
||||
pendingInjectionRef.current = null
|
||||
if (pending.popup && !pending.popup.closed) pending.popup.close()
|
||||
message.warning('未检测到注入完成,请确认浏览器扩展已启用后重试')
|
||||
}, INJECTION_FOLLOW_UP_TIMEOUT_MS)
|
||||
pendingInjectionRef.current = {
|
||||
taskId: task.id,
|
||||
platform: task.platform,
|
||||
popup,
|
||||
timeoutId,
|
||||
}
|
||||
injectionMutation.mutate(task)
|
||||
}
|
||||
|
||||
const credentialExportMutation = useMutation({
|
||||
mutationFn: (taskId: number) => exportAdminSemiAutoCredential(taskId),
|
||||
onSuccess: (response) => {
|
||||
@@ -267,7 +382,12 @@ export default function AdminSemiAutoTasksPage() {
|
||||
}
|
||||
|
||||
async function copyCustomerLink(task: SemiAutoTask, successMessage = '客户授权链接已复制') {
|
||||
const url = `${window.location.origin}/c/${encodeURIComponent(task.orderNo)}`
|
||||
const path = buildCustomerFulfillmentPath(task.orderNo, task.platform)
|
||||
if (!path) {
|
||||
message.error('订单号为空,无法生成客户链接')
|
||||
return false
|
||||
}
|
||||
const url = `${window.location.origin}${path}`
|
||||
try {
|
||||
await navigator.clipboard.writeText(url)
|
||||
if (successMessage) message.success(successMessage)
|
||||
@@ -451,7 +571,7 @@ export default function AdminSemiAutoTasksPage() {
|
||||
icon={<ChromeOutlined />}
|
||||
disabled={!taskCanUseCredential(task)}
|
||||
loading={injectionMutation.isPending}
|
||||
onClick={() => injectionMutation.mutate(task)}
|
||||
onClick={() => startInjection(task)}
|
||||
>
|
||||
注入浏览器
|
||||
</Button>
|
||||
@@ -720,7 +840,7 @@ export default function AdminSemiAutoTasksPage() {
|
||||
icon={<ChromeOutlined />}
|
||||
disabled={!taskCanUseCredential(detail.task)}
|
||||
loading={injectionMutation.isPending}
|
||||
onClick={() => injectionMutation.mutate(detail.task)}
|
||||
onClick={() => startInjection(detail.task)}
|
||||
>
|
||||
注入浏览器
|
||||
</Button>
|
||||
|
||||
Reference in New Issue
Block a user