浏览代码

feat: 增加重置密码功能

lanjianrong 3 年之前
父节点
当前提交
caa8fdcac4
共有 2 个文件被更改,包括 72 次插入25 次删除
  1. 59 24
      src/pages/Institutions/Staff/components/StaffDetail.tsx
  2. 13 1
      src/services/api/user.ts

+ 59 - 24
src/pages/Institutions/Staff/components/StaffDetail.tsx

@@ -19,6 +19,8 @@ import type { InstitutionsModelState } from '../../model'
 
 import 'antd/lib/tree-select/style/index'
 import { ModalType } from '@/utils/enum'
+import { ModalForm, ProFormText } from '@ant-design/pro-form'
+import { changeAccountPsw } from '@/services/api/user'
 
 type StaffModalProps = ConnectProps & {
   visible: boolean
@@ -55,6 +57,13 @@ const StaffDrawer: React.FC<StaffModalProps> = ({
     }
   })
 
+  const { run: tryChangePsw } = useRequest(changeAccountPsw, {
+    manual: true,
+    onSuccess: () => {
+      message.success('修改成功')
+    }
+  })
+
   const { run: tryAddAccount } = useRequest(addAccount, {
     manual: true,
     onSuccess: () => {
@@ -242,9 +251,59 @@ const StaffDrawer: React.FC<StaffModalProps> = ({
         />
       )}
       <div className="ml-120px">
+        {type === ModalType.UPDATE ? (
+          <ModalForm
+            title="修改密码"
+            isKeyPressSubmit
+            trigger={<Button type="primary">重置密码</Button>}
+            onFinish={async values => {
+              try {
+                await tryChangePsw({ ...values, ID: defaultFormData?.dataID })
+                return true
+              } catch (error) {
+                return false
+              }
+            }}
+          >
+            <ProFormText.Password
+              label="新密码"
+              name="newPassword"
+              rules={[
+                { required: true, message: '请输入新密码' },
+                () => ({
+                  validator(_, value) {
+                    if (!value) {
+                      return Promise.resolve()
+                    }
+                    if (!/(?=.*[a-zA-Z])(?=.*\d).{8,16}/.test(value)) {
+                      return Promise.reject(new Error('密码长度最小8位且是数字和英文的组合'))
+                    }
+                    return Promise.resolve()
+                  }
+                })
+              ]}
+            />
+            <ProFormText.Password
+              label="确认新密码"
+              name="confirmPassword"
+              rules={[
+                { required: true, message: '请输入新密码' },
+                ({ getFieldValue }) => ({
+                  validator(_, value) {
+                    if (!value || getFieldValue('newPassword') === value) {
+                      return Promise.resolve()
+                    }
+                    return Promise.reject(new Error('两次输入的密码不一致!'))
+                  }
+                })
+              ]}
+            />
+          </ModalForm>
+        ) : null}
         {type !== ModalType.PREVIEW && (
           <Button
             type="primary"
+            className="ml-2"
             onClick={() => {
               form.submit()
             }}
@@ -253,30 +312,6 @@ const StaffDrawer: React.FC<StaffModalProps> = ({
           </Button>
         )}
       </div>
-      {/* <Form>
-        {schema && (
-          <FormRender
-            form={form}
-            schema={schema}
-            onFinish={onFinish}
-            onMount={onMount}
-            readOnly={type === ModalType.PREVIEW ? true : false}
-            widgets={{ site: SiteInput }}
-            watch={watch}
-          />
-        )}
-        <div className="ml-120px">
-          {type !== ModalType.PREVIEW && (
-            <Button
-              type="primary"
-              onClick={() => {
-                form.submit()
-              }}>
-              提交
-            </Button>
-          )}
-        </div>
-      </Form> */}
     </div>
   )
 }

+ 13 - 1
src/services/api/user.ts

@@ -113,10 +113,22 @@ export async function deleteUser(
   })
 }
 
-// 报错创建人
+// 保存 创建人
 export async function saveCreator(params: { ID: string; isCreated?: 1 | 0 }) {
   return request('/account/save/create', {
     method: 'POST',
     data: params
   })
 }
+
+// 更换账号密码
+export async function changeAccountPsw(params: {
+  ID: string
+  newPassword: string
+  confirmPassword: string
+}) {
+  return request('/account/change/password', {
+    method: 'POST',
+    data: params
+  })
+}