用户模块

This commit is contained in:
mmc
2026-04-29 12:49:47 +08:00
parent e5360f49b6
commit 935b2eb46a
5 changed files with 273 additions and 220 deletions

54
src/api/user/index.ts Normal file
View File

@@ -0,0 +1,54 @@
import request from '../request';
export function addUser(data) {
return request({
url: '/system/user',
method: 'post',
data
});
}
export function deleteUser(data) {
return request({
url: `/system/user/${data}`,
method: 'delete',
});
}
export function editUser(data) {
return request({
url: `/system/user`,
method: 'put',
data
});
}
export function getUser(params) {
return request({
url: `/system/user/list`,
method: 'get',
params
});
}
//通过角色查找 用户
export function getUserByRole(params) {
return request({
url: `/system/user/getByRoleId`,
method: 'get',
params
});
}

View File

@@ -1,20 +1,16 @@
import React from 'react'; import React from 'react';
import { Empty } from 'antd'; import { Empty } from 'antd';
import { FileTextOutlined } from '@ant-design/icons';
interface EmptyDataProps { interface EmptyDataProps {
/** 自定义文案 */ /** 自定义文案 */
description?: string; description?: string;
/** 组件高度 */ /** 组件高度 */
height?: number | string; height?: number | string;
/** 图标大小 */
iconSize?: number;
} }
const EmptyData: React.FC<EmptyDataProps> = ({ const EmptyData: React.FC<EmptyDataProps> = ({
description = '暂无数据', description = '暂无数据',
height = 220, height = 220
iconSize = 56
}) => { }) => {
return ( return (
<div <div
@@ -28,8 +24,8 @@ const EmptyData: React.FC<EmptyDataProps> = ({
}} }}
> >
<Empty <Empty
image={<FileTextOutlined style={{ fontSize: iconSize, color: '#c5c8ce' }} />} image={Empty.PRESENTED_IMAGE_SIMPLE}
description={<span style={{ color: '#999', fontSize: 16 }}>{description}</span>} description={<span style={{ color: '#999', fontSize: 14 }}>{description}</span>}
/> />
</div> </div>
); );

View File

@@ -43,7 +43,7 @@ export default function AppLayout() {
useEffect(() => { useEffect(() => {
const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000); const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000);
getMenu(); //getMenu();
return () => clearInterval(timer); return () => clearInterval(timer);
}, []); }, []);

View File

@@ -4,7 +4,8 @@ import {
Input, Popconfirm, Divider, InputNumber, Switch, Pagination Input, Popconfirm, Divider, InputNumber, Switch, Pagination
} from 'antd'; } from 'antd';
import { import {
PlusOutlined, EditOutlined, DeleteOutlined, CloseOutlined PlusOutlined, EditOutlined, DeleteOutlined, CloseOutlined,
UserOutlined
} from '@ant-design/icons'; } from '@ant-design/icons';
import { getMenuList } from '../api/mune'; import { getMenuList } from '../api/mune';
import { buildPermTree, buildTreeFromFlat } from '../lib/utils'; import { buildPermTree, buildTreeFromFlat } from '../lib/utils';
@@ -191,9 +192,18 @@ export default function Roles() {
type="text" type="text"
icon={<EditOutlined />} icon={<EditOutlined />}
onClick={() => handleEditRole(role)} onClick={() => handleEditRole(role)}
style={{ color: '#1677ff' }}
> >
编辑 编辑
</Button>, </Button>,
<Button
type="text"
icon={<UserOutlined />}
onClick={() => console.log('管理成员', role.roleId)}
style={{ color: '#00b96b' }}
>
管理成员
</Button>,
<Popconfirm <Popconfirm
title="确定删除该角色?" title="确定删除该角色?"
onConfirm={() => handleDeleteRole(role.roleId)} onConfirm={() => handleDeleteRole(role.roleId)}
@@ -211,9 +221,11 @@ export default function Roles() {
<Card.Meta <Card.Meta
title={<span style={{ fontWeight: 600 }}>{role.roleName}</span>} title={<span style={{ fontWeight: 600 }}>{role.roleName}</span>}
description={ description={
<Space direction="vertical" size={2}> <Space direction="vertical" size={2} style={{ width: '100%' }}>
<Text type="secondary">权限标识:{role.roleKey}</Text> <Text type="secondary">权限标识:{role.roleKey}</Text>
<Text type="secondary">状态:{role.status == 1 ? '正常' : '停用'}</Text> <Text type="secondary">
状态:{role.status == 1 ? '正常' : '停用'}
</Text>
<Text type="secondary"> <Text type="secondary">
{role.admin && '【超级管理员】'} {role.admin && '【超级管理员】'}
{role.manager && '【机构管理员】'} {role.manager && '【机构管理员】'}
@@ -226,7 +238,7 @@ export default function Roles() {
)) ))
) : ( ) : (
<Col span={24}> <Col span={24}>
<EmptyData height={260} description="暂无角色数据" /> <EmptyData height={260} description="暂无数据" />
</Col> </Col>
)} )}
</Row> </Row>

