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

280 lines
6.3 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 12:49:47 +08:00
Input, Select, Popconfirm, message, Tag, Row, Col, Switch
2026-04-17 17:20:41 +08:00
} from 'antd';
2026-04-24 15:28:16 +08:00
import {
2026-04-29 12:49:47 +08:00
PlusOutlined, EditOutlined, DeleteOutlined, UserOutlined
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-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();
// 统一分页
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
});
};
2026-04-24 15:28:16 +08:00
2026-04-29 12:49:47 +08:00
useEffect(() => {
getUserList();
}, [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 12:49:47 +08:00
form.setFieldsValue({
...record
});
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-24 15:28:16 +08:00
};
2026-04-29 12:49:47 +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 12:49:47 +08:00
roleIds: values.roleId ? [values.roleId] : [] // 角色ID数组
2026-04-24 15:28:16 +08:00
};
if (currentUser) {
// 编辑
2026-04-29 12:49:47 +08:00
editUser(params).then(res => {
if (res.code === 200) {
messageApi.success('编辑成功');
getUserList();
setModalVisible(false);
}
});
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-24 15:28:16 +08:00
}
});
};
2026-04-29 12:49:47 +08:00
// 表格列
2026-04-24 15:28:16 +08:00
const columns = [
{
2026-04-29 12:49:47 +08:00
title: '用户账号',
dataIndex: 'userName'
2026-04-24 15:28:16 +08:00
},
{
2026-04-29 12:49:47 +08:00
title: '用户昵称',
dataIndex: 'nickName'
2026-04-24 15:28:16 +08:00
},
{
2026-04-29 12:49:47 +08:00
title: '手机号',
dataIndex: 'phonenumber'
},
{
title: '邮箱',
dataIndex: 'email'
},
{
title: '角色',
dataIndex: 'roleNames',
render: (t) => <Tag color="blue">{t || '未分配'}</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 12:49:47 +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}>
<Form.Item name="displayPassword" 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="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>
2026-04-24 15:28:16 +08:00
2026-04-29 12:49:47 +08:00
<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
}