Browse Source

feat(user): reset password

lind 3 years ago
parent
commit
5563fb5a0d

+ 134 - 0
src/pages/system/User/ResetPassword/index.tsx

@@ -0,0 +1,134 @@
+import { message, Modal } from 'antd';
+import { createSchemaField } from '@formily/react';
+import { Form, FormItem, Password } from '@formily/antd';
+import { ISchema } from '@formily/json-schema';
+import { useIntl } from 'umi';
+import { useMemo } from 'react';
+import { createForm } from '@formily/core';
+import { service } from '@/pages/system/User';
+
+interface Props {
+  visible: boolean;
+  close: Function;
+  data: Partial<UserItem>;
+}
+
+const ResetPassword = (props: Props) => {
+  const intl = useIntl();
+  const SchemaField = createSchemaField({
+    components: {
+      FormItem,
+      Password,
+    },
+  });
+
+  const schema: ISchema = {
+    type: 'object',
+    properties: {
+      password: {
+        type: 'string',
+        title: intl.formatMessage({
+          id: 'pages.system.password',
+          defaultMessage: '密码',
+        }),
+        'x-decorator': 'FormItem',
+        'x-component': 'Password',
+        'x-component-props': {
+          checkStrength: true,
+          placeholder: '请输入密码',
+        },
+        required: true,
+        'x-reactions': [
+          {
+            dependencies: ['.confirmPassword'],
+            fulfill: {
+              state: {
+                selfErrors:
+                  '{{$deps[0] && $self.value && $self.value !==$deps[0] ? "两次密码输入不一致" : ""}}',
+              },
+            },
+          },
+        ],
+        name: 'password',
+        'x-validator': [
+          {
+            max: 128,
+            message: '密码最多可输入128位',
+          },
+          {
+            message: '密码不能少于6位',
+          },
+          {
+            required: true,
+            message: '请输入密码',
+          },
+        ],
+      },
+      confirmPassword: {
+        type: 'string',
+        title: intl.formatMessage({
+          id: 'pages.system.confirmPassword',
+          defaultMessage: '确认密码?',
+        }),
+        'x-decorator': 'FormItem',
+        'x-component': 'Password',
+        'x-component-props': {
+          checkStrength: true,
+          placeholder: '请再次输入密码',
+        },
+        'x-validator': [
+          {
+            max: 128,
+            message: '密码最多可输入128位',
+          },
+          {
+            min: 6,
+            message: '密码不能少于6位',
+          },
+          {
+            required: true,
+            message: '请输入确认密码',
+          },
+        ],
+        'x-reactions': [
+          {
+            dependencies: ['.password'],
+            fulfill: {
+              state: {
+                selfErrors:
+                  '{{$deps[0] && $self.value && $self.value !== $deps[0] ? "两次密码输入不一致" : ""}}',
+              },
+            },
+          },
+        ],
+        'x-decorator-props': {},
+        name: 'confirmPassword',
+      },
+    },
+  };
+
+  const form = useMemo(() => createForm({}), []);
+  return (
+    <Modal
+      title="重置密码"
+      visible={props.visible}
+      onCancel={() => props.close()}
+      onOk={async () => {
+        const value: { password: string; confirmPassword: string } = await form.submit();
+        if (props.data.id) {
+          const resp = await service.resetPassword(props.data.id, value.confirmPassword);
+          if (resp.status === 200) {
+            message.success('操作成功');
+          }
+        } else {
+          props.close();
+        }
+      }}
+    >
+      <Form form={form} layout="vertical">
+        <SchemaField schema={schema} />
+      </Form>
+    </Modal>
+  );
+};
+export default ResetPassword;

+ 22 - 6
src/pages/system/User/Save/index.tsx

