index.jsx 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import {
  3. Button,
  4. Popconfirm,
  5. Modal,
  6. Form,
  7. Input,
  8. message,
  9. Select,
  10. } from 'antd';
  11. import { PlusOutlined } from '@ant-design/icons';
  12. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  13. import PageTable from '@/components/PageTable';
  14. import md5 from 'md5';
  15. import {
  16. getUserList,
  17. addUser,
  18. deleteUser,
  19. updateUser,
  20. getUserDetail,
  21. } from '@/services/user';
  22. import './index.less';
  23. const FormItem = Form.Item;
  24. const { Option } = Select;
  25. export default (props) => {
  26. //编辑弹窗
  27. const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 14 } };
  28. const [form] = Form.useForm();
  29. const [editModal, setEditModal] = useState(false);
  30. const [loading, setLoading] = useState(false);
  31. const [userId, setuserId] = useState();
  32. //列表数据
  33. const actionRef = useRef();
  34. // 编辑弹窗的表单提交
  35. const Submit = (values) => {
  36. const newData = { ...values };
  37. if (!newData.sex && newData.sex !== 0) {
  38. newData.sex = 1;
  39. }
  40. setLoading(true);
  41. if (!/^1[0-9]{10}$/.test(newData.phone)) {
  42. message.warning('请输入正确的十一位手机号');
  43. setLoading(false);
  44. return false;
  45. }
  46. newData.password=md5(newData.password);
  47. if (userId) {
  48. updateUser(userId, newData).then(() => {
  49. setLoading(false);
  50. message.success(`修改成功`);
  51. onCancel();
  52. actionRef.current.reload();
  53. }).catch((err) => {
  54. setLoading(false);
  55. console.log(err.message)
  56. });
  57. } else {
  58. addUser(newData)
  59. .then(() => {
  60. setLoading(false);
  61. message.success(`保存成功`);
  62. onCancel();
  63. actionRef.current.reload();
  64. })
  65. .catch((err) => {
  66. setLoading(false);
  67. message.error(err.message || err);
  68. });
  69. }
  70. };
  71. //表单点击取消按钮
  72. const onCancel = () => {
  73. setuserId();
  74. form.resetFields();
  75. setEditModal(false);
  76. };
  77. // 列表点击编辑按钮
  78. const handleEdit = (val) => {
  79. setuserId(val.userId);
  80. setEditModal(true);
  81. };
  82. //列表点击删除按钮
  83. const handleDelete = (id) => {
  84. deleteUser(id)
  85. .then(() => {
  86. message.success('删除成功');
  87. actionRef.current.reload();
  88. })
  89. .catch((err) => {
  90. console.log(err.message)
  91. });
  92. };
  93. //列表切换人员状态方法
  94. const handleOK = (record, data) => {
  95. const title = record.status
  96. ? '您确定要将该人员状态变更为禁用吗? 禁用后该人员将不能登录'
  97. : '您确定要将该人员状态变更为启用吗? 启用后该人员可以登录!';
  98. Modal.confirm({
  99. title: title,
  100. okText: '确认',
  101. cancelText: '取消',
  102. onOk() {
  103. updateUser(record.userId, { ...record, status: record.status === 1 ? 0 : 1 })
  104. .then((res) => {
  105. message.success('操作成功');
  106. actionRef.current.reload();
  107. })
  108. .catch((err) => {
  109. console.log(err.message)
  110. });
  111. },
  112. });
  113. };
  114. useEffect(() => {
  115. if (userId) {
  116. getUserDetail(userId).then((res) => {
  117. form.setFieldsValue(res);
  118. }).catch((err) => {
  119. console.log(err.message)
  120. });
  121. } else {
  122. form.resetFields();
  123. }
  124. }, [userId]);
  125. const actions = () => [
  126. <Button key="add" type="primary" icon={<PlusOutlined />} onClick={() => setEditModal(true)}>
  127. 新增
  128. </Button>,
  129. ];
  130. const columns = [
  131. {
  132. title: '用户名',
  133. dataIndex: 'userName',
  134. key: 'userName',
  135. },
  136. {
  137. title: '手机号',
  138. dataIndex: 'phone',
  139. key: 'phone',
  140. search:false
  141. },
  142. {
  143. title: '角色',
  144. dataIndex: 'roleName',
  145. key: 'roleName',
  146. render: (_, record) => {
  147. return record.roleName === 'auditor' ? '审核员' : '证件发放员';
  148. },
  149. search:false
  150. },
  151. {
  152. title: '状态',
  153. dataIndex: 'status',
  154. key: 'status',
  155. search: false,
  156. render: (_, record) => {
  157. return record.status === 1 ? '启用' : '未启用';
  158. },
  159. },
  160. {
  161. title: '操作',
  162. valueType: 'option',
  163. render: (_, record) => [
  164. <Button style={{ padding: 0 }} type="link" key={1} onClick={() => handleOK(record)}>
  165. {record.status === 0 ? '启用' : '禁用'}
  166. </Button>,
  167. <Button style={{ padding: 0 }} type="link" key={2} onClick={() => handleEdit(record)}>
  168. 编辑
  169. </Button>,
  170. <Popconfirm
  171. key={3}
  172. title="您是否确认删除 ?"
  173. onConfirm={() => handleDelete(record.userId)}
  174. okText="确定"
  175. cancelText="取消"
  176. >
  177. <Button style={{ padding: 0 }} type="link">
  178. 删除
  179. </Button>
  180. </Popconfirm>,
  181. ],
  182. },
  183. ];
  184. return (
  185. <PageHeaderWrapper>
  186. <PageTable
  187. request={getUserList}
  188. columns={columns}
  189. actionRef={actionRef}
  190. rowKey="userId"
  191. options={false}
  192. toolBarRender={actions}
  193. />
  194. <Modal
  195. forceRender
  196. title={userId ? '人员编辑' : '人员新增'}
  197. visible={editModal}
  198. onCancel={onCancel}
  199. keyboard={false}
  200. maskClosable={false}
  201. destroyOnClose={true}
  202. footer={null}
  203. >
  204. <Form {...formItemLayout} onFinish={Submit} form={form}>
  205. <FormItem label="用户名" name="userName" rules={[{ required: true, message: '请输入' }]}>
  206. <Input placeholder="请输入" />
  207. </FormItem>
  208. <FormItem label="手机号" name="phone" rules={[{ required: true, message: '请输入' }]}>
  209. <Input maxLength="11" placeholder="请输入" />
  210. </FormItem>
  211. <FormItem label="登录账号" name="loginName" rules={[{ required: true, message: '请输入登录账号' }]}>
  212. <Input placeholder="请输入" />
  213. </FormItem>
  214. <FormItem label="密码" name="password" rules={[{ required: true, message: '请输入登录密码' }]}>
  215. <Input placeholder="请输入" />
  216. </FormItem>
  217. <FormItem label="角色" name="roleName" rules={[{ required: true, message: '请选择' }]}>
  218. <Select placeholder="请选择角色">
  219. <Option value='manager'>管理员</Option>
  220. <Option value='auditor'>审核员</Option>
  221. <Option value='maker'>证件发放员</Option>
  222. </Select>
  223. </FormItem>
  224. <FormItem label=" " colon={false}>
  225. <Button type="default" onClick={onCancel}>
  226. 取消
  227. </Button>
  228. <Button
  229. type="primary"
  230. loading={loading}
  231. htmlType="Submit"
  232. style={{ marginLeft: '4em' }}
  233. >
  234. 确认
  235. </Button>
  236. </FormItem>
  237. </Form>
  238. </Modal>
  239. </PageHeaderWrapper>
  240. );
  241. };