129 lines
4.4 KiB
TypeScript
129 lines
4.4 KiB
TypeScript
import { useEffect, useState } from 'react'
|
|
import { Card, Table, Typography, Space, Select, Tag, message } from 'antd'
|
|
import { listRules, listSites, updateRuleStatus, getMe, type Rule, type Site } from '../api'
|
|
import { useMember } from '../MemberContext'
|
|
|
|
const MODE_LABELS: Record<string, string> = {
|
|
two_party_distributor_receive: '两方-分销收号',
|
|
two_party_platform_receive: '两方-分销卖出',
|
|
three_party: '三方',
|
|
}
|
|
|
|
export default function Rules() {
|
|
const member = useMember()
|
|
const [sites, setSites] = useState<Site[]>([])
|
|
const [siteID, setSiteID] = useState<number | undefined>(member?.site_id)
|
|
const [rules, setRules] = useState<Rule[]>([])
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
async function load(site?: number) {
|
|
setLoading(true)
|
|
try {
|
|
setRules(await listRules(site))
|
|
} catch {
|
|
message.error('规则加载失败')
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
async function init() {
|
|
let currentMember = member
|
|
if (!currentMember) {
|
|
try {
|
|
currentMember = await getMe()
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
const activeSiteID = currentMember?.site_id
|
|
if (activeSiteID) {
|
|
setSiteID(activeSiteID)
|
|
void load(activeSiteID)
|
|
}
|
|
|
|
if (currentMember?.is_superadmin) {
|
|
try {
|
|
const rows = await listSites()
|
|
setSites(rows)
|
|
if (!activeSiteID) {
|
|
void load(undefined)
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
}
|
|
void init()
|
|
}, [member])
|
|
|
|
async function toggleStatus(rule: Rule) {
|
|
const next = rule.status === 'active' ? 'disabled' : 'active'
|
|
try {
|
|
await updateRuleStatus(rule.id, next)
|
|
message.success(`规则已${next === 'active' ? '启用' : '停用'}`)
|
|
void load(siteID)
|
|
} catch {
|
|
message.error('状态更新失败')
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Space direction="vertical" className="page">
|
|
<Typography.Title level={3}>比例与税费规则</Typography.Title>
|
|
<Space>
|
|
{member?.is_superadmin && sites.length > 1 && (
|
|
<Select
|
|
allowClear
|
|
placeholder="分销网站"
|
|
style={{ width: 200 }}
|
|
value={siteID}
|
|
options={sites.map((site) => ({ label: site.site_name, value: site.id }))}
|
|
onChange={(value) => {
|
|
setSiteID(value)
|
|
void load(value)
|
|
}}
|
|
/>
|
|
)}
|
|
</Space>
|
|
<Card>
|
|
<Table
|
|
rowKey="id"
|
|
loading={loading}
|
|
dataSource={rules}
|
|
pagination={false}
|
|
columns={[
|
|
{ title: '模式', dataIndex: 'mode', render: (v: string) => MODE_LABELS[v] ?? v },
|
|
{ title: '版本', dataIndex: 'version' },
|
|
{ title: '状态', dataIndex: 'status', render: (v: string) => <Tag color={v === 'active' ? 'green' : 'default'}>{v}</Tag> },
|
|
{
|
|
title: '比例',
|
|
render: (_: unknown, r: Rule) => {
|
|
const parts: string[] = []
|
|
if (r.distributor_receive_ratio > 0) parts.push(`分销收 ${r.distributor_receive_ratio}%`)
|
|
if (r.platform_sell_ratio > 0) parts.push(`主站卖 ${r.platform_sell_ratio}%`)
|
|
if (r.distributor_sell_ratio > 0) parts.push(`分销卖 ${r.distributor_sell_ratio}%`)
|
|
if (r.platform_receive_ratio > 0) parts.push(`主站收 ${r.platform_receive_ratio}%`)
|
|
if (r.distributor_a_ratio > 0) parts.push(`分销A ${r.distributor_a_ratio}%`)
|
|
if (r.distributor_b_ratio > 0) parts.push(`分销B ${r.distributor_b_ratio}%`)
|
|
return parts.join(' / ') || '-'
|
|
},
|
|
},
|
|
{ title: '服务费', render: (_: unknown, r: Rule) => `${r.service_fee_value}%` },
|
|
{ title: '手续费', dataIndex: 'payment_fee_bps', render: (v: number) => `${v / 100}%` },
|
|
{ title: '税费', render: (_: unknown, r: Rule) => (r.tax_enabled ? `${r.tax_rate_bps / 100}%` : '关闭') },
|
|
{ title: '生效时间', dataIndex: 'effective_from' },
|
|
{
|
|
title: '操作',
|
|
render: (_: unknown, r: Rule) => (
|
|
<a onClick={() => toggleStatus(r)}>{r.status === 'active' ? '停用' : '启用'}</a>
|
|
),
|
|
},
|
|
]}
|
|
/>
|
|
</Card>
|
|
</Space>
|
|
)
|
|
}
|