@@ -153,8 +153,8 @@ const Save = (props: Props) => {
             },
             'x-validator': [
               {
-                max: 50,
-                message: '最多可输入50个字符',
+                max: 64,
+                message: '最多可输入64个字符',
               },
               {
                 required: true,
@@ -200,6 +200,7 @@ const Save = (props: Props) => {
           checkStrength: true,
           placeholder: '请输入密码',
         },
+        'x-visible': model === 'add',
         'x-reactions': [
           {
             dependencies: ['.confirmPassword'],
@@ -218,7 +219,7 @@ const Save = (props: Props) => {
             message: '密码最多可输入128位',
           },
           {
-            min: model === 'edit' ? 0 : 6,
+            min: 8,
             message: '密码不能少于6位',
           },
           {
@@ -239,15 +240,14 @@ const Save = (props: Props) => {
           checkStrength: true,
           placeholder: '请再次输入密码',
         },
-        maxLength: 128,
-        minLength: 6,
+        'x-visible': model === 'add',
         'x-validator': [
           {
             max: 128,
             message: '密码最多可输入128位',
           },
           {
-            min: model === 'edit' ? 0 : 6,
+            min: 8,
             message: '密码不能少于6位',
           },
           {
@@ -364,6 +364,22 @@ const Save = (props: Props) => {
             },
             'x-reactions': ['{{useAsyncDataSource(getOrg)}}'],
           },
+          telephone: {
+            title: '手机号',
+            'x-decorator': 'FormItem',
+            'x-component': 'Input',
+            'x-decorator-props': {
+              gridSpan: 1,
+            },
+          },
+          email: {
+            title: '邮箱',
+            'x-decorator': 'FormItem',
+            'x-component': 'Input',
+            'x-decorator-props': {
+              gridSpan: 1,
+            },
+          },
         },
       },
     },

+ 27 - 0
src/pages/system/User/index.tsx

@@ -10,6 +10,7 @@ import {
   EditOutlined,
   PlayCircleOutlined,
   PlusOutlined,
+  SafetyOutlined,
 } from '@ant-design/icons';
 import { useIntl } from '@@/plugin-locale/localeExports';
 import { useRef, useState } from 'react';
@@ -17,6 +18,7 @@ import Save from './Save';
 import { observer } from '@formily/react';
 import { PermissionButton } from '@/components';
 import usePermissions from '@/hooks/permission';
+import ResetPassword from '@/pages/system/User/ResetPassword';
 
 export const service = new Service('user');
 
@@ -32,6 +34,7 @@ const User = observer(() => {
     setCurrent(record);
   };
 
+  const [reset, setReset] = useState<boolean>(false);
   const columns: ProColumns<UserItem>[] = [
     {
       title: intl.formatMessage({
@@ -108,6 +111,14 @@ const User = observer(() => {
       ),
     },
     {
+      dataIndex: 'telephone',
+      title: '手机号',
+    },
+    {
+      dataIndex: 'email',
+      title: '邮箱',
+    },
+    {
       title: intl.formatMessage({
         id: 'pages.data.option',
         defaultMessage: '操作',
@@ -165,6 +176,21 @@ const User = observer(() => {
         </PermissionButton>,
         <PermissionButton
           type="link"
+          key="password"
+          style={{ padding: 0 }}
+          tooltip={{
+            title: '重置密码',
+          }}
+          onClick={() => {
+            setReset(true);
+            setCurrent(record);
+          }}
+          isPermission={userPermission.update}
+        >
+          <SafetyOutlined />
+        </PermissionButton>,
+        <PermissionButton
+          type="link"
           key="delete"
           style={{ padding: 0 }}
           isPermission={userPermission.delete}
@@ -231,6 +257,7 @@ const User = observer(() => {
         }}
         data={current}
       />
+      <ResetPassword data={current} visible={reset} close={() => setReset(false)} />
     </PageContainer>
   );
 });

+ 6 - 0
src/pages/system/User/serivce.ts

@@ -42,6 +42,12 @@ class Service extends BaseService<UserItem> {
       method: 'POST',
       data: name,
     });
+
+  resetPassword = (id: string, password: string) =>
+    request(`/${SystemConst.API_BASE}/user/${id}/password/_reset`, {
+      method: 'POST',
+      data: password,
+    });
 }
 
 export default Service;