index.jsx 8.4KB

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