index.tsx 7.7 KB

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