addUser.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import React, {
  2. useState, useEffect, useContext
  3. } from 'react';
  4. import { Modal, Form, Input, Select, Button, Switch, TreeSelect, message, Space } from 'antd';
  5. import apiObj from '@api/index';
  6. import utils from '@utils/index'
  7. import UserContext from './user-context';
  8. import { useSelector } from 'react-redux'
  9. import Item from 'antd/lib/list/Item';
  10. const { post, api, xPost } = apiObj;
  11. const { Option, OptGroup } = Select;
  12. const { organizationData } = utils;
  13. const { SHOW_PARENT } = TreeSelect;
  14. function AddUser(props) {
  15. useEffect(() => {
  16. getHospitalTree();
  17. getCreateRoles()
  18. }, []);
  19. const [form] = Form.useForm();
  20. const { userId, type, formData, roleList } = useContext(UserContext);
  21. const [treeData, setTreeData] = useState([]);
  22. const [treeRloe, setTreeRloe] = useState([]);
  23. const [addHospitalTreeVO, setAddHospitalTreeVO] = useState({
  24. depts: [],
  25. hospitals: []
  26. });
  27. const staticInfo = useSelector(state => {
  28. return state.staticInfo;
  29. });
  30. const { titleList } = staticInfo;
  31. const initialValues = formData;
  32. console.log(form.getFieldsValue())
  33. let addHospitalTreeVOs = {
  34. depts: [],
  35. hospitals: [],
  36. }
  37. //获取当前用户组织
  38. function getHospitalTree() {
  39. post(api.getHospitalTree).then((res) => {
  40. if (res.data.code === 200) {
  41. const data = res.data.data;
  42. const treeData = organizationData(data)
  43. setTreeData(treeData)
  44. }
  45. })
  46. }
  47. function addAttr(data) {
  48. for (var j = 0; j < data.length; j++) {
  49. data[j].title = data[j].name //添加title属性
  50. data[j].key = data[j].code //添加key属性
  51. if (data[j].children.length > 0) {
  52. addAttr(data[j].children)
  53. }
  54. }
  55. return data
  56. }
  57. //获取当前用于所属角色
  58. function getCreateRoles() {
  59. const params = {
  60. softwareId: ''
  61. }
  62. xPost(api.getCreateRoles, params).then((res) => {
  63. if (res.data.code === 200) {
  64. const data = res.data.data;
  65. let arr = JSON.parse(JSON.stringify(data).replaceAll(/name/g, 'title').replaceAll(/id/g, 'value'))
  66. setTreeRloe(arr)
  67. }
  68. })
  69. }
  70. // 去重
  71. function unique(arr) {
  72. return arr.filter(function (item, index, arr) {
  73. return arr.indexOf(item, 0) === index;
  74. });
  75. }
  76. function swichChange(val) {
  77. form.setFieldsValue({ status: val ? 1 : 0 })
  78. }
  79. // 判断元素存在格式
  80. function getSameNum(val, arr) {
  81. let processArr = arr.filter(function (value) {
  82. return value == val;
  83. })
  84. return processArr.length;
  85. }
  86. //递归获取科室
  87. function gethospitals(arr, val) {
  88. arr.forEach(item => {
  89. if (item.value == val) {
  90. if (item.children) {
  91. getdepts(item.children)
  92. }
  93. if (item.children && item.depts) {
  94. item.children.forEach(item => {
  95. addHospitalTreeVOs.depts.push(item.value.split('-')[1])
  96. })
  97. }
  98. } else {
  99. if (item.children) {
  100. gethospitals(item.children, val)
  101. }
  102. }
  103. })
  104. }
  105. //递归获取医院
  106. function getdepts(arr) {
  107. arr.forEach(item => {
  108. if (JSON.stringify(item.value).indexOf('-') < 0) {
  109. addHospitalTreeVOs.hospitals.push(item.value)
  110. }
  111. if (item.children && item.depts) {
  112. item.children.forEach(it => {
  113. addHospitalTreeVOs.depts.push(it.value.split('-')[1])
  114. })
  115. return
  116. }
  117. if (item.children) {
  118. getdepts(item.children)
  119. }
  120. })
  121. }
  122. const onChange = value => {
  123. value.forEach(it => {
  124. if (JSON.stringify(it).indexOf('-') > 0) {
  125. addHospitalTreeVOs.depts.push(it.split('-')[1])
  126. } else {
  127. addHospitalTreeVOs.hospitals.push(it)
  128. gethospitals(treeData, it)
  129. }
  130. })
  131. setAddHospitalTreeVO(addHospitalTreeVOs)
  132. console.log(form.getFieldsValue())
  133. };
  134. const onChangeRloe = value => {
  135. form.setFieldsValue({ roles: value })
  136. };
  137. const onFinish = values => {
  138. let params = values
  139. addHospitalTreeVO.hospitals = unique(addHospitalTreeVO.hospitals)
  140. params.addHospitalTreeVO = addHospitalTreeVO
  141. if (type == 2) {
  142. params.id = userId
  143. editUser(params)
  144. } else {
  145. addUser(params)
  146. }
  147. };
  148. function addUser(param) {
  149. post(api.addUser, param).then((res) => {
  150. if (res.data.code === 200) {
  151. props.userChange()
  152. message.success(res.data.message);
  153. form.resetFields();
  154. } else {
  155. message.error(res.data.message);
  156. }
  157. })
  158. }
  159. function editUser(param) {
  160. post(api.updateUser, param).then((res) => {
  161. if (res.data.code === 200) {
  162. props.userChange()
  163. message.success(res.data.message);
  164. form.resetFields();
  165. } else {
  166. message.error(res.data.message);
  167. }
  168. })
  169. }
  170. function cancel() {
  171. props.userChange()
  172. }
  173. return (
  174. <>
  175. <Form
  176. labelCol={{ span: 6 }}
  177. wrapperCol={{ span: 16 }}
  178. form={form}
  179. name="register"
  180. onFinish={onFinish}
  181. initialValues={initialValues}
  182. >
  183. <Form.Item
  184. name="username"
  185. label="用户名"
  186. rules={[
  187. {
  188. required: true,
  189. message: '请输入用户名',
  190. },
  191. ]}
  192. >
  193. {type == 3 ?
  194. <span>{initialValues.username}</span>
  195. :
  196. <Input placeholder="用户名是组织中唯一标识,请勿重复" />
  197. }
  198. </Form.Item>
  199. <Form.Item
  200. name="mobilePhone"
  201. label="手机号码"
  202. rules={[
  203. {
  204. required: true,
  205. message: '请输入手机号码',
  206. },
  207. ]}
  208. >
  209. {type == 3 ?
  210. <span>{initialValues.mobilePhone}</span>
  211. :
  212. <Input placeholder="请输入手机号码" />
  213. }
  214. </Form.Item>
  215. <Form.Item
  216. name="password"
  217. label="初始密码"
  218. rules={[
  219. {
  220. required: true,
  221. message: '8-12位大小写字母、数字、特殊字符',
  222. },
  223. ({ getFieldValue }) => ({
  224. validator(_, value) {
  225. if (value.length < 8 || value.length > 12) {
  226. return Promise.reject(new Error('密码长度8-12位'));
  227. }
  228. },
  229. }),
  230. ]}
  231. hasFeedback
  232. >
  233. {type == 3 ?
  234. <span>{initialValues.password}</span>
  235. :
  236. <Input.Password placeholder="8-12位大小写字母、数字、特殊字符" />
  237. }
  238. </Form.Item>
  239. <Form.Item
  240. name="againpassword"
  241. label="确认密码"
  242. dependencies={['password']}
  243. hasFeedback
  244. rules={[
  245. {
  246. required: true,
  247. message: '8-12位大小写字母、数字、特殊字符',
  248. },
  249. ({ getFieldValue }) => ({
  250. validator(_, value) {
  251. if (!value || getFieldValue('password') === value) {
  252. return Promise.resolve();
  253. }
  254. return Promise.reject(new Error('两次密码不一致,请确认密码'));
  255. },
  256. }),
  257. ]}
  258. >
  259. {type == 3 ?
  260. <span>{initialValues.password}</span>
  261. :
  262. <Input.Password placeholder="8-12位大小写字母、数字、特殊字符" />
  263. }
  264. </Form.Item>
  265. <Form.Item
  266. name="name"
  267. label="姓名"
  268. rules={[{ required: true, message: '请输入姓名', whitespace: true }]}
  269. >
  270. {type == 3 ?
  271. <span>{initialValues.name}</span>
  272. :
  273. <Input placeholder="请输入姓名" />
  274. }
  275. </Form.Item>
  276. <Form.Item
  277. name="idcard"
  278. label="身份证号码"
  279. >
  280. {type == 3 ?
  281. <span>{initialValues.idcard}</span>
  282. :
  283. <Input placeholder="请输入身份证号码" />
  284. }
  285. </Form.Item>
  286. <Form.Item
  287. name="addHospitalTreeVO"
  288. label="所属组织"
  289. rules={[{ required: true, message: '请选择所属组织' }]}
  290. >
  291. <TreeSelect
  292. showSearch={false}
  293. treeData={treeData}
  294. onChange={onChange}
  295. maxTagCount={1}
  296. treeCheckable
  297. showCheckedStrategy={SHOW_PARENT}
  298. placeholder="请选择组织"
  299. style={{ width: '100%' }}
  300. />
  301. </Form.Item>
  302. <Form.Item
  303. name="titleId"
  304. label="职称"
  305. >
  306. {type == 3 ?
  307. <span>{initialValues.idcard}</span>
  308. :
  309. <Select placeholder="请选择职称">
  310. {titleList.map((item) => {
  311. return (
  312. <Option value={item.name} key={item.name}>{item.val}</Option>
  313. )
  314. })}
  315. </Select>
  316. }
  317. </Form.Item>
  318. <Form.Item
  319. name="jobNo"
  320. label="工号"
  321. >
  322. {type == 3 ?
  323. <span>{initialValues.jobNo}</span>
  324. :
  325. <Input placeholder="请输入工号" />
  326. }
  327. </Form.Item>
  328. <Form.Item
  329. name="roles"
  330. label="所属角色"
  331. rules={[{ required: true, message: '请选择角色' }]}
  332. >
  333. {type == 3 ?
  334. <div>
  335. {roleList.map((it, i) => {
  336. return (
  337. <span key={it.id}>{it.roleName}、</span>
  338. );
  339. })}
  340. </div>
  341. :
  342. <TreeSelect
  343. showSearch={false}
  344. treeData={treeRloe}
  345. treeCheckable
  346. onChange={onChangeRloe}
  347. showCheckedStrategy={SHOW_PARENT}
  348. placeholder="请选择角色"
  349. style={{ width: '100%' }}
  350. disabled={type == 3 ? true : false}
  351. />
  352. }
  353. </Form.Item>
  354. <Form.Item
  355. name="orderNo"
  356. label="排序"
  357. >
  358. {type == 3 ?
  359. <span>{initialValues.orderNo}</span>
  360. :
  361. <Input placeholder="大于0的整数" />
  362. }
  363. </Form.Item>
  364. <Form.Item
  365. name="status"
  366. valuePropName="checked"
  367. label="当前状态"
  368. rules={[{ required: true, message: '请选择状态' }]}
  369. >
  370. {type == 3 ?
  371. <span>{initialValues.status == 1 ? '启用' : '禁用'}</span>
  372. :
  373. <Switch onChange={swichChange} />
  374. }
  375. </Form.Item>
  376. {type == 3 ?
  377. <Form.Item wrapperCol={{ offset: 10, span: 16 }}>
  378. <Space size="middle">
  379. <Button type="primary" onClick={e => cancel()}>
  380. 返回
  381. </Button>
  382. </Space>
  383. </Form.Item>
  384. :
  385. <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
  386. <Space size="middle">
  387. <Button htmlType="button" onClick={e => cancel()}>
  388. 取消
  389. </Button>
  390. <Button type="primary" htmlType="submit">
  391. 保存
  392. </Button>
  393. </Space>
  394. </Form.Item>}
  395. </Form>
  396. </>
  397. );
  398. }
  399. export default AddUser;