Files
web-Iot/src/pages/Users.tsx
2026-04-30 10:49:44 +08:00

297 lines
7.2 KiB
TypeScript

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