用户模块
This commit is contained in:
54
src/api/user/index.ts
Normal file
54
src/api/user/index.ts
Normal 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
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
import React from 'react';
|
||||
import { Empty } from 'antd';
|
||||
import { FileTextOutlined } from '@ant-design/icons';
|
||||
|
||||
interface EmptyDataProps {
|
||||
/** 自定义文案 */
|
||||
description?: string;
|
||||
/** 组件高度 */
|
||||
height?: number | string;
|
||||
/** 图标大小 */
|
||||
iconSize?: number;
|
||||
}
|
||||
|
||||
const EmptyData: React.FC<EmptyDataProps> = ({
|
||||
description = '暂无数据',
|
||||
height = 220,
|
||||
iconSize = 56
|
||||
height = 220
|
||||
}) => {
|
||||
return (
|
||||
<div
|
||||
@@ -28,8 +24,8 @@ const EmptyData: React.FC<EmptyDataProps> = ({
|
||||
}}
|
||||
>
|
||||
<Empty
|
||||
image={<FileTextOutlined style={{ fontSize: iconSize, color: '#c5c8ce' }} />}
|
||||
description={<span style={{ color: '#999', fontSize: 16 }}>{description}</span>}
|
||||
image={Empty.PRESENTED_IMAGE_SIMPLE}
|
||||
description={<span style={{ color: '#999', fontSize: 14 }}>{description}</span>}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -43,7 +43,7 @@ export default function AppLayout() {
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000);
|
||||
getMenu();
|
||||
//getMenu();
|
||||
return () => clearInterval(timer);
|
||||
}, []);
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@ import {
|
||||
Input, Popconfirm, Divider, InputNumber, Switch, Pagination
|
||||
} from 'antd';
|
||||
import {
|
||||
PlusOutlined, EditOutlined, DeleteOutlined, CloseOutlined
|
||||
PlusOutlined, EditOutlined, DeleteOutlined, CloseOutlined,
|
||||
UserOutlined
|
||||
} from '@ant-design/icons';
|
||||
import { getMenuList } from '../api/mune';
|
||||
import { buildPermTree, buildTreeFromFlat } from '../lib/utils';
|
||||
@@ -191,9 +192,18 @@ export default function Roles() {
|
||||
type="text"
|
||||
icon={<EditOutlined />}
|
||||
onClick={() => handleEditRole(role)}
|
||||
style={{ color: '#1677ff' }}
|
||||
>
|
||||
编辑
|
||||
</Button>,
|
||||
<Button
|
||||
type="text"
|
||||
icon={<UserOutlined />}
|
||||
onClick={() => console.log('管理成员', role.roleId)}
|
||||
style={{ color: '#00b96b' }}
|
||||
>
|
||||
管理成员
|
||||
</Button>,
|
||||
<Popconfirm
|
||||
title="确定删除该角色?"
|
||||
onConfirm={() => handleDeleteRole(role.roleId)}
|
||||
@@ -211,9 +221,11 @@ export default function Roles() {
|
||||
<Card.Meta
|
||||
title={<span style={{ fontWeight: 600 }}>{role.roleName}</span>}
|
||||
description={
|
||||
<Space direction="vertical" size={2}>
|
||||
<Space direction="vertical" size={2} style={{ width: '100%' }}>
|
||||
<Text type="secondary">权限标识:{role.roleKey}</Text>
|
||||
<Text type="secondary">状态:{role.status == 1 ? '正常' : '停用'}</Text>
|
||||
<Text type="secondary">
|
||||
状态:{role.status == 1 ? '正常' : '停用'}
|
||||
</Text>
|
||||
<Text type="secondary">
|
||||
{role.admin && '【超级管理员】'}
|
||||
{role.manager && '【机构管理员】'}
|
||||
@@ -226,7 +238,7 @@ export default function Roles() {
|
||||
))
|
||||
) : (
|
||||
<Col span={24}>
|
||||
<EmptyData height={260} description="暂无角色数据" />
|
||||
<EmptyData height={260} description="暂无数据" />
|
||||
</Col>
|
||||
)}
|
||||
</Row>
|
||||
|
||||
@@ -1,199 +1,156 @@
|
||||
import React, { useState } from 'react';
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import {
|
||||
Card, Table, Button, Typography, Space, Modal, Form,
|
||||
Input, Select, Popconfirm, message, Tag, TreeSelect, Divider
|
||||
Input, Select, Popconfirm, message, Tag, Row, Col, Switch
|
||||
} from 'antd';
|
||||
import {
|
||||
PlusOutlined, EditOutlined, DeleteOutlined, UserOutlined,
|
||||
KeyOutlined, TeamOutlined
|
||||
PlusOutlined, EditOutlined, DeleteOutlined, UserOutlined
|
||||
} from '@ant-design/icons';
|
||||
import { addUser, deleteUser, editUser, getUser } from '../api/user';
|
||||
|
||||
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 [userList, setUserList] = useState([]);
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
const [form] = Form.useForm();
|
||||
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 = () => {
|
||||
form.resetFields();
|
||||
setCurrentUser(null);
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 打开编辑
|
||||
// 编辑
|
||||
const handleEdit = (record) => {
|
||||
form.setFieldsValue(record);
|
||||
setCurrentUser(record);
|
||||
form.setFieldsValue({
|
||||
...record
|
||||
});
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 删除用户
|
||||
const handleDelete = (id) => {
|
||||
setUserList(userList.filter(u => u.id !== id));
|
||||
message.success('删除成功');
|
||||
// 删除
|
||||
const handleDelete = (userId) => {
|
||||
deleteUser(userId).then(res => {
|
||||
if (res.code === 200) {
|
||||
messageApi.success('删除成功');
|
||||
getUserList();
|
||||
} else {
|
||||
messageApi.error(res.msg || '删除失败');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 提交保存
|
||||
// 提交保存(完全匹配后端字段)
|
||||
const handleSubmit = () => {
|
||||
form.validateFields().then(values => {
|
||||
// 角色标签回填,方便表格展示
|
||||
const roleInfo = roleOptions.find(r => r.value === values.role);
|
||||
const payload = {
|
||||
// 构建后端需要的完整参数
|
||||
const params = {
|
||||
...values,
|
||||
roleLabel: roleInfo?.label
|
||||
roleIds: values.roleId ? [values.roleId] : [] // 角色ID数组
|
||||
};
|
||||
|
||||
if (currentUser) {
|
||||
// 编辑
|
||||
setUserList(userList.map(u => u.id === currentUser.id ? { ...u, ...payload } : u));
|
||||
message.success('编辑成功');
|
||||
editUser(params).then(res => {
|
||||
if (res.code === 200) {
|
||||
messageApi.success('编辑成功');
|
||||
getUserList();
|
||||
setModalVisible(false);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// 新增
|
||||
const newUser = { ...payload, id: Date.now() };
|
||||
setUserList([...userList, newUser]);
|
||||
message.success('新增用户成功');
|
||||
addUser(params).then(res => {
|
||||
if (res.code === 200) {
|
||||
messageApi.success('新增成功');
|
||||
getUserList();
|
||||
setModalVisible(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
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: 'userName'
|
||||
},
|
||||
{
|
||||
title: '用户昵称',
|
||||
dataIndex: 'nickName'
|
||||
},
|
||||
{
|
||||
title: '手机号',
|
||||
dataIndex: 'phonenumber'
|
||||
},
|
||||
{
|
||||
title: '邮箱',
|
||||
dataIndex: 'email'
|
||||
},
|
||||
{
|
||||
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>;
|
||||
}
|
||||
dataIndex: 'roleNames',
|
||||
render: (t) => <Tag color="blue">{t || '未分配'}</Tag>
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
dataIndex: 'status',
|
||||
key: 'status',
|
||||
render: (s) => s === '0' ?
|
||||
<Tag color="green">正常</Tag> :
|
||||
<Tag color="red">禁用</Tag>
|
||||
<Tag color="red">停用</Tag>
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
render: (_, record) => (
|
||||
render: (_, r) => (
|
||||
<Space>
|
||||
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑</Button>
|
||||
<Popconfirm title="确定删除该用户?" onConfirm={() => handleDelete(record.id)}>
|
||||
<Button type="link" danger icon={<DeleteOutlined />}>删除</Button>
|
||||
<Button type="text" icon={<EditOutlined />} onClick={() => handleEdit(r)}>编辑</Button>
|
||||
<Popconfirm title="确定删除?" onConfirm={() => handleDelete(r.userId)}>
|
||||
<Button type="text" danger>删除</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
)
|
||||
@@ -201,88 +158,122 @@ export default function UserList() {
|
||||
];
|
||||
|
||||
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>
|
||||
<div style={{ padding: 20 }}>
|
||||
{contextHolder}
|
||||
<Card title="用户管理">
|
||||
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between' }}>
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>
|
||||
新增用户
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<Table
|
||||
rowKey="id"
|
||||
loading={loading}
|
||||
columns={columns}
|
||||
dataSource={userList}
|
||||
pagination={{ pageSize: 10 }}
|
||||
/>
|
||||
<Table
|
||||
rowKey="userId"
|
||||
columns={columns}
|
||||
dataSource={userList}
|
||||
pagination={{
|
||||
current: pagination.pageNum,
|
||||
pageSize: pagination.pageSize,
|
||||
total: pagination.total,
|
||||
showSizeChanger: true,
|
||||
showTotal: t => `共 ${t} 条`
|
||||
}}
|
||||
onChange={handleTableChange}
|
||||
bordered
|
||||
/>
|
||||
</Card>
|
||||
|
||||
{/* 新增/编辑弹窗:角色+数据权限双配置 */}
|
||||
{/* 新增/编辑弹窗(完全匹配后端字段) */}
|
||||
<Modal
|
||||
title={currentUser ? '编辑用户' : '新增用户'}
|
||||
open={modalVisible}
|
||||
width={650}
|
||||
onCancel={() => setModalVisible(false)}
|
||||
onOk={handleSubmit}
|
||||
maskClosable={false}
|
||||
width={600}
|
||||
destroyOnClose
|
||||
>
|
||||
<Form form={form} layout="vertical">
|
||||
<Form.Item
|
||||
name="username"
|
||||
label="登录账号"
|
||||
rules={[{ required: true, message: '请输入账号' }]}
|
||||
>
|
||||
<Input placeholder="请输入登录账号" />
|
||||
<Row gutter={16}>
|
||||
<Col span={12}>
|
||||
<Form.Item name="userName" label="用户账号" rules={[{ required: true }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
</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
|
||||
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"
|
||||
>
|
||||
<Form.Item name="sex" label="性别" initialValue="2">
|
||||
<Select>
|
||||
<Option value="0">正常</Option>
|
||||
<Option value="1">禁用</Option>
|
||||
<Option value="0">男</Option>
|
||||
<Option value="1">女</Option>
|
||||
<Option value="2">未知</Option>
|
||||
</Select>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user