index.tsx 7.8 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, queryUserDetail, 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 [detailData, setDetailData] = 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. setDetailData(null);
  58. };
  59. // 编辑弹框
  60. const toEdit = (data: any) => {
  61. // 获取详情信息
  62. queryUserDetail(data.record_id).then((res) => {
  63. if (res?.code === 0) {
  64. setDetailData(res?.data || null);
  65. setVisible(true);
  66. }
  67. });
  68. };
  69. // 新增编辑弹框回调
  70. const editCallback = () => {
  71. setVisible(false);
  72. setLoading(true);
  73. getListData();
  74. };
  75. // 搜索
  76. const onFinish = () => {
  77. form.validateFields().then((data) => {
  78. setLoading(true);
  79. setSearchData(data);
  80. });
  81. };
  82. // 重置
  83. const onReset = () => {
  84. form.resetFields();
  85. setLoading(true);
  86. setSearchData(null);
  87. };
  88. // 启用
  89. const toEnable = (record: any) => {
  90. enableUser(record.record_id)
  91. .then((res) => {
  92. if (res.code === 0) {
  93. message.success('启用成功');
  94. setLoading(true);
  95. getListData();
  96. } else {
  97. message.error('启用失败');
  98. }
  99. })
  100. .catch((e) => {
  101. message.error(e.message);
  102. });
  103. };
  104. //停用
  105. const toDisable = (record: any) => {
  106. disableUser(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. useEffect(() => {
  121. getListData();
  122. }, [searchData]);
  123. // 分页切换
  124. const tableChange = (page: any) => {
  125. setLoading(true);
  126. const param = {
  127. q: 'page',
  128. current: page.current,
  129. pageSize: page.pageSize,
  130. ...searchData,
  131. };
  132. queryUserList(param).then((res) => {
  133. if (res.code === 0) {
  134. setDataList(res.data.list);
  135. setPagination(res.data.pagination);
  136. setLoading(false);
  137. }
  138. });
  139. };
  140. // 查看
  141. const toCheck = (record: DataType) => {
  142. setCheckVisible(true);
  143. setCheckId(record?.record_id);
  144. };
  145. // 查看回调
  146. const checkCallback = () => {
  147. setCheckVisible(false);
  148. };
  149. const columns: ColumnsType<DataType> = [
  150. {
  151. title: '序号',
  152. align: 'center',
  153. key: 'index',
  154. render: (_: any, row: any, index: number) => index + 1,
  155. },
  156. {
  157. title: '头像',
  158. dataIndex: 'photo',
  159. key: 'photo',
  160. render: (v) => v && <Image width={50} src={v} alt="头像" />,
  161. },
  162. {
  163. title: '用户名',
  164. dataIndex: 'user_name',
  165. key: 'user_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} detailData={detailData} />}
  289. {checkVisible && <Check visible={checkVisible} id={checkId} onCallback={checkCallback} />}
  290. </Card>
  291. </PageContainer>
  292. );
  293. };
  294. export default UserManagement;