import { CheckCircleOutlined, DeleteOutlined, ReloadOutlined, RollbackOutlined, SearchOutlined, SendOutlined, SyncOutlined, } from '@ant-design/icons' import { Alert, Button, Card, Descriptions, Input, Popconfirm, Select, Space, Tag, Typography, } from 'antd' import PercentTable from '@/components/common/PercentTable' import type { TableColumnsType } from 'antd' import { useEffect, useMemo, useRef, useState } from 'react' import PageHeader from '@/components/admin/PageHeader' import { showError, showSuccess } from '@/lib/feedback' import { cancelAdminDouyinMashidaCertificateVerify, checkAdminDouyinMashidaCertificate, fetchAdminDouyinMashidaCertificates, invalidateAdminDouyinMashidaCertificate, resendAdminDouyinMashidaCertificate, syncAdminDouyinMashidaCertificate, verifyAdminDouyinMashidaCertificate, } from '@/services/admin' import type { AdminDouyinMashidaCertificate, AdminDouyinMashidaCertificateActionPayload, AdminDouyinMashidaPlatformCoupon, } from '@/types/admin' import { getAdminRole } from '@/utils/admin-auth' import { ADMIN_DEFAULT_PAGE_SIZE, buildAdminTablePagination } from '@/utils/admin-pagination' import { formatAdminDateTime } from '@/utils/admin-time' /** * 后台「抖音券核销」。 * * 与「快手电子凭证」是两套独立的表和状态机: * 这里的券是我们自己生成的 DYC 券码,经 `coupons.syncV2` 交给抖音码速达展示给买家, * 核销打的是 `coupons.verifyV3`、冲正打 `coupons.cancelVerify`。 * * 界面照 `AdminKuaishouIndustryPage` 的骨架做(筛选 + 左列表 + 右操作),运营不用重新学习; * 但数据口径换成抖音自己的:**平台侧券码优先**(syncV2 可能改写我们的券码, * 拿旧券码核销会得到 `10017 核销码与订单不匹配`)。 */ type CertificateFilterState = { certNo: string orderId: string status: string syncStatus: string /** 只看需要人跟进的异常:同步失败 / 已核销但平台显示未使用 */ abnormalOnly: boolean page: number pageSize: number } type ActionKey = 'check' | 'verify' | 'cancel' | 'sync' | 'resend' | 'invalidate' type ActionResultView = { key: ActionKey title: string success: boolean message: string executedAt: string raw: unknown } const STATUS_OPTIONS = [ { label: '已生成 issued', value: 'issued' }, { label: '已同步 synced', value: 'synced' }, { label: '已核销 verified', value: 'verified' }, { label: '已作废 invalid', value: 'invalid' }, ] const SYNC_STATUS_OPTIONS = [ { label: '待同步 pending', value: 'pending' }, { label: '同步成功 success', value: 'success' }, { label: '同步失败 failed', value: 'failed' }, ] function createEmptyFilters(): CertificateFilterState { return { certNo: '', orderId: '', status: '', syncStatus: '', abnormalOnly: false, page: 1, pageSize: ADMIN_DEFAULT_PAGE_SIZE, } } export default function AdminDouyinMashidaCertificatePage() { const role = getAdminRole() const canInvalidate = role === 'admin' || role === 'operator' const [loading, setLoading] = useState(false) const [actionLoading, setActionLoading] = useState('') const [certificates, setCertificates] = useState([]) const [total, setTotal] = useState(0) const [selected, setSelected] = useState(null) const [filters, setFilters] = useState(createEmptyFilters) // 异步操作后刷新列表时,沿用用户最后一次的筛选条件 const filtersRef = useRef(filters) const [result, setResult] = useState(null) const [manualCertNo, setManualCertNo] = useState('') useEffect(() => { void loadCertificates() }, []) async function loadCertificates(patch: Partial = {}) { const next = updateFilters(patch) setLoading(true) try { const response = await fetchAdminDouyinMashidaCertificates({ certNo: next.certNo.trim(), orderId: next.orderId.trim(), status: next.abnormalOnly ? '' : next.status, syncStatus: next.abnormalOnly ? 'failed' : next.syncStatus, page: next.page, pageSize: next.pageSize, }) setCertificates(response.data.items) setTotal(response.data.pagination.total) setSelected((current) => { if (!current) { return null } return response.data.items.find((item) => item.id === current.id) || current }) } catch (error) { showError(error instanceof Error ? error.message : '查询抖音兑换券失败') } finally { setLoading(false) } } function updateFilters(patch: Partial): CertificateFilterState { const next = { ...filtersRef.current, ...patch } filtersRef.current = next setFilters(next) return next } const columns = useMemo>( () => [ { title: '券码', width: 240, render: (_, row) => (
{row.verifyCertNo || '-'} {/* 平台改过券码时必须让它显眼:贴错券码核销会得到 10017 */} {row.platformCertNo && row.platformCertNo !== row.certNo ? ( 平台已改名:{row.certNo} ) : ( 发券:{formatAdminDateTime(row.grantTime)} )}
), }, { title: '订单商品', width: 300, ellipsis: true, render: (_, row) => (
{row.orderId || '-'} {row.skuCode || row.skuName ? ( {row.skuCode || row.skuName} ) : ( 未关联商品 )} 任务 {row.taskNo || '-'} {row.parentOrderId && row.parentOrderId !== row.orderId ? ` · 父单 ${row.parentOrderId}` : ''}
), }, { title: '本地状态', width: 150, render: (_, row) => (
{resolveStatusLabel(row.status)} 同步 {resolveSyncLabel(row.syncStatus)} {row.syncError ? ( {row.syncError} ) : null}
), }, { title: '平台侧', width: 160, render: (_, row) => (
{row.platformStatusLabel || '未回查'} 核销:{formatAdminDateTime(row.verifiedAt)}
), }, { title: '操作', fixed: 'right', width: 96, render: (_, row) => { const isSelected = selected?.id === row.id return ( ) }, }, ], [selected?.id], ) function selectRow(row: AdminDouyinMashidaCertificate) { setSelected(row) setManualCertNo('') } const targetCertNo = String(selected?.verifyCertNo || selected?.certNo || manualCertNo).trim() function buildPayload(): AdminDouyinMashidaCertificateActionPayload { return { certNo: targetCertNo, orderId: selected?.orderId || undefined, localOrderId: selected?.localOrderId || undefined, orderItemId: selected?.orderItemId || undefined, } } async function runAction( key: ActionKey, title: string, successMessage: string, action: () => Promise<{ data: unknown }>, ) { if (!targetCertNo) { showError('请先选择券码,或在下方手动填写券码') return } setActionLoading(key) try { const response = await action() publishResult(key, title, true, successMessage, response.data) showSuccess(successMessage) await loadCertificates() } catch (error) { const message = error instanceof Error ? error.message : `${title}失败` publishResult(key, title, false, message, null) showError(message) } finally { setActionLoading('') } } function publishResult( key: ActionKey, title: string, success: boolean, message: string, raw: unknown, ) { setResult({ key, title, success, message, executedAt: new Date().toISOString(), raw, }) } return (
updateFilters({ certNo: event.target.value })} /> updateFilters({ orderId: event.target.value })} /> updateFilters({ syncStatus: value || '' })} />
selected?.id === row.id ? 'kuaishou-industry-row-selected' : '' } scroll={{ x: 1046 }} pagination={buildAdminTablePagination({ current: filters.page, pageSize: filters.pageSize, total, onChange: (page, pageSize) => { void loadCertificates({ page, pageSize }) }, })} />
当前卡券
{selected ? (
{selected.verifyCertNo || '-'} {resolveStatusLabel(selected.status)}
订单号 {selected.orderId || '-'}
本地订单 {selected.localOrderId ?? '-'}
平台侧 {selected.platformStatusLabel || '未回查'} {selected.platformCertNo || selected.platformCertId || '-'}
兑换链接 {selected.certLink || '-'}
) : (
尚未选择卡券 请从左侧列表选择,或在下方手动指定。
)}
手动指定卡券
void runAction('invalidate', '作废券码', '券码已作废', () => invalidateAdminDouyinMashidaCertificate({ ...buildPayload(), reason: '后台手动作废', }), ) } >
{selected?.status === 'verified' ? ( ) : null}
{result ? (
{result.title} {formatAdminDateTime(result.executedAt)} {result.message} {result.key === 'check' || result.key === 'sync' ? ( {renderPlatformCouponDescriptions(result.raw)} ) : null}
) : ( 暂无操作记录 )}
) } /** 从「对账 / 重新同步」的返回里取出平台侧券信息。 */ function renderPlatformCouponDescriptions(raw: unknown) { const coupon = extractPlatformCoupon(raw) return [ {coupon?.certNo || '-'} , {coupon?.statusLabel || '-'} , {coupon ? String(coupon.verifyNum) : '-'} , {coupon?.certLink || '-'} , ] } function extractPlatformCoupon(raw: unknown): AdminDouyinMashidaPlatformCoupon | null { if (!raw || typeof raw !== 'object') { return null } const data = raw as { data?: unknown } const source = (data.data && typeof data.data === 'object' ? data.data : raw) as Record< string, unknown > const platform = source.platform return platform && typeof platform === 'object' ? (platform as AdminDouyinMashidaPlatformCoupon) : null } function resolveStatusColor(status: string): string { switch (String(status || '').trim()) { case 'verified': return 'green' case 'synced': return 'blue' case 'invalid': return 'red' case 'issued': return 'gold' default: return 'default' } } function resolveStatusLabel(status: string): string { switch (String(status || '').trim()) { case 'verified': return '已核销' case 'synced': return '已同步' case 'invalid': return '已作废' case 'issued': return '已生成' default: return status || '未知' } } function resolveSyncLabel(syncStatus: string): string { switch (String(syncStatus || '').trim()) { case 'success': return '成功' case 'failed': return '失败' case 'pending': return '待同步' default: return syncStatus || '-' } } /** 平台侧状态:3 已使用是真核销,其余都按待处理/异常提示。 */ function resolvePlatformColor(platformStatus: number | null): string { switch (Number(platformStatus)) { case 2: return 'blue' case 3: case 6: return 'green' case 4: case 5: return 'red' case 1: return 'gold' default: return 'default' } }