index.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import { Button, Card, Form, Input, Space, Table, Image, Select, message } from 'antd';
  2. import React, { useEffect, useState } from 'react';
  3. import { PlusCircleOutlined, ReloadOutlined, SearchOutlined } from '@ant-design/icons';
  4. import type { ColumnsType } from 'antd/es/table';
  5. import moment from 'moment';
  6. import Edit from '@/pages/setting/UserManagement/edit';
  7. import { disableUser, enableUser, queryUserList } from '@/services/setting';
  8. import { PageContainer } from '@ant-design/pro-components';
  9. interface DataType {
  10. key: string;
  11. user_name: string;
  12. real_name: string;
  13. photo: string;
  14. phone: string;
  15. status: number;
  16. record_id: string;
  17. updated_at: string;
  18. }
  19. /**
  20. * 用户管理页面
  21. * @constructor
  22. */
  23. const UserManagement: React.FC = () => {
  24. const [form] = Form.useForm();
  25. const [visible, setVisible] = useState(false);
  26. const [editData, setEditData] = useState<object | null>({});
  27. const [searchData, setSearchData] = useState<object | null>({});
  28. const [dataList, setDataList] = useState([]);
  29. const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  30. const [loading, setLoading] = useState(false);
  31. // 获取列表数据
  32. const getListData = () => {
  33. const params = {
  34. q: 'page',
  35. current: pagination.current,
  36. pageSize: pagination.pageSize,
  37. ...searchData,
  38. };
  39. queryUserList(params).then((res) => {
  40. if (res && !res.error) {
  41. setDataList(res.list);
  42. setPagination(res.pagination);
  43. setLoading(false);
  44. }
  45. });
  46. };
  47. useEffect(() => {
  48. setLoading(true);
  49. getListData();
  50. }, []);
  51. // 新增弹框
  52. const onAdd = () => {
  53. setVisible(true);
  54. setEditData(null);
  55. };
  56. // 编辑弹框
  57. const toEdit = (data: object) => {
  58. setVisible(true);
  59. setEditData(data);
  60. };
  61. // 新增编辑弹框回调
  62. const editCallback = () => {
  63. setVisible(false);
  64. setLoading(true);
  65. getListData();
  66. };
  67. // 搜索
  68. const onFinish = () => {
  69. form.validateFields().then((data) => {
  70. setLoading(true);
  71. setSearchData(data);
  72. });
  73. };
  74. // 重置
  75. const onReset = () => {
  76. form.resetFields();
  77. setLoading(true);
  78. setSearchData(null);
  79. };
  80. // 启用
  81. const toEnable = (record: any) => {
  82. enableUser(record.record_id)
  83. .then((res) => {
  84. if (res && !res.error) {
  85. message.success('启用成功');
  86. setLoading(true);
  87. getListData();
  88. } else {
  89. message.error('启用失败');
  90. }
  91. })
  92. .catch((e) => {
  93. message.error(e?.error?.message);
  94. });
  95. };
  96. //停用
  97. const toDisable = (record: any) => {
  98. disableUser(record.record_id)
  99. .then((res) => {
  100. if (res && !res.error) {
  101. message.success('停用成功');
  102. setLoading(true);
  103. getListData();
  104. } else {
  105. message.error('停用失败');
  106. }
  107. })
  108. .catch((e) => {
  109. message.error(e?.error?.message);
  110. });
  111. };
  112. useEffect(() => {
  113. getListData();
  114. }, [searchData]);
  115. // 分页切换
  116. const tableChange = (page: any) => {
  117. setLoading(true);
  118. const param = {
  119. q: 'page',
  120. current: page.current,
  121. pageSize: page.pageSize,
  122. ...searchData,
  123. };
  124. queryUserList(param).then((res) => {
  125. if (res && !res.error) {
  126. setDataList(res.list);
  127. setPagination(res.pagination);
  128. setLoading(false);
  129. }
  130. });
  131. };
  132. const columns: ColumnsType<DataType> = [
  133. {
  134. title: '序号',
  135. align: 'center',
  136. key: 'index',
  137. render: (_: any, row: any, index: number) => index + 1,
  138. },
  139. {
  140. title: '头像',
  141. dataIndex: 'photo',
  142. key: 'photo',
  143. render: (v) => v && <Image width={50} src={`http://192.168.0.224:18199${v}`} alt="头像" />,
  144. },
  145. {
  146. title: '用户姓名',
  147. dataIndex: 'user_name',
  148. key: 'user_name',
  149. },
  150. {
  151. title: '真实姓名',
  152. dataIndex: 'real_name',
  153. key: 'real_name',
  154. },
  155. {
  156. title: '手机号',
  157. dataIndex: 'phone',
  158. key: 'phone',
  159. },
  160. {
  161. title: '状态',
  162. dataIndex: 'status',
  163. key: 'status',
  164. render: (v) =>
  165. v && (
  166. <span style={{ color: `${{ 1: '#00a650', 2: 'red' }[v]}` }}>
  167. {{ 1: '启用', 2: '停用' }[v]}
  168. </span>
  169. ),
  170. },
  171. {
  172. title: '更新时间',
  173. dataIndex: 'updated_at',
  174. key: 'updated_at',
  175. render: (v) => v && moment(v).format('YYYY-MM-DD HH:ss'),
  176. },
  177. {
  178. title: '操作',
  179. key: 'action',
  180. render: (_, record) => (
  181. <Space size="middle">
  182. <a
  183. onClick={() => {
  184. toEdit(record);
  185. }}
  186. >
  187. 编辑
  188. </a>
  189. {record?.status === 2 && (
  190. <a
  191. style={{ color: 'green' }}
  192. onClick={() => {
  193. toEnable(record);
  194. }}
  195. >
  196. 启用
  197. </a>
  198. )}
  199. {record?.status === 1 && (
  200. <a
  201. style={{ color: 'red' }}
  202. onClick={() => {
  203. toDisable(record);
  204. }}
  205. >
  206. 停用
  207. </a>
  208. )}
  209. </Space>
  210. ),
  211. },
  212. ];
  213. const paginationProps = {
  214. showSizeChanger: true,
  215. showQuickJumper: true,
  216. showTotal: (total: number) => {
  217. return <span> 共 {total}条 </span>;
  218. },
  219. ...pagination,
  220. };
  221. return (
  222. <PageContainer>
  223. <Card>
  224. <Form form={form} layout="inline" onFinish={onFinish}>
  225. <Form.Item name="user_name" label="用户姓名">
  226. <Input placeholder="请输入用户姓名" />
  227. </Form.Item>
  228. <Form.Item name="real_name" label="真实姓名">
  229. <Input placeholder="请输入真实姓名" />
  230. </Form.Item>
  231. <Form.Item name="phone" label="手机号">
  232. <Input placeholder="请输入手机号" />
  233. </Form.Item>
  234. <Form.Item name="status" label="状态">
  235. <Select style={{ width: '175px' }} placeholder="请选择状态">
  236. <Select.Option key={1} value={1}>
  237. 启用
  238. </Select.Option>
  239. <Select.Option key={2} value={2}>
  240. 停用
  241. </Select.Option>
  242. </Select>
  243. </Form.Item>
  244. <Form.Item style={{ marginBottom: '10px' }}>
  245. <Space>
  246. <Button type="primary" htmlType="submit">
  247. <SearchOutlined />
  248. 查询
  249. </Button>
  250. <Button htmlType="button" onClick={onReset}>
  251. <ReloadOutlined />
  252. 重置
  253. </Button>
  254. </Space>
  255. </Form.Item>
  256. </Form>
  257. <Button htmlType="button" type="primary" style={{ margin: '20px 0' }} onClick={onAdd}>
  258. <PlusCircleOutlined />
  259. 新增用户
  260. </Button>
  261. <Table
  262. columns={columns}
  263. dataSource={dataList}
  264. rowKey={(record) => record.record_id}
  265. pagination={paginationProps}
  266. loading={loading}
  267. onChange={tableChange}
  268. />
  269. {visible && <Edit visible={visible} editCallback={editCallback} params={editData} />}
  270. </Card>
  271. </PageContainer>
  272. );
  273. };
  274. export default UserManagement;