index.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { MessageFu } from '@/utils/message'
  2. import { Button, Cascader, Checkbox, Form, FormInstance, Input, Modal, Select } from 'antd'
  3. import React, { useCallback, useEffect, useRef } from 'react'
  4. import styles from './index.module.scss'
  5. import MyPopconfirm from '@/components/MyPopconfirm'
  6. import { getUserInfoByIdAPI, userSaveAPI } from '@/store/action/Z6user'
  7. import { useDispatch, useSelector } from 'react-redux'
  8. import { RootState } from '@/store'
  9. import { Z5_APIgetList } from '@/store/action/Z5role'
  10. import { treeLastIdFindFatherFu } from '../data'
  11. import { UserTableAPIType } from '@/types'
  12. type Props = {
  13. id: any
  14. closePage: () => void
  15. upTableList: (formOld: UserTableAPIType) => void
  16. addTableList: () => void
  17. formOld: UserTableAPIType
  18. }
  19. function UserAdd({ id, closePage, upTableList, addTableList, formOld }: Props) {
  20. // 角色
  21. const dispatch = useDispatch()
  22. useEffect(() => {
  23. dispatch(
  24. Z5_APIgetList({
  25. pageNum: 1,
  26. pageSize: 99999,
  27. searchKey: ''
  28. })
  29. )
  30. }, [dispatch])
  31. const { list: roleList } = useSelector((state: RootState) => state.Z5role.tableInfo)
  32. // 所属部门
  33. const { treeData } = useSelector((state: RootState) => state.Z4organization)
  34. // 设置表单初始数据(区分编辑和新增)
  35. const FormBoxRef = useRef<FormInstance>(null)
  36. const getInfoInAPIFu = useCallback(
  37. async (id: number) => {
  38. if (treeData && treeData.length) {
  39. const res = await getUserInfoByIdAPI(id)
  40. const data = res.data
  41. const deptId = treeLastIdFindFatherFu(treeData, data.deptId + '', 'id')
  42. FormBoxRef.current?.setFieldsValue({
  43. ...data,
  44. deptId
  45. })
  46. }
  47. // console.log("是编辑,在这里发请求拿数据", res);
  48. },
  49. [treeData]
  50. )
  51. // 没有通过校验
  52. const onFinishFailed = useCallback(() => {
  53. // return MessageFu.warning("有表单不符号规则!");
  54. }, [])
  55. useEffect(() => {
  56. if (id) getInfoInAPIFu(id)
  57. else {
  58. FormBoxRef.current?.setFieldsValue({})
  59. }
  60. }, [getInfoInAPIFu, id])
  61. // 通过校验点击确定
  62. const onFinish = useCallback(
  63. async (values: any) => {
  64. const obj = {
  65. ...values,
  66. id: id ? id : null
  67. }
  68. const deptIdRes = obj.deptId
  69. if (deptIdRes) obj.deptId = Number(deptIdRes[deptIdRes.length - 1])
  70. obj.isLeader = obj.isLeader ? 1 : 0
  71. // if (1 + 1 === 2) {
  72. // console.log(123, obj)
  73. // return
  74. // }
  75. const res: any = await userSaveAPI(obj)
  76. if (res.code === 0) {
  77. MessageFu.success(id ? '编辑成功!' : '新增成功!')
  78. if (id) upTableList(formOld)
  79. else addTableList()
  80. closePage()
  81. }
  82. // console.log("通过校验,点击确定");
  83. },
  84. [addTableList, closePage, formOld, id, upTableList]
  85. )
  86. return (
  87. <Modal
  88. wrapClassName={styles.userAdd}
  89. destroyOnClose
  90. open={true}
  91. title={id ? '编辑用户' : '新增用户'}
  92. footer={
  93. [] // 设置footer为空,去掉 取消 确定默认按钮
  94. }
  95. >
  96. <div className='userAddMain'>
  97. <Form
  98. scrollToFirstError={true}
  99. ref={FormBoxRef}
  100. name='basic'
  101. labelCol={{ span: 5 }}
  102. onFinish={onFinish}
  103. onFinishFailed={onFinishFailed}
  104. autoComplete='off'
  105. >
  106. <Form.Item
  107. label='登录账号'
  108. name='userName'
  109. rules={[
  110. { required: true, message: '请输入账号名!' },
  111. { min: 6, message: '最少6个字!' }
  112. ]}
  113. getValueFromEvent={e => e.target.value.replace(/\s+/g, '')}
  114. >
  115. <Input disabled={id} maxLength={15} showCount placeholder='请输入内容' />
  116. </Form.Item>
  117. <Form.Item
  118. label='真实姓名'
  119. name='realName'
  120. rules={[{ required: true, message: '请输入真实姓名!' }]}
  121. getValueFromEvent={e => e.target.value.replace(/\s+/g, '')}
  122. >
  123. <Input maxLength={10} showCount placeholder='请输入内容' />
  124. </Form.Item>
  125. <Form.Item
  126. label='手机号'
  127. name='phone'
  128. rules={[
  129. { required: true, message: '请输入手机号!' },
  130. {
  131. pattern: /^1[3-9][0-9]{9}$/,
  132. message: '请输入正确格式的手机号!'
  133. }
  134. ]}
  135. getValueFromEvent={e => e.target.value.replace(/\s+/g, '')}
  136. >
  137. <Input maxLength={11} showCount placeholder='请输入11位手机号' />
  138. </Form.Item>
  139. <div className='Z6arow'>
  140. <Form.Item
  141. label='所属部门'
  142. name='deptId'
  143. rules={[{ required: true, message: '请选择所属部门部门!' }]}
  144. >
  145. <Cascader
  146. allowClear={false}
  147. changeOnSelect
  148. style={{ width: 240 }}
  149. options={treeData}
  150. fieldNames={{ label: 'name', value: 'id', children: 'children' }}
  151. placeholder='请选择'
  152. />
  153. </Form.Item>
  154. <Form.Item
  155. label=''
  156. name='isLeader'
  157. valuePropName='checked'
  158. getValueFromEvent={e => (e.target.checked ? 1 : 0)}
  159. >
  160. <Checkbox>设为主管</Checkbox>
  161. </Form.Item>
  162. </div>
  163. <Form.Item
  164. label='角色'
  165. name='roleId'
  166. rules={[{ required: true, message: '请选择角色!' }]}
  167. >
  168. <Select
  169. style={{ width: 240 }}
  170. placeholder='请选择'
  171. options={roleList.filter(v => v.id !== 1)}
  172. fieldNames={{ value: 'id', label: 'roleName' }}
  173. />
  174. </Form.Item>
  175. {id ? null : <div className='passTit'>* 默认密码 Aa147852</div>}
  176. {/* 确定和取消按钮 */}
  177. <br />
  178. <Form.Item wrapperCol={{ offset: 9, span: 16 }}>
  179. <Button type='primary' htmlType='submit'>
  180. 提交
  181. </Button>
  182. &emsp;
  183. <MyPopconfirm txtK='取消' onConfirm={closePage} />
  184. </Form.Item>
  185. </Form>
  186. </div>
  187. </Modal>
  188. )
  189. }
  190. const MemoUserAdd = React.memo(UserAdd)
  191. export default MemoUserAdd