index.tsx 8.9 KB

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