View File

@@ -1,199 +1,156 @@
import React, { useState } from 'react'; import React, { useState, useEffect } from 'react';
import { import {
Card, Table, Button, Typography, Space, Modal, Form, Card, Table, Button, Typography, Space, Modal, Form,
Input, Select, Popconfirm, message, Tag, TreeSelect, Divider Input, Select, Popconfirm, message, Tag, Row, Col, Switch
} from 'antd'; } from 'antd';
import { import {
PlusOutlined, EditOutlined, DeleteOutlined, UserOutlined, PlusOutlined, EditOutlined, DeleteOutlined, UserOutlined
KeyOutlined, TeamOutlined
} from '@ant-design/icons'; } from '@ant-design/icons';
import { addUser, deleteUser, editUser, getUser } from '../api/user';
const { Title } = Typography; const { Title } = Typography;
const { Option } = Select; 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() { export default function UserList() {
const [userList, setUserList] = useState(initUserList); const [userList, setUserList] = useState([]);
const [loading, setLoading] = useState(false);
// 新增/编辑弹窗
const [modalVisible, setModalVisible] = useState(false); const [modalVisible, setModalVisible] = useState(false);
const [form] = Form.useForm(); const [form] = Form.useForm();
const [currentUser, setCurrentUser] = useState(null); const [currentUser, setCurrentUser] = useState(null);
const [messageApi, contextHolder] = message.useMessage();
// 打开新增 // 统一分页
const [pagination, setPagination] = useState({
pageNum: 1,
pageSize: 10,
total: 0
});
// 获取用户列表
const getUserList = () => {
const params = {
pageNum: pagination.pageNum,
pageSize: pagination.pageSize
};
getUser(params).then(res => {
if (res.code === 200) {
setUserList(res.rows || []);
setPagination({
...pagination,
total: res.total || res.rows?.length || 0
});
}
});
};
// 分页切换
const handleTableChange = (newPagination) => {
setPagination({
...pagination,
pageNum: newPagination.current,
pageSize: newPagination.pageSize
});
};
useEffect(() => {
getUserList();
}, [pagination.pageNum, pagination.pageSize]);
// 新增
const handleAdd = () => { const handleAdd = () => {
form.resetFields(); form.resetFields();
setCurrentUser(null); setCurrentUser(null);
setModalVisible(true); setModalVisible(true);
}; };
// 打开编辑 // 编辑
const handleEdit = (record) => { const handleEdit = (record) => {
form.setFieldsValue(record);
setCurrentUser(record); setCurrentUser(record);
form.setFieldsValue({
...record
});
setModalVisible(true); setModalVisible(true);
}; };
// 删除用户 // 删除
const handleDelete = (id) => { const handleDelete = (userId) => {
setUserList(userList.filter(u => u.id !== id)); deleteUser(userId).then(res => {
message.success('删除成功'); if (res.code === 200) {
messageApi.success('删除成功');
getUserList();
} else {
messageApi.error(res.msg || '删除失败');
}
});
}; };
// 提交保存 // 提交保存(完全匹配后端字段)
const handleSubmit = () => { const handleSubmit = () => {
form.validateFields().then(values => { form.validateFields().then(values => {
// 角色标签回填,方便表格展示 // 构建后端需要的完整参数
const roleInfo = roleOptions.find(r => r.value === values.role); const params = {
const payload = {
...values, ...values,
roleLabel: roleInfo?.label roleIds: values.roleId ? [values.roleId] : [] // 角色ID数组
}; };
if (currentUser) { if (currentUser) {
// 编辑 // 编辑
setUserList(userList.map(u => u.id === currentUser.id ? { ...u, ...payload } : u)); editUser(params).then(res => {
message.success('编辑成功'); if (res.code === 200) {
messageApi.success('编辑成功');
getUserList();
setModalVisible(false);
}
});
} else { } else {
// 新增 // 新增
const newUser = { ...payload, id: Date.now() }; addUser(params).then(res => {
setUserList([...userList, newUser]); if (res.code === 200) {
message.success('新增用户成功'); messageApi.success('新增成功');
getUserList();
setModalVisible(false);
}
});
} }
setModalVisible(false);
}); });
}; };
// 表格列(增强角色+数据权限展示) // 表格列
const columns = [ const columns = [
{ {
title: '用户信息', title: '用户账号',
key: 'userInfo', dataIndex: 'userName'
render: (_, record) => ( },
<Space> {
<UserOutlined style={{ fontSize: 16 }} /> title: '用户昵称',
<div> dataIndex: 'nickName'
<div style={{ fontWeight: 500 }}>{record.nickName}</div> },
<div style={{ fontSize: 12, color: '#888' }}>账号:{record.username}</div> {
</div> title: '手机号',
</Space> dataIndex: 'phonenumber'
) },
{
title: '邮箱',
dataIndex: 'email'
}, },
{ {
title: '角色', title: '角色',
dataIndex: 'roleLabel', dataIndex: 'roleNames',
key: 'roleLabel', render: (t) => <Tag color="blue">{t || '未分配'}</Tag>
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: '状态', title: '状态',
dataIndex: 'status', dataIndex: 'status',
key: 'status',
render: (s) => s === '0' ? render: (s) => s === '0' ?
<Tag color="green">正常</Tag> : <Tag color="green">正常</Tag> :
<Tag color="red">禁用</Tag> <Tag color="red">停用</Tag>
}, },
{ {
title: '操作', title: '操作',
key: 'action', render: (_, r) => (
render: (_, record) => (
<Space> <Space>
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑</Button> <Button type="text" icon={<EditOutlined />} onClick={() => handleEdit(r)}>编辑</Button>
<Popconfirm title="确定删除该用户?" onConfirm={() => handleDelete(record.id)}> <Popconfirm title="确定删除?" onConfirm={() => handleDelete(r.userId)}>
<Button type="link" danger icon={<DeleteOutlined />}>删除</Button> <Button type="text" danger>删除</Button>
</Popconfirm> </Popconfirm>
</Space> </Space>
) )
@@ -201,88 +158,122 @@ export default function UserList() {
]; ];
return ( return (
<Card> <div style={{ padding: 20 }}>
<div className="flex justify-between mb-4"> {contextHolder}
<Title level={5} style={{ margin: 0 }}>用户管理(角色+数据权限)</Title> <Card title="用户管理">
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>新增用户</Button> <div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between' }}>
</div> <Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>
新增用户
</Button>
</div>
<Table <Table
rowKey="id" rowKey="userId"
loading={loading} columns={columns}
columns={columns} dataSource={userList}
dataSource={userList} pagination={{
pagination={{ pageSize: 10 }} current: pagination.pageNum,
/> pageSize: pagination.pageSize,
total: pagination.total,
showSizeChanger: true,
showTotal: t => `共 ${t} 条`
}}
onChange={handleTableChange}
bordered
/>
</Card>
{/* 新增/编辑弹窗:角色+数据权限双配置 */} {/* 新增/编辑弹窗(完全匹配后端字段) */}
<Modal <Modal
title={currentUser ? '编辑用户' : '新增用户'} title={currentUser ? '编辑用户' : '新增用户'}
open={modalVisible} open={modalVisible}
width={650}
onCancel={() => setModalVisible(false)} onCancel={() => setModalVisible(false)}
onOk={handleSubmit} onOk={handleSubmit}
maskClosable={false} destroyOnClose
width={600}
> >
<Form form={form} layout="vertical"> <Form form={form} layout="vertical">
<Form.Item <Row gutter={16}>
name="username" <Col span={12}>
label="登录账号" <Form.Item name="userName" label="用户账号" rules={[{ required: true }]}>
rules={[{ required: true, message: '请输入账号' }]} <Input />
> </Form.Item>
<Input placeholder="请输入登录账号" /> </Col>
<Col span={12}>
<Form.Item name="nickName" label="用户昵称" rules={[{ required: true }]}>
<Input />
</Form.Item>
</Col>
</Row>
<Row gutter={16}>
<Col span={12}>
<Form.Item name="phonenumber" label="手机号码">
<Input />
</Form.Item>
</Col>
<Col span={12}>
<Form.Item name="email" label="用户邮箱">
<Input />
</Form.Item>
</Col>
</Row>
<Row gutter={16}>
<Col span={12}>
<Form.Item name="password" label="密码">
<Input.Password placeholder="不填则不修改" />
</Form.Item>
</Col>
<Col span={12}>
<Form.Item name="displayPassword" label="明文密码">
<Input />
</Form.Item>
</Col>
</Row>
<Row gutter={16}>
<Col span={12}>
<Form.Item name="roleId" label="分配角色">
<Input placeholder="角色ID" />
</Form.Item>
</Col>
<Col span={12}>
<Form.Item name="status" label="状态" initialValue="0">
<Select>
<Option value="0">正常</Option>
<Option value="1">停用</Option>
</Select>
</Form.Item>
</Col>
</Row>
<Row gutter={16}>
<Col span={12}>
<Form.Item name="deptId" label="部门ID">
<Input />
</Form.Item>
</Col>
<Col span={12}>
<Form.Item name="orgId" label="组织ID">
<Input />
</Form.Item>
</Col>
</Row>
<Form.Item name="siteId" label="场站ID">
<Input />
</Form.Item> </Form.Item>
<Form.Item <Form.Item name="sex" label="性别" initialValue="2">
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> <Select>
<Option value="0">正常</Option> <Option value="0">男</Option>
<Option value="1">禁用</Option> <Option value="1">女</Option>
<Option value="2">未知</Option>
</Select> </Select>
</Form.Item> </Form.Item>
</Form> </Form>
</Modal> </Modal>
</Card> </div>
); );
} }