import React, { useCallback, useState } from 'react'; import { AdminPageProps } from '../'; import { Form, Button, Card, message, FormItemProps } from 'antd'; import { commonRules, useServiceStore } from '../../../utils'; import { CheckboxChangeEvent, CheckboxProps } from 'antd/lib/checkbox'; import { Entity, StringUtil, UserEntity } from 'matrix-ui-service'; import { CheckboxField, InputField } from '../../../ant-design-field'; const { required, email, cellPhone, password } = commonRules; export function UserProfile(props: AdminPageProps) { const { services } = props; const [showPassword, setShowPassword] = useState(); const handleCheckboxChange = useCallback((e: CheckboxChangeEvent) => setShowPassword(e.target.checked), []); const loginStore = useServiceStore(services.loginService); const handleSave = useCallback((item: UserEntity) => { //if (item.password === services.loginService.initPasswordHash) message.error('新密码不能和初始密码相同'); services.userService.save(item).then((user) => { services.loginService.updateLoginInfo({ user }); message.success('保存成功'); }); }, []); const { loginInfo: { user }, forcePasswordChange, } = loginStore; const showPasswordFinally = forcePasswordChange || showPassword; return (
); } interface ProfileFormProps { inputItem: UserEntity; showPassword: boolean; onCheckboxChange: CheckboxProps['onChange']; onSave: (item: Entity) => void; } export function ProfileFrom(props: ProfileFormProps) { const { onSave, inputItem } = props; const [firstPassword, setFirstPassword] = useState(); //个人设置接口单独开发,防止恶意修改“帐号”等重要信息 const handleSubmit = useCallback( (saveItem) => { if (saveItem.showPassword) { if (saveItem.password === saveItem.passwordAgain) onSave({ id: inputItem.id, ...saveItem, password: StringUtil.sha256(saveItem.password) }); else message.error('两次输入的密码不一致'); } else onSave({ id: inputItem.id, ...saveItem }); }, [onSave, inputItem], ); const itemCol: Pick = { labelCol: { span: 8 }, wrapperCol: { span: 16 }, }; return (
{/*嵌套的form item,样式需特殊处理*/} {(form) => form.getFieldValue('showPassword') && ( setFirstPassword(e.target.value)} /> ) } ); }