布局修改
This commit is contained in:
338
src/pages/UserPage.tsx
Normal file
338
src/pages/UserPage.tsx
Normal file
@@ -0,0 +1,338 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import {
|
||||
Layout, Tabs, Card, Row, Col, Button, Typography, Space, Modal, Form, Tree, message,
|
||||
Input, Popconfirm, Divider, InputNumber, Switch, Pagination, Table, Select,
|
||||
Tag
|
||||
} from 'antd';
|
||||
import {
|
||||
PlusOutlined, EditOutlined, DeleteOutlined, UserOutlined
|
||||
} from '@ant-design/icons';
|
||||
import { getMenuList } from '../api/mune';
|
||||
import { buildPermTree, buildTreeFromFlat } from '../lib/utils';
|
||||
import { addRole, deleteRole, editRoleApi, getRoleList } from '../api/role';
|
||||
import { addUser, deleteUser, editUser, getUser } from '../api/user';
|
||||
import EmptyData from '../components/Empty';
|
||||
|
||||
const { Text } = Typography;
|
||||
const { TabPane } = Tabs;
|
||||
const AntdCard = Card as any;
|
||||
|
||||
// 用户 + 角色 合并页面
|
||||
export default function UserRolePage() {
|
||||
// ====================== 角色管理状态 ======================
|
||||
const [roles, setRoles] = useState([]);
|
||||
const [menuTree, setMenuTree] = useState([]);
|
||||
const [checkedKeys, setCheckedKeys] = useState<number[]>([]);
|
||||
const [roleModalVisible, setRoleModalVisible] = useState(false);
|
||||
const [roleForm] = Form.useForm();
|
||||
const [editRole, setEditRole] = useState(null);
|
||||
|
||||
// ====================== 用户管理状态 ======================
|
||||
const [userList, setUserList] = useState([]);
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
const [form] = Form.useForm();
|
||||
const [currentUser, setCurrentUser] = useState(null);
|
||||
const [rolesOption, setRolesOption] = useState([]);
|
||||
|
||||
// ====================== 统一分页 ======================
|
||||
const [rolePagination, setRolePagination] = useState({
|
||||
pageNum: 1, pageSize: 12, total: 0
|
||||
});
|
||||
const [userPagination, setUserPagination] = useState({
|
||||
pageNum: 1, pageSize: 10, total: 0
|
||||
});
|
||||
|
||||
// ====================== 获取角色列表 ======================
|
||||
const getRoleData = () => {
|
||||
const params = {
|
||||
pageNum: rolePagination.pageNum,
|
||||
pageSize: rolePagination.pageSize
|
||||
};
|
||||
getRoleList(params).then(res => {
|
||||
if (res.code === 200) {
|
||||
setRoles(res.data || []);
|
||||
setRolePagination({
|
||||
...rolePagination,
|
||||
total: res.total || res.data?.length || 0
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// ====================== 获取菜单树 ======================
|
||||
const getMenuData = () => {
|
||||
getMenuList({ pageNum: 1, pageSize: 1000 }).then(res => {
|
||||
if (res.code === 200) {
|
||||
const treeData = buildTreeFromFlat(res.data || []);
|
||||
setMenuTree(buildPermTree(treeData));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// ====================== 获取用户列表 ======================
|
||||
const getUserData = () => {
|
||||
const params = {
|
||||
pageNum: userPagination.pageNum,
|
||||
pageSize: userPagination.pageSize
|
||||
};
|
||||
getUser(params).then(res => {
|
||||
if (res.code === 200) {
|
||||
setUserList(res.rows || []);
|
||||
setUserPagination({
|
||||
...userPagination,
|
||||
total: res.total || res.rows?.length || 0
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// ====================== 获取角色下拉框 ======================
|
||||
const getRoleForSelect = () => {
|
||||
getRoleList({ pageNum: 1, pageSize: 1000 }).then(res => {
|
||||
if (res.code === 200) setRolesOption(res.data || []);
|
||||
});
|
||||
};
|
||||
|
||||
// ====================== 加载数据 ======================
|
||||
useEffect(() => {
|
||||
getRoleData();
|
||||
getMenuData();
|
||||
}, [rolePagination.pageNum, rolePagination.pageSize]);
|
||||
|
||||
useEffect(() => {
|
||||
getUserData();
|
||||
getRoleForSelect();
|
||||
}, [userPagination.pageNum, userPagination.pageSize]);
|
||||
|
||||
// ====================== 角色操作 ======================
|
||||
const handleAddRole = () => {
|
||||
roleForm.resetFields();
|
||||
setCheckedKeys([]);
|
||||
setEditRole(null);
|
||||
roleForm.setFieldsValue({ status: true, roleSort: 1 });
|
||||
setRoleModalVisible(true);
|
||||
};
|
||||
|
||||
const handleEditRole = (record) => {
|
||||
setEditRole(record);
|
||||
roleForm.setFieldsValue({ ...record, status: record.status == 1 });
|
||||
setCheckedKeys((record.menuIds || []).map(Number));
|
||||
setRoleModalVisible(true);
|
||||
};
|
||||
|
||||
const handleDeleteRole = (roleId) => {
|
||||
deleteRole([roleId]).then(res => {
|
||||
if (res.code === 200) { message.success('删除成功'); getRoleData(); }
|
||||
else message.error(res.msg || '删除失败');
|
||||
});
|
||||
};
|
||||
|
||||
const handleSaveRole = () => {
|
||||
roleForm.validateFields().then(values => {
|
||||
const params = {
|
||||
...values,
|
||||
menuIds: checkedKeys,
|
||||
status: values.status ? 1 : 0,
|
||||
roleSort: values.roleSort || 1,
|
||||
};
|
||||
if (editRole) params.roleId = editRole.roleId;
|
||||
|
||||
const api = editRole ? editRoleApi : addRole;
|
||||
api(params).then(res => {
|
||||
if (res.code === 200) {
|
||||
message.success(editRole ? '编辑成功' : '新增成功');
|
||||
setRoleModalVisible(false);
|
||||
getRoleData();
|
||||
getRoleForSelect();
|
||||
} else message.error(res.msg || '保存失败');
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// ====================== 用户操作 ======================
|
||||
const handleAddUser = () => {
|
||||
form.resetFields();
|
||||
setCurrentUser(null);
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
const handleEditUser = (record) => {
|
||||
setCurrentUser(record);
|
||||
form.setFieldsValue({ ...record, roleIds: record.roles?.map(i => i.roleId) || [] });
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
const handleDeleteUser = (userId) => {
|
||||
deleteUser(userId).then(res => {
|
||||
if (res.code === 200) { message.success('删除成功'); getUserData(); }
|
||||
else message.error(res.msg || '删除失败');
|
||||
});
|
||||
};
|
||||
|
||||
const handleSaveUser = () => {
|
||||
form.validateFields().then(values => {
|
||||
const params = { ...values, roleIds: values.roleIds || [] };
|
||||
if (currentUser) params.userId = currentUser.userId;
|
||||
|
||||
const api = currentUser ? editUser : addUser;
|
||||
api(params).then(res => {
|
||||
if (res.code === 200) {
|
||||
message.success(currentUser ? '编辑成功' : '新增成功');
|
||||
setModalVisible(false);
|
||||
getUserData();
|
||||
} else message.error(res.msg || '保存失败');
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// ====================== 页面主体 ======================
|
||||
return (
|
||||
<div style={{
|
||||
height: '100%',
|
||||
maxHeight: 'calc(100vh - 64px)',
|
||||
overflowY: 'auto',
|
||||
scrollbarWidth: 'none',
|
||||
msOverflowStyle: 'none',
|
||||
padding: '20px',
|
||||
background: '#f5f7fa'
|
||||
}}>
|
||||
|
||||
{/* 整体卡片 */}
|
||||
<Card bodyStyle={{ padding: '20px 24px' }} style={{ borderRadius: 12, boxShadow: '0 2px 10px rgba(0,0,0,0.06)' }}>
|
||||
<Tabs defaultActiveKey="user" type="card" size="large" style={{ marginBottom: 20 }}>
|
||||
|
||||
{/* ========== 用户管理 ========== */}
|
||||
<TabPane tab="用户管理" key="user">
|
||||
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between' }}>
|
||||
<div></div>
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={handleAddUser}>新增用户</Button>
|
||||
</div>
|
||||
|
||||
<Table
|
||||
rowKey="userId"
|
||||
columns={[
|
||||
{ title: '用户账号', dataIndex: 'userName' },
|
||||
{ title: '用户昵称', dataIndex: 'nickName' },
|
||||
{ title: '手机号', dataIndex: 'phonenumber' },
|
||||
{ title: '邮箱', dataIndex: 'email' },
|
||||
{
|
||||
title: '角色', render: (_, r) => (r.roles || []).map(i => (
|
||||
<Tag color="blue" key={i.roleId}>{i.roleName || i.roleKey}</Tag>
|
||||
))
|
||||
},
|
||||
{
|
||||
title: '状态', render: (s) => s == '0' ?
|
||||
<Tag color="green">正常</Tag> : <Tag color="red">停用</Tag>
|
||||
},
|
||||
{
|
||||
title: '操作', render: (_, r) => (
|
||||
<Space>
|
||||
<Button type="text" icon={<EditOutlined />} onClick={() => handleEditUser(r)}>编辑</Button>
|
||||
<Popconfirm onConfirm={() => handleDeleteUser(r.userId)} title="确定删除?">
|
||||
<Button type="text" danger>删除</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
)
|
||||
}
|
||||
]}
|
||||
dataSource={userList}
|
||||
pagination={{
|
||||
current: userPagination.pageNum,
|
||||
pageSize: userPagination.pageSize,
|
||||
total: userPagination.total,
|
||||
showSizeChanger: true,
|
||||
showTotal: t => `共 ${t} 条`
|
||||
}}
|
||||
onChange={(p) => setUserPagination({ ...userPagination, pageNum: p.current, pageSize: p.pageSize })}
|
||||
bordered
|
||||
size="middle"
|
||||
/>
|
||||
</TabPane>
|
||||
|
||||
{/* ========== 角色管理 ========== */}
|
||||
<TabPane tab="角色管理" key="role">
|
||||
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between' }}>
|
||||
<div></div>
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={handleAddRole}>新增角色</Button>
|
||||
</div>
|
||||
|
||||
<Row gutter={[16, 16]}>
|
||||
{roles.length > 0 ? roles.map(r => (
|
||||
<Col xs={24} sm={12} md={8} lg={6} key={r.roleId}>
|
||||
<Card hoverable style={{ borderRadius: 10 }}
|
||||
actions={[
|
||||
<Button type="text" icon={<EditOutlined />} onClick={() => handleEditRole(r)}>编辑</Button>,
|
||||
<Popconfirm onConfirm={() => handleDeleteRole(r.roleId)} title="确定删除?">
|
||||
<Button type="text" danger>删除</Button>
|
||||
</Popconfirm>
|
||||
]}
|
||||
>
|
||||
<Card.Meta
|
||||
title={<strong>{r.roleName}</strong>}
|
||||
description={
|
||||
<Space direction="vertical" size={2}>
|
||||
<Text type="secondary">权限:{r.roleKey}</Text>
|
||||
<Text type="secondary">状态:{r.status == 1 ? '正常' : '停用'}</Text>
|
||||
{r.admin && <Tag color="red">超级管理员</Tag>}
|
||||
</Space>
|
||||
}
|
||||
/>
|
||||
</Card>
|
||||
</Col>
|
||||
)) : <EmptyData height={260} />}
|
||||
</Row>
|
||||
|
||||
<div style={{ textAlign: 'right', marginTop: 20 }}>
|
||||
<Pagination
|
||||
current={rolePagination.pageNum}
|
||||
pageSize={rolePagination.pageSize}
|
||||
total={rolePagination.total}
|
||||
showSizeChanger
|
||||
showTotal={(t) => `共 ${t} 条`}
|
||||
onChange={(p, s) => setRolePagination({ pageNum: p, pageSize: s, total: rolePagination.total })}
|
||||
/>
|
||||
</div>
|
||||
</TabPane>
|
||||
</Tabs>
|
||||
</Card>
|
||||
|
||||
{/* ========== 角色弹窗 ========== */}
|
||||
<Modal title={editRole ? '编辑角色' : '新增角色'} open={roleModalVisible}
|
||||
onCancel={() => setRoleModalVisible(false)} onOk={handleSaveRole} width={560}>
|
||||
<Form form={roleForm} layout="vertical">
|
||||
<Form.Item name="roleName" label="角色名称" rules={[{ required: true }]}><Input /></Form.Item>
|
||||
<Form.Item name="roleKey" label="权限标识" rules={[{ required: true }]}><Input /></Form.Item>
|
||||
<Form.Item name="roleSort" label="排序"><InputNumber style={{ width: '100%' }} /></Form.Item>
|
||||
<Form.Item name="status" label="状态" valuePropName="checked"><Switch checkedChildren="正常" unCheckedChildren="停用" /></Form.Item>
|
||||
<Divider>菜单权限</Divider>
|
||||
<Tree checkable treeData={menuTree} checkedKeys={checkedKeys} onCheck={setCheckedKeys} defaultExpandAll />
|
||||
</Form>
|
||||
</Modal>
|
||||
|
||||
{/* ========== 用户弹窗 ========== */}
|
||||
<Modal title={currentUser ? '编辑用户' : '新增用户'} open={modalVisible}
|
||||
onCancel={() => setModalVisible(false)} onOk={handleSaveUser} width={680} destroyOnClose>
|
||||
<Form form={form} layout="vertical">
|
||||
<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="language" label="语言"><Select><Select.Option value="zh-CN">中文</Select.Option><Select.Option value="en">英语</Select.Option></Select></Form.Item></Col>
|
||||
</Row>
|
||||
<Row gutter={16}>
|
||||
<Col span={12}><Form.Item name="roleIds" label="分配角色"><Select mode="multiple" placeholder="选择角色">{rolesOption.map(i => <Select.Option key={i.roleId} value={i.roleId}>{i.roleName}</Select.Option>)}</Select></Form.Item></Col>
|
||||
<Col span={12}><Form.Item name="status" label="状态" initialValue="0"><Select><Select.Option value="0">正常</Select.Option><Select.Option value="1">停用</Select.Option></Select></Form.Item></Col>
|
||||
</Row>
|
||||
<Form.Item name="siteId" label="场站ID"><Input /></Form.Item>
|
||||
<Form.Item name="sex" label="性别" initialValue="2"><Select><Select.Option value="0">男</Select.Option><Select.Option value="1">女</Select.Option><Select.Option value="2">未知</Select.Option></Select></Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user