feat(huya): 增加兑换记录结果图
This commit is contained in:
@@ -6,7 +6,7 @@ import {
|
||||
} from 'antd';
|
||||
import type { TableProps } from 'antd';
|
||||
import {
|
||||
CheckCircleOutlined, ColumnWidthOutlined, CopyOutlined, GiftOutlined, ImportOutlined, QrcodeOutlined,
|
||||
CheckCircleOutlined, ColumnWidthOutlined, CopyOutlined, DownloadOutlined, GiftOutlined, ImportOutlined, PictureOutlined, QrcodeOutlined,
|
||||
ReloadOutlined, SearchOutlined, SettingOutlined, ShoppingOutlined, StopOutlined, SwapOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import { huyaApi, type HuyaAccountItem, type HuyaConfig, type HuyaGoodsItem, type HuyaRechargeGoodsItem, type HuyaTaskItem } from '../api/modules';
|
||||
@@ -16,6 +16,12 @@ import PaymentQrQueue, { type PaymentQrQueueItem } from '../components/PaymentQr
|
||||
import RealtimeLogPanel from '../components/RealtimeLogPanel';
|
||||
import { usePermissions } from '../hooks/usePermissions';
|
||||
import { useWebSocketLogs } from '../hooks/useWebSocketLogs';
|
||||
import {
|
||||
clipboardImageSupported,
|
||||
copyImageToClipboard,
|
||||
getExchangeResultImage,
|
||||
type ExchangeResultImageData,
|
||||
} from '../utils/exchangeImage';
|
||||
import { getErrorMessage } from '../utils/error';
|
||||
import { message } from '../utils/antdMessage';
|
||||
import { formatTime, formatUnixSeconds } from '../utils/time';
|
||||
@@ -230,6 +236,12 @@ export default function HuyaElitePage() {
|
||||
const [importSelected, setImportSelected] = useState<number[]>([]);
|
||||
const [qrTask, setQrTask] = useState<HuyaTaskItem | null>(null);
|
||||
const [recordsTask, setRecordsTask] = useState<HuyaTaskItem | null>(null);
|
||||
const [exchangeImagePreview, setExchangeImagePreview] = useState<{
|
||||
data: ExchangeResultImageData;
|
||||
url: string;
|
||||
blob: Blob;
|
||||
} | null>(null);
|
||||
const [exchangeImageLoadingKey, setExchangeImageLoadingKey] = useState<string | null>(null);
|
||||
const [qrTaskLoading, setQrTaskLoading] = useState(false);
|
||||
// 小程序码图片独立保存:任务轮询只会更新轻量 tasks,不允许覆盖这张大图。
|
||||
const [qrImage, setQrImage] = useState('');
|
||||
@@ -639,6 +651,69 @@ export default function HuyaElitePage() {
|
||||
catch { message.error('复制失败,请手动选中复制'); }
|
||||
};
|
||||
|
||||
const exchangeRecordImageData = (
|
||||
record: Record<string, unknown>,
|
||||
index: number,
|
||||
): ExchangeResultImageData => {
|
||||
const snapshot = (
|
||||
recordsTask?.result?.account_snapshot
|
||||
&& typeof recordsTask.result.account_snapshot === 'object'
|
||||
)
|
||||
? recordsTask.result.account_snapshot as Record<string, unknown>
|
||||
: {};
|
||||
const recordKey = String(record.record_id || record.order_id || index);
|
||||
return {
|
||||
key: `huya:${recordsTask?.id || 'records'}:${recordKey}`,
|
||||
goodsName: String(record.prize_name || '未知商品'),
|
||||
goodsPic: String(record.icon || ''),
|
||||
roleName: String(snapshot.game_name || ''),
|
||||
channel: String(snapshot.game_channel || ''),
|
||||
accountName: String(snapshot.uid || recordsTask?.account_uid || snapshot.nickname || recordsTask?.account_nickname || '-'),
|
||||
exchangeTime: String(record.exchange_time_text || ''),
|
||||
statusText: String(record.status_label || '兑换成功'),
|
||||
footerText: '虎牙精英宝典兑换记录凭证 · 数据来自官方兑换记录',
|
||||
};
|
||||
};
|
||||
|
||||
const generateExchangeRecordImage = async (
|
||||
record: Record<string, unknown>,
|
||||
index: number,
|
||||
) => {
|
||||
const data = exchangeRecordImageData(record, index);
|
||||
const key = String(data.key);
|
||||
setExchangeImageLoadingKey(key);
|
||||
try {
|
||||
const generated = await getExchangeResultImage(data);
|
||||
setExchangeImagePreview({ data, url: generated.url, blob: generated.blob });
|
||||
} catch (error) {
|
||||
message.error(`兑换结果图生成失败:${getErrorMessage(error)}`);
|
||||
} finally {
|
||||
setExchangeImageLoadingKey(null);
|
||||
}
|
||||
};
|
||||
|
||||
const copyExchangeRecordImage = async () => {
|
||||
if (!exchangeImagePreview) return;
|
||||
try {
|
||||
await copyImageToClipboard(exchangeImagePreview.blob);
|
||||
message.success('兑换结果图已复制');
|
||||
} catch (error) {
|
||||
if (clipboardImageSupported()) {
|
||||
message.error(`复制图片失败:${getErrorMessage(error)}`);
|
||||
} else {
|
||||
message.warning('当前浏览器不支持复制图片,请使用“下载图片”');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const downloadExchangeRecordImage = () => {
|
||||
if (!exchangeImagePreview) return;
|
||||
const link = document.createElement('a');
|
||||
link.href = exchangeImagePreview.url;
|
||||
link.download = `虎牙兑换凭证-${exchangeImagePreview.data.goodsName}.png`;
|
||||
link.click();
|
||||
};
|
||||
|
||||
const viewedRecords = taskResultRecords(recordsTask);
|
||||
const exchangeRecordColumns: TableProps<Record<string, unknown>>['columns'] = [
|
||||
{
|
||||
@@ -683,6 +758,25 @@ export default function HuyaElitePage() {
|
||||
render: (value: unknown) =>
|
||||
value ? <Text copyable={{ text: String(value) }}>{String(value)}</Text> : '-',
|
||||
},
|
||||
{
|
||||
title: '结果图',
|
||||
width: 110,
|
||||
align: 'center',
|
||||
render: (_value: unknown, record, index) => {
|
||||
const key = String(record.record_id || record.order_id || index);
|
||||
return (
|
||||
<Button
|
||||
size="small"
|
||||
type="link"
|
||||
icon={<PictureOutlined />}
|
||||
loading={exchangeImageLoadingKey === `huya:${recordsTask?.id || 'records'}:${key}`}
|
||||
onClick={() => void generateExchangeRecordImage(record, index)}
|
||||
>
|
||||
结果图
|
||||
</Button>
|
||||
);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
// 列宽按比例分配(percent),表头与单元格统一居中,避免左右跳着看
|
||||
@@ -751,10 +845,37 @@ export default function HuyaElitePage() {
|
||||
showSizeChanger: false,
|
||||
showTotal: (total) => `共 ${total} 条`,
|
||||
}}
|
||||
scroll={{ x: 820 }}
|
||||
scroll={{ x: 930 }}
|
||||
/>
|
||||
</Space>
|
||||
</Modal>
|
||||
<Modal
|
||||
open={exchangeImagePreview !== null}
|
||||
title="兑换结果图"
|
||||
width={390}
|
||||
centered
|
||||
footer={null}
|
||||
onCancel={() => setExchangeImagePreview(null)}
|
||||
>
|
||||
{exchangeImagePreview && (
|
||||
<Space direction="vertical" size={12} align="center" style={{ width: '100%' }}>
|
||||
<img
|
||||
src={exchangeImagePreview.url}
|
||||
alt="兑换结果"
|
||||
style={{ width: '100%', borderRadius: 8, display: 'block' }}
|
||||
/>
|
||||
<Space wrap style={{ justifyContent: 'center' }}>
|
||||
<Button icon={<CopyOutlined />} onClick={() => void copyExchangeRecordImage()}>
|
||||
复制图片
|
||||
</Button>
|
||||
<Button icon={<DownloadOutlined />} onClick={downloadExchangeRecordImage}>
|
||||
下载图片
|
||||
</Button>
|
||||
<Button onClick={() => setExchangeImagePreview(null)}>关闭</Button>
|
||||
</Space>
|
||||
</Space>
|
||||
)}
|
||||
</Modal>
|
||||
<Modal
|
||||
open={payOpen}
|
||||
title={`支付码${payQueueItems.length ? `(${payTasks.length} 个待扫码${payQueuedCount ? ` · ${payQueuedCount} 个排队中` : ''}${payDoneCount ? ` · ${payDoneCount} 个已支付` : ''})` : ''}`}
|
||||
|
||||
Reference in New Issue
Block a user