86133 | aaa3f5d | 2025-04-20 21:33:29 +0800 | [diff] [blame] | 1 | import React, { useEffect } from 'react'; |
| 2 | import { |
| 3 | ProForm, |
| 4 | ProFormText, |
| 5 | ProFormSelect, |
| 6 | ProFormRadio, |
| 7 | ProFormTextArea, |
| 8 | ProFormTreeSelect, |
| 9 | } from '@ant-design/pro-components'; |
| 10 | import { Form, Modal } from 'antd'; |
| 11 | import { useIntl, FormattedMessage } from '@umijs/max'; |
| 12 | import { DataNode } from 'antd/es/tree'; |
| 13 | import { DictValueEnumObj } from '@/components/DictTag'; |
| 14 | |
| 15 | /* * |
| 16 | * |
| 17 | * @author whiteshader@163.com |
| 18 | * @datetime 2023/02/06 |
| 19 | * |
| 20 | * */ |
| 21 | |
| 22 | |
| 23 | export type UserFormData = Record<string, unknown> & Partial<API.System.User>; |
| 24 | |
| 25 | export type UserFormProps = { |
| 26 | onCancel: (flag?: boolean, formVals?: UserFormData) => void; |
| 27 | onSubmit: (values: UserFormData) => Promise<void>; |
| 28 | open: boolean; |
| 29 | values: Partial<API.System.User>; |
| 30 | sexOptions: DictValueEnumObj; |
| 31 | statusOptions: DictValueEnumObj; |
| 32 | postIds: number[]; |
| 33 | posts: string[]; |
| 34 | roleIds: number[]; |
| 35 | roles: string[]; |
| 36 | depts: DataNode[]; |
| 37 | }; |
| 38 | |
| 39 | const UserForm: React.FC<UserFormProps> = (props) => { |
| 40 | const [form] = Form.useForm(); |
| 41 | const userId = Form.useWatch('userId', form); |
| 42 | const { sexOptions, statusOptions, } = props; |
| 43 | const { roles, posts, depts } = props; |
| 44 | |
| 45 | useEffect(() => { |
| 46 | form.resetFields(); |
| 47 | form.setFieldsValue({ |
| 48 | userId: props.values.userId, |
| 49 | deptId: props.values.deptId, |
| 50 | postIds: props.postIds, |
| 51 | roleIds: props.roleIds, |
| 52 | userName: props.values.userName, |
| 53 | nickName: props.values.nickName, |
| 54 | email: props.values.email, |
| 55 | phonenumber: props.values.phonenumber, |
| 56 | sex: props.values.sex, |
| 57 | avatar: props.values.avatar, |
| 58 | status: props.values.status, |
| 59 | delFlag: props.values.delFlag, |
| 60 | loginIp: props.values.loginIp, |
| 61 | loginDate: props.values.loginDate, |
| 62 | remark: props.values.remark, |
| 63 | }); |
| 64 | }, [form, props]); |
| 65 | |
| 66 | const intl = useIntl(); |
| 67 | const handleOk = () => { |
| 68 | form.submit(); |
| 69 | }; |
| 70 | const handleCancel = () => { |
| 71 | props.onCancel(); |
| 72 | }; |
| 73 | const handleFinish = async (values: Record<string, any>) => { |
| 74 | props.onSubmit(values as UserFormData); |
| 75 | }; |
| 76 | |
| 77 | return ( |
| 78 | <Modal |
| 79 | width={640} |
| 80 | title={intl.formatMessage({ |
| 81 | id: 'system.user.title', |
| 82 | defaultMessage: '编辑用户信息', |
| 83 | })} |
| 84 | open={props.open} |
| 85 | destroyOnClose |
| 86 | onOk={handleOk} |
| 87 | onCancel={handleCancel} |
| 88 | > |
| 89 | <ProForm |
| 90 | grid={true} |
| 91 | form={form} |
| 92 | layout="horizontal" |
| 93 | submitter={false} |
| 94 | onFinish={handleFinish}> |
| 95 | <ProFormText |
| 96 | name="nickName" |
| 97 | label={intl.formatMessage({ |
| 98 | id: 'system.user.nick_name', |
| 99 | defaultMessage: '用户昵称', |
| 100 | })} |
| 101 | placeholder="请输入用户昵称" |
| 102 | colProps={{ xs: 24, md: 12, xl: 12 }} |
| 103 | rules={[ |
| 104 | { |
| 105 | required: true, |
| 106 | message: ( |
| 107 | <FormattedMessage id="请输入用户昵称!" defaultMessage="请输入用户昵称!" /> |
| 108 | ), |
| 109 | }, |
| 110 | ]} |
| 111 | /> |
| 112 | <ProFormTreeSelect |
| 113 | name="deptId" |
| 114 | label={intl.formatMessage({ |
| 115 | id: 'system.user.dept_name', |
| 116 | defaultMessage: '部门', |
| 117 | })} |
| 118 | request={async () => { |
| 119 | return depts; |
| 120 | }} |
| 121 | placeholder="请输入用户部门" |
| 122 | colProps={{ md: 12, xl: 12 }} |
| 123 | rules={[ |
| 124 | { |
| 125 | required: true, |
| 126 | message: ( |
| 127 | <FormattedMessage id="请输入用户部门!" defaultMessage="请输入用户部门!" /> |
| 128 | ), |
| 129 | }, |
| 130 | ]} |
| 131 | /> |
| 132 | <ProFormText |
| 133 | name="phonenumber" |
| 134 | label={intl.formatMessage({ |
| 135 | id: 'system.user.phonenumber', |
| 136 | defaultMessage: '手机号码', |
| 137 | })} |
| 138 | placeholder="请输入手机号码" |
| 139 | colProps={{ md: 12, xl: 12 }} |
| 140 | rules={[ |
| 141 | { |
| 142 | required: false, |
| 143 | message: ( |
| 144 | <FormattedMessage id="请输入手机号码!" defaultMessage="请输入手机号码!" /> |
| 145 | ), |
| 146 | }, |
| 147 | ]} |
| 148 | /> |
| 149 | <ProFormText |
| 150 | name="email" |
| 151 | label={intl.formatMessage({ |
| 152 | id: 'system.user.email', |
| 153 | defaultMessage: '用户邮箱', |
| 154 | })} |
| 155 | placeholder="请输入用户邮箱" |
| 156 | colProps={{ md: 12, xl: 12 }} |
| 157 | rules={[ |
| 158 | { |
| 159 | required: false, |
| 160 | message: ( |
| 161 | <FormattedMessage id="请输入用户邮箱!" defaultMessage="请输入用户邮箱!" /> |
| 162 | ), |
| 163 | }, |
| 164 | ]} |
| 165 | /> |
| 166 | <ProFormText |
| 167 | name="userName" |
| 168 | label={intl.formatMessage({ |
| 169 | id: 'system.user.user_name', |
| 170 | defaultMessage: '用户账号', |
| 171 | })} |
| 172 | hidden={userId} |
| 173 | placeholder="请输入用户账号" |
| 174 | colProps={{ md: 12, xl: 12 }} |
| 175 | rules={[ |
| 176 | { |
| 177 | required: true, |
| 178 | }, |
| 179 | ]} |
| 180 | /> |
| 181 | <ProFormText.Password |
| 182 | name="password" |
| 183 | label={intl.formatMessage({ |
| 184 | id: 'system.user.password', |
| 185 | defaultMessage: '密码', |
| 186 | })} |
| 187 | hidden={userId} |
| 188 | placeholder="请输入密码" |
| 189 | colProps={{ md: 12, xl: 12 }} |
| 190 | rules={[ |
| 191 | { |
| 192 | required: false, |
| 193 | message: <FormattedMessage id="请输入密码!" defaultMessage="请输入密码!" />, |
| 194 | }, |
| 195 | ]} |
| 196 | /> |
| 197 | <ProFormSelect |
| 198 | valueEnum={sexOptions} |
| 199 | name="sex" |
| 200 | label={intl.formatMessage({ |
| 201 | id: 'system.user.sex', |
| 202 | defaultMessage: '用户性别', |
| 203 | })} |
| 204 | initialValue={'0'} |
| 205 | placeholder="请输入用户性别" |
| 206 | colProps={{ md: 12, xl: 12 }} |
| 207 | rules={[ |
| 208 | { |
| 209 | required: false, |
| 210 | message: ( |
| 211 | <FormattedMessage id="请输入用户性别!" defaultMessage="请输入用户性别!" /> |
| 212 | ), |
| 213 | }, |
| 214 | ]} |
| 215 | /> |
| 216 | <ProFormRadio.Group |
| 217 | valueEnum={statusOptions} |
| 218 | name="status" |
| 219 | label={intl.formatMessage({ |
| 220 | id: 'system.user.status', |
| 221 | defaultMessage: '帐号状态', |
| 222 | })} |
| 223 | initialValue={'0'} |
| 224 | placeholder="请输入帐号状态" |
| 225 | colProps={{ md: 12, xl: 12 }} |
| 226 | rules={[ |
| 227 | { |
| 228 | required: false, |
| 229 | message: ( |
| 230 | <FormattedMessage id="请输入帐号状态!" defaultMessage="请输入帐号状态!" /> |
| 231 | ), |
| 232 | }, |
| 233 | ]} |
| 234 | /> |
| 235 | <ProFormSelect |
| 236 | name="postIds" |
| 237 | mode="multiple" |
| 238 | label={intl.formatMessage({ |
| 239 | id: 'system.user.post', |
| 240 | defaultMessage: '岗位', |
| 241 | })} |
| 242 | options={posts} |
| 243 | placeholder="请选择岗位" |
| 244 | colProps={{ md: 12, xl: 12 }} |
| 245 | rules={[{ required: true, message: '请选择岗位!' }]} |
| 246 | /> |
| 247 | <ProFormSelect |
| 248 | name="roleIds" |
| 249 | mode="multiple" |
| 250 | label={intl.formatMessage({ |
| 251 | id: 'system.user.role', |
| 252 | defaultMessage: '角色', |
| 253 | })} |
| 254 | options={roles} |
| 255 | placeholder="请选择角色" |
| 256 | colProps={{ md: 12, xl: 12 }} |
| 257 | rules={[{ required: true, message: '请选择角色!' }]} |
| 258 | /> |
| 259 | <ProFormTextArea |
| 260 | name="remark" |
| 261 | label={intl.formatMessage({ |
| 262 | id: 'system.user.remark', |
| 263 | defaultMessage: '备注', |
| 264 | })} |
| 265 | placeholder="请输入备注" |
| 266 | colProps={{ md: 24, xl: 24 }} |
| 267 | rules={[ |
| 268 | { |
| 269 | required: false, |
| 270 | message: <FormattedMessage id="请输入备注!" defaultMessage="请输入备注!" />, |
| 271 | }, |
| 272 | ]} |
| 273 | /> |
| 274 | </ProForm> |
| 275 | </Modal> |
| 276 | ); |
| 277 | }; |
| 278 | |
| 279 | export default UserForm; |