Resetpassword.vue 4.81 KB
<template>
  <div class="passform-container">
    <div class="passform-box">
      <div class="re-header">重置密码</div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="passform"
      >
        <div class="passformitem">
          <el-form-item label="旧密码" prop="oldpassword">
            <el-input v-model="ruleForm.oldpassword" type="password"></el-input>
          </el-form-item>
        </div>
        <div class="passformitem">
          <el-form-item label="新密码" prop="newpassword">
            <el-input v-model="ruleForm.newpassword" type="password"></el-input>
          </el-form-item>
        </div>
        <div class="passformitem">
          <el-form-item label="密码确认" prop="checknewpassword">
            <el-input
              v-model="ruleForm.checknewpassword"
              type="password"
            ></el-input>
          </el-form-item>
        </div>
        <el-form-item>
          <el-button @click="back('ruleForm')">取消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
let sha1 = require("js-sha1");
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (value == this.ruleForm.oldpassword) {
          callback(new Error("新旧密码不能重复!"));
          return;
        }
        var pwdRegex = new RegExp(
          "(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,30}"
        );
        if (!pwdRegex.test(value)) {
          callback(
            new Error(
              "您的密码复杂度太低(密码中必须包含大写和小写字母、数字、特殊字符长度大于8位),请及时修改密码!"
            )
          );
        }
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.newpassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        oldpassword: "",
        newpassword: "",
        checknewpassword: "",
      },
      rules: {
        oldpassword: [
          { required: true, message: "请输入旧密码", trigger: "blur" },
        ],
        newpassword: [
          { validator: validatePass, trigger: "blur" },
          { required: true, message: "", trigger: "blur" },
        ],
        checknewpassword: [
          { validator: validatePass2, trigger: "blur" },
          { required: true, message: "", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      let data = {
        old_pwd: window.config.encrypt
          ? sha1(this.ruleForm.oldpassword)
          : this.baseencode(this.ruleForm.oldpassword),
        new_pwd: window.config.encrypt
          ? sha1(this.ruleForm.newpassword)
          : this.baseencode(this.ruleForm.newpassword),
      };

      this.$refs[formName].validate((valid) => {
        if (valid) {
          let user_unid = localStorage.getItem("user_unid");
          this.$api.ops
            .resetPwd(data, user_unid)
            .then((res) => {
              if (res.ecode == 200) {
                this.$logs.oplogs(res, "serv_login", `修改密码`);
                this.$router.push("/login");
                this.$message({
                  type: "success",
                  message: res.enote,
                });
              } else {
                this.$logs.oplogs(res, "serv_login", `修改密码${res.enote}`);
                this.$message({
                  type: "error",
                  message: res.enote,
                });
              }
            })
            .catch((err) => {
              if (err.response) {
                this.$message({
                  type: "error",
                  message: err.response.data.enote,
                });
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    back() {
      this.$router.back(-1);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.passform-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
}
.passform {
  margin: 20px;
  width: 400px;
}
.passformitem {
  margin-bottom: 20px;
}
.passform-box {
  width: 400px;
  margin-top: 10%;
}
.re-header {
  margin-left: 60px;
  margin-top: 20px;
}
</style>