Files
web-Iot/src/pages/Users.tsx

297 lines
7.2 KiB
TypeScript
Raw Normal View History

2026-04-29 12:49:47 +08:00
import React, { useState, useEffect } from 'react';
2026-04-24 15:28:16 +08:00
import {
Card, Table, Button, Typography, Space, Modal, Form,
2026-04-29 14:26:01 +08:00
Input, Select, Popconfirm, message, Tag, Row, Col
2026-04-17 17:20:41 +08:00
} from 'antd';
2026-04-24 15:28:16 +08:00
import {
2026-04-29 14:26:01 +08:00
PlusOutlined, EditOutlined, DeleteOutlined
2026-04-17 17:20:41 +08:00
} from '@ant-design/icons';
2026-04-29 12:49:47 +08:00
import { addUser, deleteUser, editUser, getUser } from '../api/user';
2026-04-29 14:26:01 +08:00
import { getRoleList } from '../api/role';
2026-04-24 15:28:16 +08:00
const { Title } = Typography;
const { Option } = Select;
export default function UserList() {
2026-04-29 12:49:47 +08:00
const [userList, setUserList] = useState([]);
2026-04-24 15:28:16 +08:00
const [modalVisible, setModalVisible] = useState(false);
const [form] = Form.useForm();
const [currentUser, setCurrentUser] = useState(null);
2026-04-29 12:49:47 +08:00
const [messageApi, contextHolder] = message.useMessage();
2026-04-29 14:26:01 +08:00
const [roles, setRoles] = useState([]);
2026-04-29 12:49:47 +08:00
// 统一分页
const [pagination, setPagination] = useState({
pageNum: 1,
pageSize: 10,
total: 0
});
2026-04-29 14:26:01 +08:00
// 获取角色列表
const getRole = () => {
const params = { pageNum: 1, pageSize: 1000 };
getRoleList(params).then(res => {
if (res.code === 200) {
setRoles(res.data || []);
}
});
};
2026-04-29 12:49:47 +08:00
// 获取用户列表
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
});
};
2026-04-24 15:28:16 +08:00
2026-04-29 12:49:47 +08:00
useEffect(() => {
getUserList();
2026-04-29 14:26:01 +08:00
getRole();
2026-04-29 12:49:47 +08:00
}, [pagination.pageNum, pagination.pageSize]);
// 新增
2026-04-24 15:28:16 +08:00
const handleAdd = () => {
form.resetFields();
setCurrentUser(null);
setModalVisible(true);
};
2026-04-29 12:49:47 +08:00
// 编辑
2026-04-24 15:28:16 +08:00
const handleEdit = (record) => {
setCurrentUser(record);
2026-04-29 14:26:01 +08:00
const roleIds = record.roles?.map(item => item.roleId) || [];
2026-04-29 12:49:47 +08:00
form.setFieldsValue({
2026-04-29 14:26:01 +08:00
...record,
roleIds: roleIds, // 回填多选角色
2026-04-29 12:49:47 +08:00
});
2026-04-24 15:28:16 +08:00
setModalVisible(true);
};
2026-04-29 12:49:47 +08:00
// 删除
const handleDelete = (userId) => {
deleteUser(userId).then(res => {
if (res.code === 200) {
messageApi.success('删除成功');
getUserList();
} else {
messageApi.error(res.msg || '删除失败');
}
2026-04-29 14:26:01 +08:00
}).catch(() => {
messageApi.error('删除失败');
})
2026-04-24 15:28:16 +08:00
};
2026-04-29 14:26:01 +08:00
// 提交保存
2026-04-24 15:28:16 +08:00
const handleSubmit = () => {
form.validateFields().then(values => {
2026-04-29 12:49:47 +08:00
const params = {
2026-04-24 15:28:16 +08:00
...values,
2026-04-29 14:26:01 +08:00
roleIds: values.roleIds || [], // 直接提交数组
2026-04-24 15:28:16 +08:00
};
if (currentUser) {
2026-04-29 14:26:01 +08:00
params.userId = currentUser.userId;
2026-04-29 12:49:47 +08:00
editUser(params).then(res => {
if (res.code === 200) {
messageApi.success('编辑成功');
getUserList();
setModalVisible(false);
2026-04-29 14:26:01 +08:00
} else {
messageApi.error(res.msg || '编辑失败');
2026-04-29 12:49:47 +08:00
}
2026-04-29 14:26:01 +08:00
}).catch(() => {
messageApi.error('编辑失败');
2026-04-29 12:49:47 +08:00
});
2026-04-24 15:28:16 +08:00
} else {
2026-04-29 12:49:47 +08:00
addUser(params).then(res => {
if (res.code === 200) {
messageApi.success('新增成功');
getUserList();
setModalVisible(false);
2026-04-29 14:26:01 +08:00
} else {
messageApi.error(res.msg || '新增失败');
2026-04-29 12:49:47 +08:00
}
2026-04-29 14:26:01 +08:00
}).catch(() => {
messageApi.error('新增失败');
})
2026-04-24 15:28:16 +08:00
}
});
};
2026-04-29 14:26:01 +08:00
// 表格列(支持多角色展示)
2026-04-24 15:28:16 +08:00
const columns = [
2026-04-29 14:26:01 +08:00
{ title: '用户账号', dataIndex: 'userName' },
{ title: '用户昵称', dataIndex: 'nickName' },
{ title: '手机号', dataIndex: 'phonenumber' },
{ title: '邮箱', dataIndex: 'email' },
2026-04-29 12:49:47 +08:00
{
title: '角色',
2026-04-29 14:26:01 +08:00
render: (_, record) => {
const roles = record.roles || [];
if (roles.length === 0) return <Tag>未分配</Tag>;
return roles.map(item => (
<Tag color="blue" key={item.roleId}>
{item.roleName || item.roleKey || '角色'}
</Tag>
));
}
2026-04-24 15:28:16 +08:00
},
{
title: '状态',
dataIndex: 'status',
render: (s) => s === '0' ?
<Tag color="green">正常</Tag> :
2026-04-29 12:49:47 +08:00
<Tag color="red">停用</Tag>
2026-04-24 15:28:16 +08:00
},
{
title: '操作',
2026-04-29 12:49:47 +08:00
render: (_, r) => (
2026-04-24 15:28:16 +08:00
<Space>
2026-04-29 12:49:47 +08:00
<Button type="text" icon={<EditOutlined />} onClick={() => handleEdit(r)}>编辑</Button>
<Popconfirm title="确定删除?" onConfirm={() => handleDelete(r.userId)}>
<Button type="text" danger>删除</Button>
2026-04-24 15:28:16 +08:00
</Popconfirm>
</Space>
)
}
];
return (
2026-04-29 12:49:47 +08:00
<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>
2026-04-24 15:28:16 +08:00
2026-04-29 12:49:47 +08:00
<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>
2026-04-24 15:28:16 +08:00
2026-04-29 14:26:01 +08:00
{/* 新增/编辑弹窗 */}
2026-04-24 15:28:16 +08:00
<Modal
title={currentUser ? '编辑用户' : '新增用户'}
open={modalVisible}
2026-04-29 12:49:47 +08:00
width={650}
2026-04-24 15:28:16 +08:00
onCancel={() => setModalVisible(false)}
onOk={handleSubmit}
2026-04-29 12:49:47 +08:00
destroyOnClose
2026-04-24 15:28:16 +08:00
>
<Form form={form} layout="vertical">
2026-04-29 12:49:47 +08:00
<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>
2026-04-24 15:28:16 +08:00
2026-04-29 12:49:47 +08:00
<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>
2026-04-24 15:28:16 +08:00
2026-04-29 12:49:47 +08:00
<Row gutter={16}>
<Col span={12}>
<Form.Item name="password" label="密码">
<Input.Password placeholder="不填则不修改" />
</Form.Item>
</Col>
<Col span={12}>
2026-04-29 14:26:01 +08:00
<Form.Item name="language" label="语言">
<Select placeholder="请选择语言">
<Option value="zh-CN">中文</Option>
<Option value="en">英语</Option>
</Select>
2026-04-29 12:49:47 +08:00
</Form.Item>
</Col>
</Row>
2026-04-24 15:28:16 +08:00
2026-04-29 12:49:47 +08:00
<Row gutter={16}>
<Col span={12}>
2026-04-29 14:26:01 +08:00
{/* 多角色选择 */}
<Form.Item name="roleIds" label="分配角色">
<Select mode="multiple" placeholder="请选择角色">
{roles.map(item => (
<Option key={item.roleId} value={item.roleId}>
{item.roleName || item.roleKey}
</Option>
))}
</Select>
2026-04-29 12:49:47 +08:00
</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>
<Form.Item name="siteId" label="场站ID">
<Input />
2026-04-24 15:28:16 +08:00
</Form.Item>
2026-04-29 12:49:47 +08:00
<Form.Item name="sex" label="性别" initialValue="2">
2026-04-24 15:28:16 +08:00
<Select>
2026-04-29 12:49:47 +08:00
<Option value="0">男</Option>
<Option value="1">女</Option>
<Option value="2">未知</Option>
2026-04-24 15:28:16 +08:00
</Select>
</Form.Item>
</Form>
</Modal>
2026-04-29 12:49:47 +08:00
</div>
2026-04-24 15:28:16 +08:00
);
2026-04-17 17:20:41 +08:00
}