index.tsx 9.4 KB

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