实现 P2 管理端:租户生命周期、套餐与运维真实 API
- 租户开通自动创建管理员账号与网页渠道,支持暂停/恢复/编辑 - 运营概览返回月收入估算、套餐分布与真实操作日志 - 套餐上下架/新建,公告与操作日志前后端打通 - 补充管理端生命周期集成测试
This commit is contained in:
+212
-34
@@ -1,7 +1,13 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { Table, Input, Select, Tag, Button, Drawer, Form, InputNumber, Space, message, Descriptions, Empty } from 'antd'
|
||||
import {
|
||||
Table, Input, Select, Tag, Button, Drawer, Form, InputNumber, Space, message,
|
||||
Descriptions, Empty, Modal, Popconfirm,
|
||||
} from 'antd'
|
||||
import { PlusOutlined, SearchOutlined, ReloadOutlined, PauseCircleOutlined, EyeOutlined } from '@ant-design/icons'
|
||||
import { getTenants, type Tenant } from '@/services/api'
|
||||
import {
|
||||
createTenant, getPlans, getTenants, resumeTenant, suspendTenant, updateTenant,
|
||||
type Plan, type Tenant,
|
||||
} from '@/services/api'
|
||||
|
||||
const statusConfig: Record<string, { color: string; text: string }> = {
|
||||
normal: { color: 'green', text: '正常' },
|
||||
@@ -12,6 +18,7 @@ const statusConfig: Record<string, { color: string; text: string }> = {
|
||||
|
||||
const Tenants = () => {
|
||||
const [tenants, setTenants] = useState<Tenant[]>([])
|
||||
const [plans, setPlans] = useState<Plan[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [total, setTotal] = useState(0)
|
||||
const [page, setPage] = useState(1)
|
||||
@@ -20,73 +27,244 @@ const Tenants = () => {
|
||||
const [drawerOpen, setDrawerOpen] = useState(false)
|
||||
const [detailOpen, setDetailOpen] = useState(false)
|
||||
const [selectedTenant, setSelectedTenant] = useState<Tenant | null>(null)
|
||||
const [form] = Form.useForm()
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [createdCreds, setCreatedCreds] = useState<{ username: string; password: string; name: string } | null>(null)
|
||||
const [createForm] = Form.useForm()
|
||||
const [editForm] = Form.useForm()
|
||||
|
||||
useEffect(() => { loadTenants() }, [page, search, statusFilter])
|
||||
useEffect(() => {
|
||||
getPlans().then(res => setPlans(Array.isArray(res.data) ? res.data.filter(p => p.status === 'active') : [])).catch(() => setPlans([]))
|
||||
}, [])
|
||||
|
||||
const loadTenants = async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const res = await getTenants({ search, status: statusFilter, page })
|
||||
const res = await getTenants({ search, status: statusFilter, page, pageSize: 10 })
|
||||
setTenants(res.list)
|
||||
setTotal(res.total)
|
||||
} catch { setTenants([]) } finally { setLoading(false) }
|
||||
} catch {
|
||||
setTenants([])
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const planName = (planId: number | null) => {
|
||||
if (!planId) return '—'
|
||||
return plans.find(p => p.id === planId)?.name || `#${planId}`
|
||||
}
|
||||
|
||||
const handleCreate = async (values: {
|
||||
name: string; contact_name: string; contact_phone: string; contact_email?: string
|
||||
plan_id?: number; seat_count?: number; duration_months?: number
|
||||
}) => {
|
||||
setSaving(true)
|
||||
try {
|
||||
const res = await createTenant({
|
||||
name: values.name.trim(),
|
||||
contact_name: values.contact_name.trim(),
|
||||
contact_phone: values.contact_phone.trim(),
|
||||
contact_email: values.contact_email?.trim() || '',
|
||||
plan_id: values.plan_id,
|
||||
seat_count: values.seat_count,
|
||||
duration_months: values.duration_months || 12,
|
||||
})
|
||||
message.success('租户开通成功')
|
||||
setDrawerOpen(false)
|
||||
createForm.resetFields()
|
||||
setCreatedCreds({
|
||||
username: res.data.admin_username,
|
||||
password: res.data.admin_password,
|
||||
name: res.data.tenant.name,
|
||||
})
|
||||
setPage(1)
|
||||
await loadTenants()
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '开通失败')
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleSuspend = async (id: number) => {
|
||||
try {
|
||||
await suspendTenant(id)
|
||||
message.success('已暂停')
|
||||
await loadTenants()
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '暂停失败')
|
||||
}
|
||||
}
|
||||
|
||||
const handleResume = async (id: number) => {
|
||||
try {
|
||||
await resumeTenant(id)
|
||||
message.success('已恢复')
|
||||
await loadTenants()
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '恢复失败')
|
||||
}
|
||||
}
|
||||
|
||||
const handleUpdateSeats = async () => {
|
||||
if (!selectedTenant) return
|
||||
try {
|
||||
const values = await editForm.validateFields()
|
||||
const res = await updateTenant(selectedTenant.id, {
|
||||
contact_name: values.edit_contact_name,
|
||||
contact_phone: values.edit_contact_phone,
|
||||
contact_email: values.edit_contact_email,
|
||||
seat_count: values.edit_seat_count,
|
||||
})
|
||||
message.success('已更新')
|
||||
setSelectedTenant(res.data)
|
||||
await loadTenants()
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.message) message.error(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
const columns = [
|
||||
{ title: '公司名称', dataIndex: 'name', key: 'name', render: (t: string) => <span className="text-sm font-medium text-neutral-800">{t}</span> },
|
||||
{ title: '联系人', dataIndex: 'contact_name', key: 'contact_name' },
|
||||
{ title: '手机号', dataIndex: 'contact_phone', key: 'contact_phone', render: (t: string) => <span className="text-neutral-500">{t}</span> },
|
||||
{
|
||||
title: '套餐', key: 'plan',
|
||||
render: (_: unknown, r: Tenant) => <span className="text-sm text-neutral-600">{planName(r.plan_id)}</span>,
|
||||
},
|
||||
{ title: '坐席数', dataIndex: 'seat_count', key: 'seat_count', align: 'center' as const },
|
||||
{ title: '到期日期', dataIndex: 'expire_at', key: 'expire_at', render: (t: string) => <span className="text-xs text-neutral-500">{t ? new Date(t).toLocaleDateString() : '-'}</span> },
|
||||
{ title: '状态', dataIndex: 'status', key: 'status', render: (s: string) => <Tag color={statusConfig[s]?.color}>{statusConfig[s]?.text || s}</Tag> },
|
||||
{ title: '操作', key: 'actions', width: 200, render: (_: unknown, record: Tenant) => (
|
||||
<Space size={0}>
|
||||
<Button type="link" size="small" icon={<EyeOutlined />} onClick={(e) => { e.stopPropagation(); setSelectedTenant(record); setDetailOpen(true) }}>查看</Button>
|
||||
{record.status === 'normal' && <Button type="link" size="small" danger icon={<PauseCircleOutlined />} onClick={(e) => { e.stopPropagation(); message.info('暂停') }}>暂停</Button>}
|
||||
{(record.status === 'suspended' || record.status === 'expired') && <Button type="link" size="small" icon={<ReloadOutlined />} onClick={(e) => { e.stopPropagation(); message.info('恢复') }}>恢复</Button>}
|
||||
</Space>
|
||||
)},
|
||||
{
|
||||
title: '到期日期', dataIndex: 'expire_at', key: 'expire_at',
|
||||
render: (t: string) => <span className="text-xs text-neutral-500">{t ? new Date(t).toLocaleDateString() : '—'}</span>,
|
||||
},
|
||||
{
|
||||
title: '状态', dataIndex: 'status', key: 'status',
|
||||
render: (s: string) => <Tag color={statusConfig[s]?.color}>{statusConfig[s]?.text || s}</Tag>,
|
||||
},
|
||||
{
|
||||
title: '操作', key: 'actions', width: 220,
|
||||
render: (_: unknown, record: Tenant) => (
|
||||
<Space size={0}>
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
icon={<EyeOutlined />}
|
||||
onClick={e => {
|
||||
e.stopPropagation()
|
||||
setSelectedTenant(record)
|
||||
editForm.setFieldsValue({
|
||||
edit_contact_name: record.contact_name,
|
||||
edit_contact_phone: record.contact_phone,
|
||||
edit_contact_email: record.contact_email,
|
||||
edit_seat_count: record.seat_count,
|
||||
})
|
||||
setDetailOpen(true)
|
||||
}}
|
||||
>
|
||||
查看
|
||||
</Button>
|
||||
{record.status !== 'suspended' && (
|
||||
<Popconfirm title="确认暂停该租户?" onConfirm={e => { e?.stopPropagation(); handleSuspend(record.id) }}>
|
||||
<Button type="link" size="small" danger icon={<PauseCircleOutlined />} onClick={e => e.stopPropagation()}>暂停</Button>
|
||||
</Popconfirm>
|
||||
)}
|
||||
{(record.status === 'suspended' || record.status === 'expired') && (
|
||||
<Button type="link" size="small" icon={<ReloadOutlined />} onClick={e => { e.stopPropagation(); handleResume(record.id) }}>恢复</Button>
|
||||
)}
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
]
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h2 className="text-lg font-semibold text-neutral-800">租户管理</h2>
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={() => { form.resetFields(); setDrawerOpen(true) }}>开通新账号</Button>
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={() => { createForm.resetFields(); createForm.setFieldsValue({ duration_months: 12, seat_count: 5 }); setDrawerOpen(true) }}>
|
||||
开通新账号
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex gap-3 mb-3 flex-wrap">
|
||||
<Input prefix={<SearchOutlined />} placeholder="搜索公司名称或联系人" value={search} onChange={e => { setSearch(e.target.value); setPage(1) }} className="w-56" allowClear />
|
||||
<Select placeholder="状态筛选" value={statusFilter} onChange={v => { setStatusFilter(v); setPage(1) }} allowClear className="w-28" options={Object.entries(statusConfig).map(([k, v]) => ({ value: k, label: v.text }))} />
|
||||
</div>
|
||||
<div className="bg-white rounded-lg border border-neutral-200 overflow-hidden">
|
||||
<Table dataSource={tenants} columns={columns} rowKey="id" size="middle" loading={loading}
|
||||
<Table
|
||||
dataSource={tenants}
|
||||
columns={columns}
|
||||
rowKey="id"
|
||||
size="middle"
|
||||
loading={loading}
|
||||
pagination={{ current: page, total, pageSize: 10, showTotal: t => `共 ${t} 个租户`, onChange: p => setPage(p) }}
|
||||
locale={{ emptyText: <Empty description="暂无租户" /> }} />
|
||||
locale={{ emptyText: <Empty description="暂无租户" /> }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Drawer title="开通新账号" open={drawerOpen} onClose={() => setDrawerOpen(false)} width={480}>
|
||||
<Form form={form} layout="vertical" onFinish={() => { setDrawerOpen(false); message.success('开通成功') }}>
|
||||
<Form.Item name="company" label="公司名称" rules={[{ required: true }]}><Input /></Form.Item>
|
||||
<Form.Item name="contact" label="联系人" rules={[{ required: true }]}><Input /></Form.Item>
|
||||
<Form.Item name="phone" label="手机号" rules={[{ required: true }]}><Input /></Form.Item>
|
||||
<Form.Item name="email" label="邮箱"><Input /></Form.Item>
|
||||
<Form.Item name="seats" label="坐席数量"><InputNumber min={1} className="w-full" /></Form.Item>
|
||||
<Form.Item name="duration" label="开通时长(月)"><InputNumber min={1} max={36} className="w-full" /></Form.Item>
|
||||
<Form.Item><Button type="primary" htmlType="submit" block>确认开通</Button></Form.Item>
|
||||
<Form form={createForm} layout="vertical" onFinish={handleCreate}>
|
||||
<Form.Item name="name" label="公司名称" rules={[{ required: true }, { min: 2, max: 100 }]}><Input /></Form.Item>
|
||||
<Form.Item name="contact_name" label="联系人" rules={[{ required: true }]}><Input /></Form.Item>
|
||||
<Form.Item name="contact_phone" label="手机号" rules={[{ required: true }, { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' }]}><Input /></Form.Item>
|
||||
<Form.Item name="contact_email" label="邮箱" rules={[{ type: 'email', message: '邮箱格式不正确' }]}><Input /></Form.Item>
|
||||
<Form.Item name="plan_id" label="套餐">
|
||||
<Select allowClear placeholder="选择套餐" options={plans.map(p => ({ value: p.id, label: `${p.name} · ¥${p.price_monthly}/月` }))} />
|
||||
</Form.Item>
|
||||
<Form.Item name="seat_count" label="坐席数量"><InputNumber min={1} max={500} className="w-full" /></Form.Item>
|
||||
<Form.Item name="duration_months" label="开通时长(月)"><InputNumber min={1} max={36} className="w-full" /></Form.Item>
|
||||
<Form.Item>
|
||||
<Button type="primary" htmlType="submit" block loading={saving}>确认开通</Button>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Drawer>
|
||||
<Drawer title="租户详情" open={detailOpen} onClose={() => setDetailOpen(false)} width={400}>
|
||||
|
||||
<Drawer
|
||||
title="租户详情"
|
||||
open={detailOpen}
|
||||
onClose={() => setDetailOpen(false)}
|
||||
width={420}
|
||||
extra={<Button type="primary" size="small" onClick={handleUpdateSeats}>保存修改</Button>}
|
||||
>
|
||||
{selectedTenant && (
|
||||
<Descriptions column={1} size="small" colon={false} className="mt-4">
|
||||
<Descriptions.Item label="公司名称">{selectedTenant.name}</Descriptions.Item>
|
||||
<Descriptions.Item label="联系人">{selectedTenant.contact_name}</Descriptions.Item>
|
||||
<Descriptions.Item label="手机号">{selectedTenant.contact_phone}</Descriptions.Item>
|
||||
<Descriptions.Item label="邮箱">{selectedTenant.contact_email}</Descriptions.Item>
|
||||
<Descriptions.Item label="坐席数">{selectedTenant.seat_count}</Descriptions.Item>
|
||||
<Descriptions.Item label="到期日期">{selectedTenant.expire_at ? new Date(selectedTenant.expire_at).toLocaleDateString() : '-'}</Descriptions.Item>
|
||||
</Descriptions>
|
||||
<div className="space-y-4 mt-2">
|
||||
<Descriptions column={1} size="small" colon={false}>
|
||||
<Descriptions.Item label="公司名称">{selectedTenant.name}</Descriptions.Item>
|
||||
<Descriptions.Item label="套餐">{planName(selectedTenant.plan_id)}</Descriptions.Item>
|
||||
<Descriptions.Item label="状态">
|
||||
<Tag color={statusConfig[selectedTenant.status]?.color}>{statusConfig[selectedTenant.status]?.text || selectedTenant.status}</Tag>
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="到期日期">
|
||||
{selectedTenant.expire_at ? new Date(selectedTenant.expire_at).toLocaleDateString() : '—'}
|
||||
</Descriptions.Item>
|
||||
</Descriptions>
|
||||
<Form form={editForm} layout="vertical">
|
||||
<Form.Item name="edit_contact_name" label="联系人"><Input /></Form.Item>
|
||||
<Form.Item name="edit_contact_phone" label="手机号"><Input /></Form.Item>
|
||||
<Form.Item name="edit_contact_email" label="邮箱"><Input /></Form.Item>
|
||||
<Form.Item name="edit_seat_count" label="坐席数"><InputNumber min={1} className="w-full" /></Form.Item>
|
||||
</Form>
|
||||
</div>
|
||||
)}
|
||||
</Drawer>
|
||||
|
||||
<Modal
|
||||
title="开通成功"
|
||||
open={Boolean(createdCreds)}
|
||||
onCancel={() => setCreatedCreds(null)}
|
||||
onOk={() => setCreatedCreds(null)}
|
||||
okText="知道了"
|
||||
cancelButtonProps={{ style: { display: 'none' } }}
|
||||
>
|
||||
{createdCreds && (
|
||||
<div className="space-y-2 text-sm">
|
||||
<p className="m-0">租户 <strong>{createdCreds.name}</strong> 已开通,请妥善保存登录信息:</p>
|
||||
<div className="bg-neutral-50 border border-neutral-200 rounded-lg p-3">
|
||||
<div>管理员账号:<code>{createdCreds.username}</code></div>
|
||||
<div>初始密码:<code>{createdCreds.password}</code></div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user