123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import { Button, Popconfirm, Modal, Form, Input, message, Radio, Select } from 'antd';
  3. import { PlusOutlined } from '@ant-design/icons';
  4. import { getCooperativeList } from '@/services/cooperative';
  5. import {
  6. getUserList,
  7. addUser,
  8. deleteUser,
  9. updateUser,
  10. getDefaultPassword,
  11. getUserDetail,
  12. } from '@/services/user';
  13. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  14. import PageTable from '@/components/PageTable';
  15. import moment from 'moment';
  16. import Search from '@/components/CooperativeSearch';
  17. const FormItem = Form.Item;
  18. const { Option } = Select;
  19. const formatterTime = (val) => {
  20. return val && val !== '-' ? moment(val).format('YYYY-MM-DD') : '-';
  21. };
  22. export default (props) => {
  23. //编辑弹窗
  24. const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 14 } };
  25. const [form] = Form.useForm();
  26. const [editModal, setEditModal] = useState(false);
  27. const [loading, setLoading] = useState(false);
  28. const [userId, setuserId] = useState();
  29. const [password, setPassWord] = useState('');
  30. const [cooperativeList, setCooperativeList] = useState([]);
  31. //列表数据
  32. const actionRef = useRef();
  33. // 编辑弹窗的表单提交
  34. const Submit = (values) => {
  35. const newData = { ...values };
  36. if (!newData.sex && newData.sex !== 0) {
  37. newData.sex = 1;
  38. }
  39. setLoading(true);
  40. if (!/^1[0-9]{10}$/.test(newData.phone)) {
  41. message.warning('请输入正确的十一位手机号');
  42. setLoading(false);
  43. return false;
  44. }
  45. if (userId) {
  46. updateUser(userId, newData).then(() => {
  47. setLoading(false);
  48. message.success(`修改成功`);
  49. onCancel();
  50. actionRef.current.reload();
  51. });
  52. } else {
  53. addUser(newData)
  54. .then(() => {
  55. setLoading(false);
  56. message.success(`保存成功`);
  57. onCancel();
  58. actionRef.current.reload();
  59. })
  60. .catch((err) => {
  61. setLoading(false);
  62. message.error(err.message || err);
  63. });
  64. }
  65. };
  66. //表单点击取消按钮
  67. const onCancel = () => {
  68. setuserId();
  69. form.resetFields();
  70. setEditModal(false);
  71. };
  72. // 弹窗表单中机构搜索框改变事件目前没用
  73. const handelChange = () => {};
  74. // 列表点击编辑按钮
  75. const handelEdit = (val) => {
  76. setuserId(val.userId);
  77. setEditModal(true);
  78. };
  79. //列表点击删除按钮
  80. const handleDelete = (id) => {
  81. deleteUser(id)
  82. .then(() => {
  83. message.success('删除成功');
  84. actionRef.current.reload();
  85. })
  86. .catch((err) => {
  87. message.error(err);
  88. });
  89. };
  90. //列表切换人员状态方法
  91. const handleOK = (record, data) => {
  92. const title = record.status
  93. ? '您确定要将该人员状态变更为禁用吗? 禁用后该人员将不能登录'
  94. : '您确定要将该人员状态变更为启用吗? 启用后该人员可以登录!';
  95. Modal.confirm({
  96. title: title,
  97. okText: '确认',
  98. cancelText: '取消',
  99. onOk() {
  100. updateUser(record.userId, { ...record, status: record.status === 1 ? 0 : 1 })
  101. .then((res) => {
  102. message.success('操作成功');
  103. actionRef.current.reload();
  104. })
  105. .catch((err) => {
  106. message.error(err);
  107. });
  108. },
  109. });
  110. };
  111. useEffect(() => {
  112. //获取账号默认密码
  113. getDefaultPassword().then((res) => {
  114. setPassWord(res);
  115. });
  116. //获取机构列表数据
  117. getCooperativeList().then((res) => {
  118. setCooperativeList(res.records);
  119. });
  120. if (userId) {
  121. getUserDetail(userId).then((res) => {
  122. if (res.orgId === '-1') {
  123. form.setFieldsValue({ ...res, orgId: cooperativeList[0].orgId });
  124. } else {
  125. form.setFieldsValue(res);
  126. }
  127. });
  128. } else {
  129. form.resetFields();
  130. }
  131. }, [userId]);
  132. const actions = () => [
  133. <Button key="add" type="primary" icon={<PlusOutlined />} onClick={() => setEditModal(true)}>
  134. 新增
  135. </Button>,
  136. ];
  137. const columns = [
  138. {
  139. title: '用户名',
  140. dataIndex: 'userName',
  141. key: 'userName',
  142. },
  143. {
  144. title: '性别',
  145. dataIndex: 'sex',
  146. key: 'sex',
  147. width: 80,
  148. render: (_, record) => {
  149. return record.sex === 1 ? '男' : record.sex === 0 ? '女' : '未知';
  150. },
  151. search: false,
  152. },
  153. {
  154. title: '手机号',
  155. dataIndex: 'phone',
  156. key: 'phone',
  157. width: 120,
  158. },
  159. {
  160. title: '邮箱',
  161. dataIndex: 'email',
  162. key: 'email',
  163. },
  164. {
  165. title: '注册时间',
  166. dataIndex: 'createDate',
  167. key: 'createDate',
  168. render: formatterTime,
  169. search: false,
  170. },
  171. {
  172. title: '状态',
  173. dataIndex: 'status',
  174. key: 'status',
  175. renderFormItem: () => {
  176. return (
  177. <Select placeholder="请选择">
  178. <Option value={1}>启用</Option>
  179. <Option value={0}>禁用</Option>
  180. </Select>
  181. );
  182. },
  183. render: (_, record) => {
  184. return record.status === 1 ? '启用' : '未启用';
  185. },
  186. },
  187. {
  188. title: '操作',
  189. valueType: 'option',
  190. render: (_, record) => [
  191. <Button type="link" key={1} onClick={() => handleOK(record)}>
  192. {record.status === 0 ? '启用' : '禁用'}
  193. </Button>,
  194. <a key={2} onClick={() => handelEdit(record)}>
  195. 编辑
  196. </a>,
  197. <Popconfirm
  198. key={3}
  199. title="您是否确认删除 ?"
  200. onConfirm={() => handleDelete(record.userId)}
  201. okText="确定"
  202. cancelText="取消"
  203. >
  204. <a href="#">删除</a>
  205. </Popconfirm>,
  206. ],
  207. },
  208. ];
  209. return (
  210. <PageHeaderWrapper>
  211. <PageTable
  212. request={getUserList}
  213. // expfunc={exportPersonList}
  214. columns={columns}
  215. actionRef={actionRef}
  216. rowKey="userId"
  217. options={false}
  218. toolBarRender={actions}
  219. scroll={{ x: 1000 }}
  220. />
  221. <Modal
  222. title={userId ? '人员编辑' : '人员新增'}
  223. visible={editModal}
  224. onCancel={onCancel}
  225. keyboard={false}
  226. maskClosable={false}
  227. destroyOnClose={true}
  228. footer={null}
  229. >
  230. <Form {...formItemLayout} onFinish={Submit} form={form}>
  231. <FormItem label="登录账号">
  232. <Input.Group compact>
  233. <Form.Item
  234. name="loginName"
  235. noStyle
  236. rules={[{ required: true, message: '请输入登录账号' }]}
  237. >
  238. <Input placeholder="请输入" />
  239. </Form.Item>
  240. <span style={{ opacity: '0.7' }}>默认密码{password}</span>
  241. </Input.Group>
  242. </FormItem>
  243. <FormItem label="用户名" name="userName" rules={[{ required: true, message: '请输入' }]}>
  244. <Input placeholder="请输入" />
  245. </FormItem>
  246. <FormItem label="性别" name="sex">
  247. <Radio.Group name="sex" defaultValue={1}>
  248. <Radio value={1}>男</Radio>
  249. <Radio value={0}>女</Radio>
  250. </Radio.Group>
  251. </FormItem>
  252. <FormItem label="手机号" name="phone" rules={[{ required: true, message: '请输入' }]}>
  253. <Input maxLength="11" placeholder="请输入" />
  254. </FormItem>
  255. <FormItem label="邮箱" name="email" rules={[{ required: true, message: '请输入' }]}>
  256. <Input placeholder="请输入" />
  257. </FormItem>
  258. <FormItem label="所属机构" name="orgId" rules={[{ required: true, message: '请输入' }]}>
  259. <Search placeholder="请选择机构" onChange={handelChange} />
  260. {/* <Select
  261. placeholder="请选择机构"
  262. showSearch
  263. onSearch={handelFormSearch}
  264. onChange={handelFormSearch}
  265. >
  266. {cooperativeList.map((item) => (
  267. <Option value={item.orgId} key={item.orgId}>
  268. {item.name}
  269. </Option>
  270. ))}
  271. </Select> */}
  272. </FormItem>
  273. <FormItem label="状态" name="status" rules={[{ required: true, message: '请选择' }]}>
  274. <Select placeholder="请选择是否启用">
  275. <Option value={1}>启用</Option>
  276. <Option value={0}>禁用</Option>
  277. </Select>
  278. </FormItem>
  279. <FormItem label=" " colon={false}>
  280. <Button type="default" onClick={onCancel}>
  281. 取消
  282. </Button>
  283. <Button
  284. type="primary"
  285. loading={loading}
  286. htmlType="Submit"
  287. style={{ marginLeft: '4em' }}
  288. >
  289. 确认
  290. </Button>
  291. </FormItem>
  292. </Form>
  293. </Modal>
  294. </PageHeaderWrapper>
  295. );
  296. };