ソースを参照

强制改密码

zhangjie 1 年間 前
コミット
c2140ee6d3
3 ファイル変更204 行追加46 行削除
  1. 57 34
      src/features/Login/Login.vue
  2. 142 0
      src/plugins/formRules.js
  3. 5 12
      src/views/Layout/components/ResetPwd.vue

+ 57 - 34
src/features/Login/Login.vue

@@ -92,6 +92,9 @@
         >鄂ICP备12000033号-10</a
       >
     </div>
+
+    <!-- ResetPwd -->
+    <reset-pwd ref="ResetPwd" @confirm="pwdModified"></reset-pwd>
   </div>
 </template>
 
@@ -99,9 +102,10 @@
 import { LOGIN_BY_USERNAME } from "../../store/action-types";
 import { ORG_CODE } from "../../constant/constants";
 import { getLogo } from "@/api/login";
+import ResetPwd from "@/views/Layout/components/ResetPwd.vue";
 
 export default {
-  // components: { BasicContainer },
+  components: { ResetPwd },
   name: "Login",
   data() {
     return {
@@ -121,42 +125,61 @@ export default {
   },
   methods: {
     async submitBtn() {
-      this.$refs["form"].validate(async (valid) => {
-        if (valid) {
-          try {
-            this.loading = true;
-            await this.$store.dispatch(LOGIN_BY_USERNAME, {
-              loginName: this.user.username,
-              password: this.user.password,
-              code: ORG_CODE,
-            });
-            // console.log(res);
-            // localStorage.setItem("themis-user", res.data);
-
-            const user = this.$store.state.user;
-            const needLiveUrl =
-              user.roleCodes.includes("SUPER_ADMIN") ||
-              user.roleCodes.includes("ADMIN") ||
-              user.roleCodes.includes("INSPECTION") ||
-              user.roleCodes.includes("INVIGILATE");
-            if (needLiveUrl)
-              await this.$store.dispatch("invigilation/fetchLiveDomains");
+      const valid = await this.$refs["form"].validate();
+      if (!valid) return;
 
-            this.$message({
-              message: "登录成功",
-              type: "success",
-              duration: 1.5,
-              showClose: true,
-            });
-            this.$router.push("/home");
-          } catch (error) {
-            // console.log(error?.response?.data?.message);
-            this.user.errorInfo = error?.response?.data?.message || "";
-          } finally {
-            this.loading = false;
-          }
+      try {
+        this.loading = true;
+        await this.$store.dispatch(LOGIN_BY_USERNAME, {
+          loginName: this.user.username,
+          password: this.user.password,
+          code: ORG_CODE,
+        });
+        if (!this.$store.state.user.pwdForce) {
+          this.$refs.ResetPwd.open();
+          return;
         }
+
+        await this.loginSuccess();
+      } catch (error) {
+        // console.log(error?.response?.data?.message);
+        this.user.errorInfo = error?.response?.data?.message || "";
+      } finally {
+        this.loading = false;
+      }
+    },
+    async pwdModified(userData) {
+      try {
+        this.loading = true;
+        await this.$store.dispatch(LOGIN_BY_USERNAME, {
+          loginName: this.user.username,
+          password: userData.password,
+          code: ORG_CODE,
+        });
+        await this.loginSuccess();
+      } catch (error) {
+        this.user.errorInfo = error?.response?.data?.message || "";
+      } finally {
+        this.loading = false;
+      }
+    },
+    async loginSuccess() {
+      const user = this.$store.state.user;
+      const needLiveUrl =
+        user.roleCodes.includes("SUPER_ADMIN") ||
+        user.roleCodes.includes("ADMIN") ||
+        user.roleCodes.includes("INSPECTION") ||
+        user.roleCodes.includes("INVIGILATE");
+      if (needLiveUrl)
+        await this.$store.dispatch("invigilation/fetchLiveDomains");
+
+      this.$message({
+        message: "登录成功",
+        type: "success",
+        duration: 1.5,
+        showClose: true,
       });
+      this.$router.push("/home");
     },
   },
 };

+ 142 - 0
src/plugins/formRules.js

@@ -0,0 +1,142 @@
+// async-validator rules
+// to view at https://github.com/yiminghe/async-validator
+
+const username = [
+  {
+    required: true,
+    pattern: /^[a-zA-Z0-9][a-zA-Z0-9_]{2,19}$/,
+    message: "用户名必须以字母或数字开头,长度为3-20位,允许字母数字下划线",
+    trigger: "change",
+  },
+];
+
+const commonCode = ({ prop, min = 3, max = 20 }) => {
+  return [
+    {
+      required: true,
+      pattern: new RegExp(`^[a-zA-Z0-9_]{${min},${max}}$`),
+      message: `${prop}只能由数字、字母和下划线组成,长度${min}-${max}个字符`,
+      trigger: "change",
+    },
+  ];
+};
+
+const email = [
+  {
+    required: true,
+    type: "email",
+    message: "邮箱格式不正确",
+    trigger: "change",
+  },
+];
+
+const password = [
+  {
+    required: true,
+    pattern: /^[a-zA-Z0-9_]{6,20}$/,
+    message: "密码只能由数字、字母和下划线组成,长度6-20个字符",
+    trigger: "change",
+  },
+];
+const strictPassword = [
+  {
+    required: true,
+    validator: (rule, value, callback) => {
+      if (!value) {
+        return callback(new Error(`请输入密码`));
+      }
+      // // 禁止使用相同的数字或字符作为密码
+      // const reg2 = /^[a-zA-Z0-9].+$/;
+      // const vals = new Set(value.split(""));
+      // if (reg2.test(value) && vals.size === 1) {
+      //   return callback(new Error(`禁止使用相同的数字或字符作为密码`));
+      // }
+      // // 禁止使用连续升序或降序的数字或字母作为密码
+      // const valCharCodes = value.split("").map((item) => item.charCodeAt());
+      // let intervals = [];
+      // for (let i = 0; i < valCharCodes.length; i++) {
+      //   const element = valCharCodes[i];
+      //   if (i === 0) continue;
+      //   intervals.push(element - valCharCodes[i - 1]);
+      // }
+      // const interVals = Array.from(new Set(intervals));
+      // if (
+      //   reg2.test(value) &&
+      //   interVals.length === 1 &&
+      //   Math.abs(interVals[0]) === 1
+      // ) {
+      //   return callback(
+      //     new Error(`禁止使用连续升序或降序的数字或字母作为密码`)
+      //   );
+      // }
+      // 密码应至少包含数字、大小写字母及特殊宇符中的两种;
+      // const reg1 =
+      //   /^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)(?![!@#$%^&*]+$)[\da-zA-Z!@#$%^&*]{6,10}$/g;
+      const reg1 =
+        /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*]+$)(?![0-9\W_!@#$%^&*]+$)[a-zA-Z0-9\W_!@#$%^&*]{6,10}$/g;
+      if (!reg1.test(value)) {
+        return callback(
+          new Error(
+            `必须包含数字、大写字母、小写字母及特殊宇符(!@#$%^&*)中的三种,长度6-10位`
+          )
+        );
+      }
+
+      return callback();
+    },
+    trigger: "change",
+  },
+];
+
+const phone = [
+  {
+    required: true,
+    pattern: /^1\d{10}$/,
+    message: "请输入合适的手机号码",
+    trigger: "change",
+  },
+];
+
+const smscode = [
+  {
+    required: true,
+    pattern: /^[a-zA-Z0-9]{4}$/,
+    message: "请输入4位短信验证码",
+    trigger: "change",
+  },
+];
+
+const numberValidator = ({ prop, min = 1, max = 100 }) => {
+  return [
+    {
+      required: true,
+      validator: (rule, value, callback) => {
+        if (!value && value !== 0) {
+          return callback(new Error(`请输入${prop}`));
+        }
+
+        if (!Number.isInteger(value)) {
+          callback(new Error("请输入数字值"));
+        } else {
+          if (value < min || value > max) {
+            callback(new Error(`${prop}的大小只能介于${min}-${max}之间。`));
+          } else {
+            callback();
+          }
+        }
+      },
+      trigger: "change",
+    },
+  ];
+};
+
+export {
+  username,
+  commonCode,
+  email,
+  password,
+  strictPassword,
+  phone,
+  smscode,
+  numberValidator,
+};

+ 5 - 12
src/views/Layout/components/ResetPwd.vue

@@ -4,7 +4,7 @@
     :visible.sync="modalIsShow"
     title="修改密码"
     top="10vh"
-    width="448px"
+    width="500px"
     :close-on-click-modal="false"
     :close-on-press-escape="false"
     append-to-body
@@ -44,6 +44,7 @@
 
 <script>
 import { resetUserPassword } from "@/api/system-user";
+import { strictPassword } from "@/plugins/formRules";
 
 const initModalForm = {
   id: "",
@@ -61,14 +62,6 @@ export default {
         callback();
       }
     };
-    const password = [
-      {
-        required: true,
-        pattern: /^[a-zA-Z0-9_]{6,20}$/,
-        message: "密码只能由数字、字母和下划线组成,长度6-20个字符",
-        trigger: "change",
-      },
-    ];
 
     return {
       modalIsShow: false,
@@ -77,9 +70,9 @@ export default {
       isFetchingCode: false,
       modalForm: { ...initModalForm },
       resetRules: {
-        password: [...password],
+        password: [...strictPassword],
         rePassword: [
-          ...password,
+          ...strictPassword,
           {
             validator: equalToPswd,
             trigger: "change",
@@ -118,7 +111,7 @@ export default {
       this.isSubmit = false;
       this.$message.success("修改成功!");
       this.cancel();
-      this.$emit("confirm");
+      this.$emit("confirm", datas);
     },
   },
 };