index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import { RootState } from "@/store";
  2. import { getUserInfoByIdAPI, userSaveAPI } from "@/store/action/A3User";
  3. import { SaveUserType } from "@/types";
  4. import { MessageFu } from "@/utils/message";
  5. import {
  6. Button,
  7. Cascader,
  8. Form,
  9. FormInstance,
  10. Input,
  11. Modal,
  12. Popconfirm,
  13. Select,
  14. } from "antd";
  15. import React, { useCallback, useEffect, useRef, useState } from "react";
  16. import { useSelector } from "react-redux";
  17. import styles from "./index.module.scss";
  18. import lastIdresArrFu from "./dataRes";
  19. type Props = {
  20. id: any;
  21. closePage: () => void;
  22. upTableList: () => void;
  23. addTableList: () => void;
  24. };
  25. function UserAdd({ id, closePage, upTableList, addTableList }: Props) {
  26. // 从仓库获取角色下拉列表信息
  27. const roleList = useSelector((state: RootState) => state.A3User.roleList);
  28. // 从仓库获取部门 级联 信息
  29. const deptList = useSelector((state: RootState) => state.A5Section.tableList);
  30. // 设置表单初始数据(区分编辑和新增)
  31. const FormBoxRef = useRef<FormInstance>(null);
  32. // 回显 级联 信息
  33. const idresArr = useCallback(
  34. (deptId: string) => {
  35. const arr = lastIdresArrFu(deptId, deptList);
  36. return arr;
  37. },
  38. [deptList]
  39. );
  40. const getInfoInAPIFu = useCallback(
  41. async (id: number) => {
  42. const res = await getUserInfoByIdAPI(id);
  43. if (res.code === 0) {
  44. FormBoxRef.current?.setFieldsValue({
  45. ...res.data,
  46. deptId: idresArr(res.data.deptId + ""),
  47. });
  48. }
  49. },
  50. [idresArr]
  51. );
  52. useEffect(() => {
  53. if (id) getInfoInAPIFu(id);
  54. // else {
  55. // FormBoxRef.current?.setFieldsValue({});
  56. // }
  57. }, [getInfoInAPIFu, id]);
  58. // 密码提示
  59. const [passFlag, setPassFlag] = useState("");
  60. // 没有通过校验
  61. const onFinishFailed = useCallback(() => {
  62. // return MessageFu.warning("有表单不符号规则!");
  63. }, []);
  64. // 通过校验点击确定
  65. const onFinish = useCallback(
  66. async (values: any) => {
  67. const obj: SaveUserType = {
  68. ...values,
  69. deptId: values.deptId[values.deptId.length - 1],
  70. id: id ? id : null,
  71. };
  72. const res: any = await userSaveAPI(obj);
  73. if (res.code === 0) {
  74. MessageFu.success(id ? "编辑成功!" : "新增成功!");
  75. if (id) upTableList();
  76. else addTableList();
  77. closePage();
  78. }
  79. // console.log("通过校验,点击确定");
  80. },
  81. [addTableList, closePage, id, upTableList]
  82. );
  83. return (
  84. <Modal
  85. wrapClassName={styles.userAdd}
  86. destroyOnClose
  87. open={true}
  88. title={id ? "编辑用户" : "新增用户"}
  89. footer={
  90. [] // 设置footer为空,去掉 取消 确定默认按钮
  91. }
  92. >
  93. <div className="userAddMain">
  94. <Form
  95. ref={FormBoxRef}
  96. name="basic"
  97. labelCol={{ span: 5 }}
  98. onFinish={onFinish}
  99. onFinishFailed={onFinishFailed}
  100. autoComplete="off"
  101. >
  102. <Form.Item
  103. label="用户名"
  104. name="userName"
  105. rules={[
  106. { required: true, message: "请输入用户名!" },
  107. {
  108. validator: (_: any, value: any) => {
  109. if (value) {
  110. const reg = new RegExp(/^[a-zA-Z0-9_]{6,15}$/);
  111. const flag = reg.test(value);
  112. if (flag) {
  113. setPassFlag(value);
  114. return Promise.resolve(value);
  115. } else {
  116. setPassFlag("");
  117. return Promise.reject(
  118. "用户名只包含字母、数字和下划线,最少 6 个字符!"
  119. );
  120. }
  121. } else {
  122. setPassFlag("");
  123. return Promise.resolve(value);
  124. }
  125. },
  126. },
  127. ]}
  128. getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
  129. >
  130. <Input
  131. style={{ width: 400 }}
  132. disabled={id}
  133. maxLength={15}
  134. showCount
  135. placeholder="请输入内容,6-15字;不能重复"
  136. />
  137. </Form.Item>
  138. <div className="e_row" hidden={id}>
  139. <div className="e_rowL">
  140. <span> </span>初始密码:
  141. </div>
  142. <div className="e_rowR">
  143. {passFlag ? `${passFlag}4dage` : "请先输入合法用户名"}
  144. </div>
  145. </div>
  146. <Form.Item
  147. label="手机号"
  148. name="phone"
  149. rules={[
  150. { required: true, message: "请输入手机号!" },
  151. // { max: 11, min: 11, message: "长度为11!" },
  152. {
  153. pattern: /^1[3-9][0-9]{9}$/,
  154. message: "请输入正确格式的手机号!",
  155. },
  156. ]}
  157. getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
  158. >
  159. <Input
  160. style={{ width: 400 }}
  161. maxLength={11}
  162. showCount
  163. placeholder="请输入11位手机号"
  164. />
  165. </Form.Item>
  166. <Form.Item
  167. label="所属部门"
  168. name="deptId"
  169. rules={[{ required: true, message: "请选择部门!" }]}
  170. >
  171. <Cascader
  172. style={{ width: 400 }}
  173. options={deptList}
  174. fieldNames={{ label: "name", value: "id" }}
  175. placeholder="请选择"
  176. />
  177. </Form.Item>
  178. <Form.Item
  179. label="角色"
  180. name="roleId"
  181. rules={[{ required: true, message: "请选择角色!" }]}
  182. >
  183. <Select
  184. style={{ width: 400 }}
  185. placeholder="请选择"
  186. options={roleList}
  187. />
  188. </Form.Item>
  189. <Form.Item
  190. label="真实姓名"
  191. name="realName"
  192. rules={[{ required: true, message: "请输入真实姓名!" }]}
  193. getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
  194. >
  195. <Input
  196. style={{ width: 400 }}
  197. maxLength={8}
  198. showCount
  199. placeholder="请输入内容"
  200. />
  201. </Form.Item>
  202. {/* 确定和取消按钮 */}
  203. <br />
  204. <Form.Item wrapperCol={{ offset: 9, span: 16 }}>
  205. <Button type="primary" htmlType="submit">
  206. 提交
  207. </Button>
  208. &emsp;
  209. <Popconfirm
  210. title="放弃编辑后,信息将不会保存!"
  211. okText="放弃"
  212. cancelText="取消"
  213. onConfirm={closePage}
  214. okButtonProps={{ loading: false }}
  215. >
  216. <Button>取消</Button>
  217. </Popconfirm>
  218. </Form.Item>
  219. </Form>
  220. </div>
  221. </Modal>
  222. );
  223. }
  224. const MemoUserAdd = React.memo(UserAdd);
  225. export default MemoUserAdd;