import { useState, useEffect, FC, Card, Table, Button, Space, Modal, Form, Input, Select, Tag, message, Row, Col, Statistic, Badge, Typography, Divider, Alert, Spin, PlusOutlined, CheckCircleOutlined, CloseCircleOutlined, SyncOutlined, ShopOutlined, LinkOutlined, ApiOutlined, ReloadOutlined, DeleteOutlined, Title, Text, Option, } from '@/imports'; import type { ColumnsType } from 'antd/es/table'; import { platformAuthDataSource } from '@/services/platformAuthDataSource'; import { PlatformAccount, PlatformAccountStats, Platform } from '@/types/platform'; const PLATFORM_CONFIG: Record = { // TikTok系列 TIKTOK: { color: 'cyan', text: 'TikTok', icon: }, TIKTOK_FULL: { color: 'cyan', text: 'TikTok全托管', icon: }, // Shopee系列 SHOPEE: { color: 'red', text: 'Shopee', icon: }, SHOPEE_FULL: { color: 'red', text: 'Shopee全托管', icon: }, SHOPEE_LIGHT: { color: 'red', text: 'Shopee轻出海', icon: }, // Lazada系列 LAZADA: { color: 'purple', text: 'Lazada', icon: }, LAZADA_FULL: { color: 'purple', text: 'Lazada全托管', icon: }, // Temu TEMU_FULL: { color: 'green', text: 'Temu全托管', icon: }, // SHEIN系列 SHEIN: { color: 'pink', text: 'SHEIN', icon: }, SHEIN_HALF: { color: 'pink', text: 'SHEIN半托管', icon: }, // 其他平台 OZON: { color: 'yellow', text: 'Ozon', icon: }, YANDEX: { color: 'blue', text: 'Yandex', icon: }, ALIEXPRESS: { color: 'orange', text: 'AliExpress', icon: }, ALIEXPRESS_HALF: { color: 'orange', text: '速卖通半托管', icon: }, ALIEXPRESS_POP: { color: 'orange', text: '速卖通本土POP', icon: }, COUPANG: { color: 'red', text: 'Coupang', icon: }, WALMART: { color: 'blue', text: 'Walmart', icon: }, WILDBERRIES: { color: 'purple', text: 'Wildberries', icon: }, ALLEGRO: { color: 'green', text: 'Allegro', icon: }, MERCADO_LIBRE: { color: 'yellow', text: 'Mercado Libre', icon: }, JUMIA: { color: 'blue', text: 'Jumia', icon: }, JOOM: { color: 'purple', text: 'Joom', icon: }, AMAZON: { color: 'orange', text: 'Amazon', icon: }, WISH: { color: 'blue', text: 'Wish', icon: }, EMAG: { color: 'green', text: 'eMAG', icon: }, MIRAVIA: { color: 'pink', text: 'Miravia', icon: }, DARAZ: { color: 'blue', text: 'Daraz', icon: }, JOYBUY: { color: 'red', text: 'Joybuy', icon: }, ALIBABA: { color: 'orange', text: 'Alibaba', icon: }, QOO10: { color: 'red', text: 'Qoo10', icon: }, SHOPIFY: { color: 'green', text: 'Shopify', icon: }, SHOPLAZZA: { color: 'blue', text: 'Shoplazza', icon: }, SHOPYY_V1: { color: 'purple', text: 'SHOPYY v1.0', icon: }, SHOPYY_V2: { color: 'purple', text: 'SHOPYY v2.0', icon: }, SHOPLINE: { color: 'green', text: 'SHOPLINE', icon: }, GREATBOSS: { color: 'blue', text: 'GreatBoss', icon: }, OTHER: { color: 'default', text: '其他', icon: }, // 原有平台 EBAY: { color: 'blue', text: 'eBay', icon: }, }; const STATUS_CONFIG: Record = { active: { color: 'success', text: '已连接' }, inactive: { color: 'default', text: '未连接' }, expired: { color: 'warning', text: '已过期' }, error: { color: 'error', text: '错误' }, }; const PlatformAuth: FC = () => { const [accounts, setAccounts] = useState([]); const [stats, setStats] = useState({ total: 0, active: 0, inactive: 0, expired: 0, error: 0 }); const [loading, setLoading] = useState(true); const [modalVisible, setModalVisible] = useState(false); const [form] = Form.useForm(); useEffect(() => { loadData(); }, []); const loadData = async () => { setLoading(true); try { const [accountList, accountStats] = await Promise.all([ platformAuthDataSource.list(), platformAuthDataSource.getStats(), ]); setAccounts(accountList); setStats(accountStats); } catch (error: any) { message.error(`加载失败: ${error.message}`); } finally { setLoading(false); } }; const handleConnect = async (values: { platform: Platform; appId: string; appSecret: string; config?: Record }) => { try { const tenantId = localStorage.getItem('tenantId') || 'default'; const result = await platformAuthDataSource.connect({ platform: values.platform, accountName: values.appId, accountId: values.appId, config: { appId: values.appId, appSecret: values.appSecret, ...values.config, }, }); if (result.authUrl) { message.success('正在跳转到授权页面...'); window.open(result.authUrl, '_blank'); } setModalVisible(false); form.resetFields(); loadData(); } catch (error: any) { message.error(`连接失败: ${error.message}`); } }; const handleDisconnect = async (id: string) => { Modal.confirm({ title: '确认断开连接?', content: '断开后将无法同步该平台的数据', onOk: async () => { try { await platformAuthDataSource.disconnect(id); message.success('已断开连接'); loadData(); } catch (error: any) { message.error(`断开失败: ${error.message}`); } }, }); }; const handleRefresh = async (id: string) => { try { message.loading({ content: '正在刷新授权...', key: 'refresh' }); const result = await platformAuthDataSource.refresh(id); if (result.success) { message.success({ content: '授权已刷新', key: 'refresh' }); loadData(); } else { message.error({ content: result.message, key: 'refresh' }); } } catch (error: any) { message.error({ content: `刷新失败: ${error.message}`, key: 'refresh' }); } }; const handleDelete = async (id: string) => { Modal.confirm({ title: '确认删除?', content: '删除后需要重新授权才能使用该平台', okType: 'danger', onOk: async () => { try { await platformAuthDataSource.delete(id); message.success('已删除'); loadData(); } catch (error: any) { message.error(`删除失败: ${error.message}`); } }, }); }; const columns: ColumnsType = [ { title: '平台', dataIndex: 'platform', key: 'platform', width: 150, render: (platform: string) => { const config = PLATFORM_CONFIG[platform] || { color: 'default', text: platform, icon: }; return ( {config.icon} {config.text} ); }, }, { title: 'App ID', dataIndex: 'accountId', key: 'accountId', width: 200, }, { title: '配置状态', dataIndex: 'status', key: 'status', width: 120, render: (status: string) => { const config = STATUS_CONFIG[status] || { color: 'default', text: status }; return ; }, }, { title: '最后同步', dataIndex: 'lastSyncAt', key: 'lastSyncAt', width: 180, render: (lastSyncAt?: string) => lastSyncAt ? new Date(lastSyncAt).toLocaleString() : '-', }, { title: '授权过期', dataIndex: 'expiresAt', key: 'expiresAt', width: 180, render: (expiresAt?: string) => expiresAt ? new Date(expiresAt).toLocaleString() : '-', }, { title: '操作', key: 'action', width: 220, render: (_, record) => ( { record.status === 'active' && ( <> )} {record.status === 'expired' && ( <> )} {record.status === 'error' && ( <> { setModalVisible(false); form.resetFields(); }} onOk={() => { form.validateFields().then(values => { handleConnect(values); }); }} >
); }; export default PlatformAuth;