Files
web-Iot/src/pages/Users.tsx
2026-04-24 15:28:16 +08:00

289 lines
7.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useState } from 'react';
import {
Card, Table, Button, Typography, Space, Modal, Form,
Input, Select, Popconfirm, message, Tag, TreeSelect, Divider
} from 'antd';
import {
PlusOutlined, EditOutlined, DeleteOutlined, UserOutlined,
KeyOutlined, TeamOutlined
} from '@ant-design/icons';
const { Title } = Typography;
const { Option } = Select;
// ---------------------- 1. 角色配置(和你的权限表对齐) ----------------------
const roleOptions = [
{
label: '平台admin',
value: 'admin',
desc: '全局配置、机构/菜单/设备权限分配、核心参数配置、平台级异常审核'
},
{
label: '区域机构管理员',
value: 'region_admin',
desc: '区域场站/账号管理、跨站机器人调度、区域报表统计'
},
{
label: '单个场站管理员',
value: 'site_admin',
desc: '场站配置、设备/机器人管理、场站告警处置、运维记录审核'
},
{
label: '场站员工',
value: 'site_staff',
desc: '机器人启停/参数调整、任务执行、巡检文件上传、故障处置'
},
];
// 模拟机构/场站数据(数据权限用)
const orgTreeData = [
{
title: '华东区域',
value: 'region_east',
children: [
{ title: '上海光伏场站', value: 'site_shanghai' },
{ title: '苏州风电场站', value: 'site_suzhou' },
]
},
{
title: '华北区域',
value: 'region_north',
children: [
{ title: '张家口光伏场站', value: 'site_zhangjiakou' },
]
}
];
// 模拟用户数据(带角色+数据权限)
const initUserList = [
{
id: 1,
username: 'admin',
nickName: '平台超级管理员',
role: 'admin',
roleLabel: '平台admin',
dataScope: ['region_east', 'region_north'],
status: '0'
},
{
id: 2,
username: 'region_shanghai',
nickName: '华东区域管理员',
role: 'region_admin',
roleLabel: '区域机构管理员',
dataScope: ['region_east'],
status: '0'
},
{
id: 3,
username: 'site_shanghai01',
nickName: '上海场站管理员',
role: 'site_admin',
roleLabel: '单个场站管理员',
dataScope: ['site_shanghai'],
status: '0'
},
{
id: 4,
username: 'staff_zhang',
nickName: '上海场站运维员',
role: 'site_staff',
roleLabel: '场站员工',
dataScope: ['site_shanghai'],
status: '0'
},
];
export default function UserList() {
const [userList, setUserList] = useState(initUserList);
const [loading, setLoading] = useState(false);
// 新增/编辑弹窗
const [modalVisible, setModalVisible] = useState(false);
const [form] = Form.useForm();
const [currentUser, setCurrentUser] = useState(null);
// 打开新增
const handleAdd = () => {
form.resetFields();
setCurrentUser(null);
setModalVisible(true);
};
// 打开编辑
const handleEdit = (record) => {
form.setFieldsValue(record);
setCurrentUser(record);
setModalVisible(true);
};
// 删除用户
const handleDelete = (id) => {
setUserList(userList.filter(u => u.id !== id));
message.success('删除成功');
};
// 提交保存
const handleSubmit = () => {
form.validateFields().then(values => {
// 角色标签回填,方便表格展示
const roleInfo = roleOptions.find(r => r.value === values.role);
const payload = {
...values,
roleLabel: roleInfo?.label
};
if (currentUser) {
// 编辑
setUserList(userList.map(u => u.id === currentUser.id ? { ...u, ...payload } : u));
message.success('编辑成功');
} else {
// 新增
const newUser = { ...payload, id: Date.now() };
setUserList([...userList, newUser]);
message.success('新增用户成功');
}
setModalVisible(false);
});
};
// 表格列(增强角色+数据权限展示)
const columns = [
{
title: '用户信息',
key: 'userInfo',
render: (_, record) => (
<Space>
<UserOutlined style={{ fontSize: 16 }} />
<div>
<div style={{ fontWeight: 500 }}>{record.nickName}</div>
<div style={{ fontSize: 12, color: '#888' }}>账号:{record.username}</div>
</div>
</Space>
)
},
{
title: '角色',
dataIndex: 'roleLabel',
key: 'roleLabel',
render: (text) => <Tag color="blue">{text}</Tag>
},
{
title: '数据权限',
dataIndex: 'dataScope',
key: 'dataScope',
render: (dataScope) => {
if (!dataScope?.length) return '-';
const count = dataScope.length;
return <Tag color="cyan">已分配 {count} 个机构/场站</Tag>;
}
},
{
title: '状态',
dataIndex: 'status',
key: 'status',
render: (s) => s === '0' ?
<Tag color="green">正常</Tag> :
<Tag color="red">禁用</Tag>
},
{
title: '操作',
key: 'action',
render: (_, record) => (
<Space>
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑</Button>
<Popconfirm title="确定删除该用户?" onConfirm={() => handleDelete(record.id)}>
<Button type="link" danger icon={<DeleteOutlined />}>删除</Button>
</Popconfirm>
</Space>
)
}
];
return (
<Card>
<div className="flex justify-between mb-4">
<Title level={5} style={{ margin: 0 }}>用户管理(角色+数据权限)</Title>
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>新增用户</Button>
</div>
<Table
rowKey="id"
loading={loading}
columns={columns}
dataSource={userList}
pagination={{ pageSize: 10 }}
/>
{/* 新增/编辑弹窗:角色+数据权限双配置 */}
<Modal
title={currentUser ? '编辑用户' : '新增用户'}
open={modalVisible}
onCancel={() => setModalVisible(false)}
onOk={handleSubmit}
maskClosable={false}
width={600}
>
<Form form={form} layout="vertical">
<Form.Item
name="username"
label="登录账号"
rules={[{ required: true, message: '请输入账号' }]}
>
<Input placeholder="请输入登录账号" />
</Form.Item>
<Form.Item
name="nickName"
label="用户昵称"
rules={[{ required: true, message: '请输入昵称' }]}
>
<Input placeholder="请输入用户昵称" />
</Form.Item>
<Divider orientation="left" style={{ margin: '16px 0' }}>权限配置</Divider>
<Form.Item
name="role"
label="分配角色"
rules={[{ required: true, message: '请选择角色' }]}
extra="角色决定菜单和操作权限,数据权限决定可见的机构/场站"
>
<Select placeholder="选择用户角色">
{roleOptions.map(item => (
<Option key={item.value} value={item.value}>{item.label}</Option>
))}
</Select>
</Form.Item>
<Form.Item
name="dataScope"
label="数据权限(可见机构/场站)"
rules={[{ required: true, message: '请选择数据权限' }]}
>
<TreeSelect
treeData={orgTreeData}
placeholder="选择用户可见的机构/场站"
multiple
treeCheckable
treeDefaultExpandAll
/>
</Form.Item>
<Form.Item
name="status"
label="用户状态"
rules={[{ required: true }]}
initialValue="0"
>
<Select>
<Option value="0">正常</Option>
<Option value="1">禁用</Option>
</Select>
</Form.Item>
</Form>
</Modal>
</Card>
);
}