Edit.jsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React from 'react';
  2. import { Button, Card, Form, Input, Select } from 'antd';
  3. import useBool from '@/utils/hooks/useBool';
  4. import { getSysRole } from '@/service/sysrole';
  5. import { postSysUser, getSysUserById } from '@/service/sysuser';
  6. import { useSearchParams, useNavigate } from 'react-router-dom';
  7. import Page from '@/components/Page';
  8. import {formItemLayout, tailFormItemLayout} from '@/utils/form';
  9. import { getSysOrg } from '@/service/sysorg';
  10. import { getSysPosition } from '@/service/sysposition';
  11. import Account from './Account';
  12. const { Option } = Select;
  13. export default (props) => {
  14. const [loading, startLoading, cancelLoading] = useBool();
  15. const [submiting, startSubmit, cancelSubmit] = useBool();
  16. const [open, setShow, setHidden] = useBool(false);
  17. const [roleList, setRoleList] = React.useState([]);
  18. const [orgList, setOrgList] = React.useState([]);
  19. const [org, setOrg] = React.useState();
  20. const [posList, setPosList] = React.useState([]);
  21. const [searchParams] = useSearchParams();
  22. const [form] = Form.useForm();
  23. const [user, setUser] = React.useState();
  24. const navigate = useNavigate();
  25. const id = searchParams.get('id');
  26. const onFinish = (values) => {
  27. const rolesList = (values.rolesList || []).map(x => ({ roleId: x }));
  28. startSubmit();
  29. postSysUser({ ...values, rolesList, userId: id }).then(res => {
  30. cancelSubmit();
  31. if (id) {
  32. navigate(-1);
  33. } else {
  34. navigate(`/system/user/edit?id=${res.userId}`, { replace: true })
  35. }
  36. }).catch(() => {
  37. cancelSubmit();
  38. });
  39. }
  40. React.useEffect(() => {
  41. getSysRole({ pageSize: 500 }).then(res => setRoleList(res.records || []));
  42. getSysOrg({ pageSize: 500 }).then(res => setOrgList(res.records || []));
  43. }, []);
  44. React.useEffect(() => {
  45. if (org) {
  46. getSysPosition({orgId: org, pageSize: 500}).then(res => setPosList(res.records || []));
  47. }
  48. }, [org])
  49. React.useEffect(() => {
  50. if (id) {
  51. startLoading();
  52. getSysUserById(id).then(res => {
  53. setOrg(res.orgId);
  54. setUser(res);
  55. form.setFieldsValue({
  56. ...res,
  57. rolesList: (res.rolesList || []).map(x => x.roleId),
  58. });
  59. cancelLoading();
  60. }).catch(() => {
  61. cancelLoading();
  62. });
  63. } else {
  64. // form.setFieldsValue({ password: '123456' });
  65. }
  66. }, [id]);
  67. return (
  68. <Page>
  69. <Card loading={loading}>
  70. <Form onFinish={onFinish} form={form} {...formItemLayout} scrollToFirstError style={{ maxWidth: '1000px' }}>
  71. <Form.Item
  72. name="name"
  73. label="姓名"
  74. rules={[
  75. {
  76. required: true,
  77. message: '请填写姓名',
  78. },
  79. ]}
  80. >
  81. <Input />
  82. </Form.Item>
  83. <Form.Item
  84. name="phone"
  85. label="手机号"
  86. rules={[
  87. {
  88. required: true,
  89. message: '请填写手机号',
  90. },
  91. ]}
  92. >
  93. <Input />
  94. </Form.Item>
  95. <Form.Item
  96. name="orgId"
  97. label="单位"
  98. >
  99. <Select onChange={setOrg}>
  100. {
  101. orgList.map(x => (
  102. <Option key={x.orgId}>{x.name}</Option>
  103. ))
  104. }
  105. </Select>
  106. </Form.Item>
  107. <Form.Item
  108. name="positionId"
  109. label="岗位"
  110. >
  111. <Select>
  112. {
  113. posList.map(x => (
  114. <Option key={x.positionId}>{x.name}</Option>
  115. ))
  116. }
  117. </Select>
  118. </Form.Item>
  119. <Form.Item
  120. name="rolesList"
  121. label="角色"
  122. >
  123. <Select
  124. mode="multiple"
  125. allowClear
  126. style={{ width: '100%' }}
  127. placeholder="请选择角色"
  128. >
  129. {
  130. roleList.map(role => (<Option key={role.roleId}>{role.name}</Option>))
  131. }
  132. </Select>
  133. </Form.Item>
  134. <Form.Item {...tailFormItemLayout}>
  135. <Button loading={submiting} type="primary" htmlType="submit">
  136. 保存
  137. </Button>
  138. <Button style={{ marginLeft: '2em' }} onClick={() => navigate(-1)}>
  139. 返回
  140. </Button>
  141. <Button disabled={!id} style={{ marginLeft: '2em' }} onClick={setShow}>
  142. 登录账户
  143. </Button>
  144. </Form.Item>
  145. </Form>
  146. </Card>
  147. <Account open={open} user={user} onSuccess={setHidden} onCancel={setHidden} />
  148. </Page>
  149. )
  150. }