edit.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import React, { useState } from 'react';
  2. import { Col, Form, Input, message, Modal, Row, Upload } from 'antd';
  3. import { createUser, editUser } from '@/services/setting';
  4. import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';
  5. import type { UploadProps } from 'antd';
  6. import md5 from 'js-md5';
  7. interface userEditPros {
  8. visible: boolean;
  9. editCallback: () => void;
  10. params: any;
  11. }
  12. /**
  13. * 用户管理编辑
  14. * @param props
  15. * @constructor
  16. */
  17. const Edit: React.FC<userEditPros> = (props) => {
  18. const { visible, editCallback, params } = props;
  19. const [form] = Form.useForm();
  20. const uploadHeaders = { Authorization: `${localStorage.getItem('token')}` };
  21. const [fileUrl, setFileUrl] = useState(params && params.photo ? params.photo : '');
  22. const [loading, setLoading] = useState(false);
  23. const getBase64 = (img: any) => {
  24. const reader = new FileReader();
  25. reader.readAsDataURL(img);
  26. reader.onloadend = () => {
  27. const url = reader.result;
  28. setFileUrl(url);
  29. };
  30. };
  31. const filesProps: UploadProps = {
  32. maxCount: 1,
  33. action: '/web/v1/files',
  34. headers: uploadHeaders,
  35. listType: 'picture-card',
  36. showUploadList: false,
  37. onChange(info) {
  38. if (info.file.status === 'uploading') {
  39. setLoading(true);
  40. return;
  41. }
  42. if (info.file.status === 'done') {
  43. setLoading(false);
  44. getBase64(info.file.originFileObj);
  45. } else if (info.file.status === 'error') {
  46. setLoading(false);
  47. message.error('文件上传失败');
  48. }
  49. },
  50. };
  51. /**
  52. * 确定
  53. */
  54. const onOk = () => {
  55. form.validateFields().then((values) => {
  56. if (values) {
  57. const data = { ...values };
  58. if (values?.photo) {
  59. data.photo = values?.photo?.file?.response?.url;
  60. }
  61. if (values?.password) {
  62. data.password = md5(values.password);
  63. }
  64. if (params) {
  65. data.record_id = params.record_id;
  66. // 编辑
  67. editUser(data)
  68. .then((res) => {
  69. if (res && !res.error) {
  70. message.success('编辑成功');
  71. editCallback();
  72. } else {
  73. message.error(res?.error?.message);
  74. editCallback();
  75. }
  76. })
  77. .catch((e) => {
  78. message.error(e?.error?.message);
  79. editCallback();
  80. });
  81. } else {
  82. // 新增
  83. createUser(data)
  84. .then((res) => {
  85. if (res && !res.error) {
  86. message.success('保存成功');
  87. editCallback();
  88. } else {
  89. message.error(res?.error?.message);
  90. editCallback();
  91. }
  92. })
  93. .catch((e) => {
  94. message.error(e?.error?.message);
  95. editCallback();
  96. });
  97. }
  98. }
  99. });
  100. };
  101. /**
  102. * 取消
  103. */
  104. const onCancel = () => {
  105. editCallback();
  106. };
  107. const formItemLayout = {
  108. labelCol: {
  109. span: 6,
  110. },
  111. wrapperCol: {
  112. span: 16,
  113. },
  114. };
  115. const uploadButton = (
  116. <div>
  117. {loading ? <LoadingOutlined /> : <PlusOutlined />}
  118. <div className="ant-upload-text">上传</div>
  119. </div>
  120. );
  121. return (
  122. <Modal
  123. title={`${params ? '编辑' : '新增'}`}
  124. open={visible}
  125. onOk={onOk}
  126. onCancel={onCancel}
  127. width={800}
  128. >
  129. <Form form={form}>
  130. <Row>
  131. <Col span={12}>
  132. <Form.Item
  133. {...formItemLayout}
  134. name="user_name"
  135. label="用户姓名"
  136. rules={[{ required: true, message: '请输入用户姓名' }]}
  137. initialValue={params?.user_name}
  138. >
  139. <Input placeholder="请输入用户姓名" />
  140. </Form.Item>
  141. </Col>
  142. <Col span={12}>
  143. <Form.Item
  144. {...formItemLayout}
  145. name="password"
  146. label="密码"
  147. // initialValue={params?.password}
  148. >
  149. <Input type="password" placeholder="请输入密码" />
  150. </Form.Item>
  151. </Col>
  152. <Col span={12}>
  153. <Form.Item
  154. {...formItemLayout}
  155. name="real_name"
  156. label="真实姓名"
  157. rules={[{ required: true, message: '请输入真实姓名' }]}
  158. initialValue={params?.real_name}
  159. >
  160. <Input placeholder="请输入真实姓名" />
  161. </Form.Item>
  162. </Col>
  163. <Col span={12}>
  164. <Form.Item
  165. {...formItemLayout}
  166. name="phone"
  167. label="手机号"
  168. rules={[
  169. { required: true, message: '请输入手机号' },
  170. {
  171. pattern:
  172. /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/,
  173. message: '请输入正确的手机号',
  174. },
  175. ]}
  176. initialValue={params?.phone}
  177. >
  178. <Input placeholder="请输入手机号" />
  179. </Form.Item>
  180. </Col>
  181. <Col span={12}>
  182. <Form.Item {...formItemLayout} name="photo" label="头像" initialValue={[]}>
  183. <Upload {...filesProps}>
  184. {fileUrl === '' ? (
  185. uploadButton
  186. ) : (
  187. <img src={fileUrl} alt="data" style={{ width: '100%' }} />
  188. )}
  189. </Upload>
  190. </Form.Item>
  191. </Col>
  192. </Row>
  193. </Form>
  194. </Modal>
  195. );
  196. };
  197. export default Edit;