功能:增加平台账号管理与管理员保护
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { Button, Popconfirm, Table, Tag, Typography, message } from 'antd'
|
||||
import { DeleteOutlined, ReloadOutlined } from '@ant-design/icons'
|
||||
import type { ColumnsType } from 'antd/es/table'
|
||||
import { userApi } from '../api'
|
||||
import { PageHeader } from '../components/PageHeader'
|
||||
import type { PageResult, User } from '../types'
|
||||
import { formatDateTime } from '../utils/time'
|
||||
import { useAuth } from '../store/auth'
|
||||
|
||||
export default function PlatformUsers() {
|
||||
const { user: currentUser } = useAuth()
|
||||
const [data, setData] = useState<PageResult<User>>({ list: [], total: 0, page: 1, size: 20 })
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const load = useCallback(async (page = data.page, size = data.size) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
setData(await userApi.list({ page, size }))
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '加载失败')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [data.page, data.size])
|
||||
|
||||
useEffect(() => {
|
||||
void load()
|
||||
}, [load])
|
||||
|
||||
const remove = async (record: User) => {
|
||||
try {
|
||||
await userApi.remove(record.id)
|
||||
message.success('账号已删除')
|
||||
load()
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '删除失败')
|
||||
}
|
||||
}
|
||||
|
||||
const columns: ColumnsType<User> = [
|
||||
{ title: '用户名', dataIndex: 'username', width: 220, render: (value) => <Typography.Text strong>{value}</Typography.Text> },
|
||||
{ title: '昵称', dataIndex: 'nickname', width: 180, render: (value) => value || '-' },
|
||||
{ title: '账号类型', dataIndex: 'role', width: 140, render: (value) => <Tag color={value === 'admin' ? 'blue' : 'default'}>{value === 'admin' ? '平台管理员' : '商户账号'}</Tag> },
|
||||
{ title: '状态', dataIndex: 'status', width: 100, render: (value) => value === 1 ? <Tag color="green">启用</Tag> : <Tag>停用</Tag> },
|
||||
{ title: '创建时间', dataIndex: 'created_at', width: 180, render: formatDateTime },
|
||||
{
|
||||
title: '操作',
|
||||
width: 120,
|
||||
render: (_, record) => record.id === currentUser?.id ? <Typography.Text type="secondary">当前账号</Typography.Text> : (
|
||||
<Popconfirm title={`确认删除账号「${record.username}」?`} description="账号及其全部商户成员关系会被删除。" onConfirm={() => remove(record)} okButtonProps={{ danger: true }}>
|
||||
<Button type="link" danger size="small" icon={<DeleteOutlined />}>删除</Button>
|
||||
</Popconfirm>
|
||||
),
|
||||
},
|
||||
]
|
||||
|
||||
return (
|
||||
<div>
|
||||
<PageHeader title="账号管理" subtitle="管理平台管理员与商户登录账号;删除会同时解除该账号的商户成员关系。" breadcrumbs={[{ title: '账号管理' }]} extra={<Button icon={<ReloadOutlined />} loading={loading} onClick={() => load()}>刷新</Button>} />
|
||||
<Table rowKey="id" loading={loading} columns={columns} dataSource={data.list} pagination={{ current: data.page, pageSize: data.size, total: data.total, showSizeChanger: true, onChange: (page, size) => load(page, size) }} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user