Edit.jsx 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React from 'react';
  2. import { Button, Card, Form, Input, Select } from 'antd';
  3. import useBool from '@/utils/hooks/useBool';
  4. import { getRoleList } from '@/services/role';
  5. import { saveUser, getUser } from '@/services/user';
  6. import { useSearchParams, useNavigate } from 'react-router-dom';
  7. import md5 from 'md5';
  8. const formItemLayout = {
  9. labelCol: {
  10. xs: { span: 24 },
  11. sm: { span: 8 },
  12. },
  13. wrapperCol: {
  14. xs: { span: 24 },
  15. sm: { span: 16 },
  16. },
  17. };
  18. const tailFormItemLayout = {
  19. wrapperCol: {
  20. xs: {
  21. span: 24,
  22. offset: 0,
  23. },
  24. sm: {
  25. span: 16,
  26. offset: 8,
  27. },
  28. },
  29. };
  30. const { Option } = Select;
  31. export default (props) => {
  32. const [loading, startLoading, cancelLoading] = useBool();
  33. const [submiting, startSubmit, cancelSubmit] = useBool();
  34. const [roleList, setRoleList] = React.useState([]);
  35. const [searchParams] = useSearchParams();
  36. const [form] = Form.useForm();
  37. const userRef = React.useRef();
  38. const navigate = useNavigate();
  39. const id = searchParams.get('id');
  40. const onFinish = (values) => {
  41. const password = values.password ? md5(values.password) : undefined;
  42. const loginName = values.phone;
  43. const rolesList = (values.rolesList || []).map(x => ({ id: x }));
  44. startSubmit();
  45. saveUser({ ...values, loginName, password, rolesList, id }).then(res => {
  46. cancelSubmit();
  47. navigate(-1);
  48. }).catch(() => {
  49. cancelSubmit();
  50. });
  51. }
  52. React.useEffect(() => {
  53. getRoleList().then(setRoleList);
  54. }, []);
  55. React.useEffect(() => {
  56. if (id) {
  57. startLoading();
  58. getUser(id).then(user => {
  59. userRef.current = user;
  60. form.setFieldsValue({
  61. ...user,
  62. rolesList: (user.rolesList || []).map(x => `${x.id}`),
  63. });
  64. cancelLoading();
  65. }).catch(() => {
  66. cancelLoading();
  67. });
  68. } else {
  69. form.setFieldsValue({ password: '123456' });
  70. }
  71. }, [id]);
  72. return (
  73. <Card loading={loading}>
  74. <Form onFinish={onFinish} form={form} {...formItemLayout} scrollToFirstError>
  75. <Form.Item
  76. name="name"
  77. label="姓名"
  78. rules={[
  79. {
  80. required: true,
  81. message: '请填写姓名',
  82. },
  83. ]}
  84. >
  85. <Input />
  86. </Form.Item>
  87. <Form.Item
  88. name="dept"
  89. label="部门"
  90. >
  91. <Input />
  92. </Form.Item>
  93. <Form.Item
  94. name="phone"
  95. label="手机号"
  96. >
  97. <Input />
  98. </Form.Item>
  99. <Form.Item label="账号">
  100. 系统账号与手机号相同
  101. </Form.Item>
  102. <Form.Item
  103. name="password"
  104. label="密码"
  105. extra={id ? '填写密码意味着重置该账户密码' : '默认密码 123456'}
  106. hasFeedback
  107. >
  108. <Input.Password allowClear />
  109. </Form.Item>
  110. <Form.Item
  111. name="rolesList"
  112. label="角色"
  113. >
  114. <Select
  115. mode="multiple"
  116. allowClear
  117. style={{ width: '100%' }}
  118. placeholder="请选择角色"
  119. >
  120. {
  121. roleList.map(role => (<Option key={role.id}>{role.name}</Option>))
  122. }
  123. </Select>
  124. </Form.Item>
  125. <Form.Item {...tailFormItemLayout}>
  126. <Button loading={submiting} type="primary" htmlType="submit">
  127. 保存
  128. </Button>
  129. <Button style={{ marginLeft: '2em' }} onClick={() => navigate(-1)}>
  130. 返回
  131. </Button>
  132. </Form.Item>
  133. </Form>
  134. </Card>
  135. )
  136. }