AddModal.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { memo, useEffect, useState } from 'react'
  2. import { LockFlag } from '#/enum'
  3. import { toast } from 'sonner'
  4. import { fetchTree } from '@/api/services/dept'
  5. import { fetchPostList } from '@/api/services/post'
  6. import { deptRoleList } from '@/api/services/role'
  7. import { addUser } from '@/api/services/user'
  8. import { Col, Form, Input, Modal, Radio, Row, Select, TreeSelect } from 'antd'
  9. export type AddModalProps = {
  10. show: boolean
  11. onCancel: VoidFunction
  12. getList: VoidFunction
  13. }
  14. const formData: Partial<API.AddUserParams> = {}
  15. export const AddModal = memo(({ show = false, onCancel, getList }: AddModalProps) => {
  16. // 查询部门树
  17. const [treeDeptData, setTreeDeptData] = useState<API.DeptTreeResult>([])
  18. const getDeptTree = async () => {
  19. const res = await fetchTree()
  20. if (res?.code === '2000') {
  21. setTreeDeptData(res.data)
  22. }
  23. }
  24. // 查询岗位列表
  25. const [postOptions, setPostOptions] = useState<API.PostListResult>([])
  26. const getPostList = async () => {
  27. const res = await fetchPostList()
  28. if (res?.code === '2000') {
  29. setPostOptions(res.data)
  30. }
  31. }
  32. // 查询角色列表
  33. const [rolesOptions, setRolesOptions] = useState<API.RoleListResult>([])
  34. const getRoleList = async () => {
  35. const res = await deptRoleList()
  36. if (res?.code === '2000') {
  37. setRolesOptions(res.data)
  38. }
  39. }
  40. useEffect(() => {
  41. getDeptTree()
  42. getPostList()
  43. getRoleList()
  44. }, [])
  45. const [form] = Form.useForm()
  46. const onOkFn = () => {
  47. form.validateFields().then((values) => {
  48. console.log(values)
  49. addUser(values as API.AddUserParams).then((res) => {
  50. if (res) {
  51. console.log(res)
  52. getList()
  53. onCancelFn()
  54. toast.success('创建成功', {
  55. position: 'top-center'
  56. })
  57. }
  58. })
  59. })
  60. }
  61. const onCancelFn = () => {
  62. form.resetFields()
  63. onCancel()
  64. }
  65. return (
  66. <Modal title='新增' open={show} width={900} onOk={onOkFn} onCancel={onCancelFn}>
  67. {/* 表单名称,会作为表单字段 id 前缀使用,不加可能会造成表单id冲突 */}
  68. <Form name='addUserModalForm' initialValues={formData} form={form} layout='vertical'>
  69. <Row gutter={16}>
  70. <Col span={12}>
  71. <Form.Item<API.AddUserParams>
  72. label='用户名'
  73. name='username'
  74. rules={[
  75. {
  76. required: true,
  77. message: '请输入用户名'
  78. },
  79. {
  80. min: 3,
  81. max: 20,
  82. message: '长度在 3 到 20 个字符'
  83. }
  84. ]}>
  85. <Input />
  86. </Form.Item>
  87. </Col>
  88. <Col span={12}>
  89. <Form.Item<API.AddUserParams>
  90. label='密码'
  91. name='password'
  92. rules={[
  93. {
  94. required: true,
  95. message: '请输入密码'
  96. }
  97. ]}>
  98. <Input />
  99. </Form.Item>
  100. </Col>
  101. </Row>
  102. <Row gutter={16}>
  103. <Col span={12}>
  104. <Form.Item<API.AddUserParams>
  105. label='真实姓名'
  106. name='realName'
  107. rules={[
  108. {
  109. required: true,
  110. message: '请输入真实姓名'
  111. }
  112. ]}>
  113. <Input />
  114. </Form.Item>
  115. </Col>
  116. <Col span={12}>
  117. <Form.Item<API.AddUserParams>
  118. label='所属部门'
  119. name='deptId'
  120. rules={[
  121. {
  122. required: true,
  123. message: '请选择部门'
  124. }
  125. ]}>
  126. <TreeSelect
  127. fieldNames={{
  128. label: 'name',
  129. value: 'id',
  130. children: 'children'
  131. }}
  132. allowClear
  133. treeData={treeDeptData}
  134. />
  135. </Form.Item>
  136. </Col>
  137. </Row>
  138. <Row gutter={16}>
  139. <Col span={12}>
  140. <Form.Item<API.AddUserParams>
  141. label='手机号'
  142. name='phone'
  143. rules={[
  144. {
  145. required: true,
  146. message: '请输入手机号'
  147. }
  148. ]}>
  149. <Input />
  150. </Form.Item>
  151. </Col>
  152. <Col span={12}>
  153. <Form.Item<API.AddUserParams>
  154. label='邮箱'
  155. name='email'
  156. rules={[
  157. {
  158. required: true,
  159. message: '请输入邮箱'
  160. }
  161. ]}>
  162. <Input />
  163. </Form.Item>
  164. </Col>
  165. </Row>
  166. <Row gutter={16}>
  167. <Col span={12}>
  168. <Form.Item<API.AddUserParams>
  169. label='岗位'
  170. name='postIds'
  171. rules={[
  172. {
  173. required: true,
  174. message: '请选择岗位'
  175. }
  176. ]}>
  177. <Select
  178. mode='multiple'
  179. fieldNames={{
  180. label: 'postName',
  181. value: 'postId'
  182. }}
  183. allowClear
  184. options={postOptions}
  185. />
  186. </Form.Item>
  187. </Col>
  188. <Col span={12}>
  189. <Form.Item<API.AddUserParams>
  190. label='角色'
  191. name='roleIds'
  192. rules={[
  193. {
  194. required: true,
  195. message: '请选择角色'
  196. }
  197. ]}>
  198. <Select
  199. mode='multiple'
  200. fieldNames={{
  201. label: 'roleName',
  202. value: 'roleId'
  203. }}
  204. allowClear
  205. options={rolesOptions}
  206. />
  207. </Form.Item>
  208. </Col>
  209. </Row>
  210. <Row gutter={16}>
  211. <Col span={12}>
  212. <Form.Item<API.AddUserParams>
  213. label='状态'
  214. name='lockFlag'
  215. rules={[
  216. {
  217. required: true,
  218. message: '请选择状态'
  219. }
  220. ]}>
  221. <Radio.Group optionType='button' buttonStyle='solid'>
  222. <Radio value={LockFlag.OK}>有效</Radio>
  223. <Radio value={LockFlag.LOCKED}>锁定</Radio>
  224. </Radio.Group>
  225. </Form.Item>
  226. </Col>
  227. </Row>
  228. </Form>
  229. </Modal>
  230. )
  231. })