feat(distribution): 独立分销前台商城多租户体系、货源与订单数据严格隔离

This commit is contained in:
yml2213
2026-09-07 17:18:14 +08:00
parent 550b42eb22
commit ecaf15417b
34 changed files with 2397 additions and 119 deletions
+260 -39
View File
@@ -1,20 +1,32 @@
import { useEffect, useState } from 'react'
import { Card, Table, Typography, Space, Select, Tag, message } from 'antd'
import { listAccountDisplays, listSites, updateAccountDisplay, type AccountDisplay, type Site } from '../api'
import { useEffect, useState, useMemo } from 'react'
import { Card, Table, Typography, Space, Select, Tag, message, Button, Input, Modal } from 'antd'
import { SyncOutlined, SearchOutlined, CheckCircleOutlined, StopOutlined } from '@ant-design/icons'
import {
listAccountDisplays,
listSites,
updateAccountDisplay,
syncSiteAccounts,
formatMoney,
type AccountDisplay,
type Site,
} from '../api'
const STATUS_LABELS: Record<string, string> = {
enabled: '展示中',
disabled: '已下架',
sold: '已售出',
offline: '已离线',
const STATUS_LABELS: Record<string, { label: string; color: string }> = {
enabled: { label: '展示中', color: 'success' },
disabled: { label: '已下架', color: 'default' },
sold: { label: '已售出', color: 'blue' },
offline: { label: '已离线', color: 'error' },
}
export default function Accounts() {
const [sites, setSites] = useState<Site[]>([])
const [siteID, setSiteID] = useState<number>()
const [status, setStatus] = useState<string>('')
const [keyword, setKeyword] = useState<string>('')
const [items, setItems] = useState<AccountDisplay[]>([])
const [loading, setLoading] = useState(false)
const [syncing, setSyncing] = useState(false)
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([])
async function load(site?: number, statusFilter?: string) {
if (!site) return
@@ -40,6 +52,27 @@ export default function Accounts() {
.catch(() => message.error('站点加载失败'))
}, [])
async function handleSync() {
if (!siteID) {
message.warning('请先选择分销站点')
return
}
setSyncing(true)
try {
const res = await syncSiteAccounts(siteID)
message.success(
`同步完成!新增可售账号 ${res.synced_count} 个,已存在 ${res.skipped_count} 个(主站当前在售总数 ${res.total_active})`,
5,
)
await load(siteID, status)
} catch (error: unknown) {
const respMessage = (error as { response?: { data?: { message?: string } } })?.response?.data?.message
message.error(respMessage ?? '同步主站商品失败')
} finally {
setSyncing(false)
}
}
async function toggle(item: AccountDisplay) {
const next = item.display_status === 'enabled' ? 'disabled' : 'enabled'
try {
@@ -52,49 +85,237 @@ export default function Accounts() {
}
}
async function handleBatchUpdate(targetStatus: 'enabled' | 'disabled') {
if (selectedRowKeys.length === 0) {
message.warning('请先勾选需要批量操作的账号')
return
}
const actionLabel = targetStatus === 'enabled' ? '批量恢复展示' : '批量下架'
Modal.confirm({
title: `确认${actionLabel}?`,
content: `即将对选中的 ${selectedRowKeys.length} 个账号执行${actionLabel}操作。`,
onOk: async () => {
let successCount = 0
for (const id of selectedRowKeys) {
try {
await updateAccountDisplay(Number(id), targetStatus)
successCount++
} catch {
// continue next
}
}
message.success(`已完成 ${successCount}/${selectedRowKeys.length} 个账号的${actionLabel}`)
setSelectedRowKeys([])
if (siteID) void load(siteID, status)
},
})
}
const filteredItems = useMemo(() => {
const kw = keyword.trim().toLowerCase()
if (!kw) return items
return items.filter(
(item) =>
(item.title && item.title.toLowerCase().includes(kw)) ||
(item.listing_no && item.listing_no.toLowerCase().includes(kw)) ||
(item.game_name && item.game_name.toLowerCase().includes(kw)) ||
(item.server_region && item.server_region.toLowerCase().includes(kw)),
)
}, [items, keyword])
const currentSite = sites.find((s) => s.id === siteID)
return (
<Space direction="vertical" className="page">
<Typography.Title level={3}>账号展示</Typography.Title>
<Space>
<Select
placeholder="分销网站"
style={{ width: 200 }}
value={siteID}
options={sites.map((site) => ({ label: site.site_name, value: site.id }))}
onChange={(value) => {
setSiteID(value)
void load(value, status)
}}
/>
<Select
allowClear
placeholder="展示状态"
style={{ width: 140 }}
options={Object.entries(STATUS_LABELS).map(([value, label]) => ({ label, value }))}
onChange={(value) => {
setStatus(value ?? '')
void load(siteID, value ?? '')
}}
/>
</Space>
<Space direction="vertical" className="page" style={{ width: '100%' }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<div>
<Typography.Title level={3} style={{ marginBottom: 4 }}>
商品与账号展示
</Typography.Title>
<Typography.Text type="secondary">
管理当前分销网站对外展示与销售的游戏账号商品,支持实时从主站同步最新上架货源。
</Typography.Text>
</div>
<Button
type="primary"
icon={<SyncOutlined spin={syncing} />}
loading={syncing}
onClick={handleSync}
style={{ background: '#1677ff' }}
>
一键同步主站商品
</Button>
</div>
<Card size="small">
<Space wrap style={{ width: '100%', justifyContent: 'space-between' }}>
<Space wrap>
<Select
placeholder="分销网站"
style={{ width: 220 }}
value={siteID}
options={sites.map((site) => ({
label: `${site.site_name} (${site.site_code})`,
value: site.id,
}))}
onChange={(value) => {
setSiteID(value)
setSelectedRowKeys([])
void load(value, status)
}}
/>
<Select
allowClear
placeholder="展示状态"
style={{ width: 140 }}
value={status || undefined}
options={Object.entries(STATUS_LABELS).map(([value, conf]) => ({
label: conf.label,
value,
}))}
onChange={(value) => {
setStatus(value ?? '')
void load(siteID, value ?? '')
}}
/>
<Input
placeholder="搜索商品编号/标题/区服"
prefix={<SearchOutlined />}
style={{ width: 240 }}
allowClear
value={keyword}
onChange={(e) => setKeyword(e.target.value)}
/>
</Space>
{selectedRowKeys.length > 0 && (
<Space>
<Typography.Text type="secondary">已勾选 {selectedRowKeys.length} 项</Typography.Text>
<Button
size="small"
icon={<CheckCircleOutlined />}
onClick={() => handleBatchUpdate('enabled')}
>
批量展示
</Button>
<Button
size="small"
danger
icon={<StopOutlined />}
onClick={() => handleBatchUpdate('disabled')}
>
批量下架
</Button>
</Space>
)}
</Space>
</Card>
<Card>
<Table
rowKey="id"
loading={loading}
dataSource={items}
pagination={{ pageSize: 20 }}
dataSource={filteredItems}
rowSelection={{
selectedRowKeys,
onChange: setSelectedRowKeys,
}}
pagination={{
pageSize: 20,
showTotal: (total) => `共 ${total} 个账号商品`,
showQuickJumper: true,
}}
columns={[
{ title: '账号 ID', dataIndex: 'account_id' },
{ title: '商品 ID', dataIndex: 'listing_id' },
{
title: '展示状态',
title: '商品编号',
dataIndex: 'listing_no',
width: 140,
render: (v: string | undefined, row: AccountDisplay) => (
<Space direction="vertical" size={2}>
<Typography.Text copyable strong>
{v || `#${row.listing_id}`}
</Typography.Text>
{row.in_transaction && <Tag color="warning">出租中</Tag>}
</Space>
),
},
{
title: '账号信息',
dataIndex: 'title',
minWidth: 240,
render: (_: unknown, row: AccountDisplay) => (
<div>
<Typography.Text strong style={{ display: 'block', fontSize: 14 }}>
{row.title || `账号 #${row.account_id}`}
</Typography.Text>
<Space size={4} style={{ marginTop: 4 }}>
<Tag color="blue">{row.game_name || '默认游戏'}</Tag>
{row.server_region && <Tag>{row.server_region}</Tag>}
{row.login_platform && <Tag>{row.login_platform}</Tag>}
</Space>
</div>
),
},
{
title: '段位 / 资产',
width: 140,
render: (_: unknown, row: AccountDisplay) => (
<div>
<div>{row.rank_level ? <Tag color="purple">{row.rank_level}</Tag> : '-'}</div>
{row.haf_coin_amount !== undefined && row.haf_coin_amount > 0 && (
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
哈夫币: {row.haf_coin_amount.toLocaleString()}
</Typography.Text>
)}
</div>
),
},
{
title: '租金 / 押金',
width: 160,
render: (_: unknown, row: AccountDisplay) => (
<div>
<Typography.Text strong style={{ color: '#cf1322' }}>
{formatMoney(row.price_cent)}
<span style={{ fontSize: 12, fontWeight: 'normal', color: '#888' }}> /小时</span>
</Typography.Text>
<div style={{ fontSize: 12, color: '#666' }}>
押金: {formatMoney(row.deposit_amount_cent)}
</div>
</div>
),
},
{
title: '主站状态',
dataIndex: 'listing_status',
width: 110,
render: (v: string | undefined) => (
<Tag color={v === 'published' ? 'green' : 'default'}>
{v === 'published' ? '主站在售' : v || '未知'}
</Tag>
),
},
{
title: '分销展示',
dataIndex: 'display_status',
render: (v: string) => <Tag color={v === 'enabled' ? 'green' : 'default'}>{STATUS_LABELS[v] ?? v}</Tag>,
width: 110,
render: (v: string) => {
const conf = STATUS_LABELS[v] ?? { label: v, color: 'default' }
return <Tag color={conf.color}>{conf.label}</Tag>
},
},
{
title: '操作',
width: 120,
render: (_: unknown, row: AccountDisplay) => (
<a onClick={() => toggle(row)}>{row.display_status === 'enabled' ? '下架' : '恢复展示'}</a>
<Button
type="link"
size="small"
danger={row.display_status === 'enabled'}
onClick={() => toggle(row)}
>
{row.display_status === 'enabled' ? '下架' : '恢复展示'}
</Button>
),
},
]}