123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <template>
- <el-dialog
- class="reset-pwd"
- :visible.sync="modalIsShow"
- :title="title"
- top="10vh"
- width="520px"
- :close-on-click-modal="false"
- :close-on-press-escape="false"
- append-to-body
- :show-close="false"
- @open="visibleChange"
- >
- <el-form
- ref="modalFormComp"
- :model="modalForm"
- :rules="resetRules"
- label-position="top"
- >
- <template v-if="needResetPwd">
- <el-form-item prop="oldPassword" label="旧密码:">
- <el-input
- type="password"
- v-model="modalForm.oldPassword"
- placeholder="请输入旧密码"
- clearable
- ></el-input>
- </el-form-item>
- <el-form-item prop="password" label="新密码:">
- <el-input
- type="password"
- v-model="modalForm.password"
- placeholder="请输入新密码"
- clearable
- ></el-input>
- </el-form-item>
- <el-form-item prop="rePassword" label="再次密码:">
- <el-input
- type="password"
- v-model="modalForm.rePassword"
- placeholder="请再次输入新密码"
- clearable
- ></el-input>
- </el-form-item>
- </template>
- <template v-if="needBindMobile && schoolInfo.phoneLogin">
- <el-form-item prop="mobileNumber" label="手机号:">
- <el-input
- v-model.trim="modalForm.mobileNumber"
- placeholder="请输入账号"
- name="mobileNumber"
- clearable
- >
- </el-input>
- </el-form-item>
- <el-form-item prop="code" label="验证码:">
- <div class="vlcode">
- <div class="vlcode-right">
- <el-button
- style="width: 100%"
- type="text"
- @click="fetchSmsCode"
- :disabled="isFetchingCode"
- >+{{ codeContent }}</el-button
- >
- </div>
- <div class="vlcode-left">
- <el-input
- v-model.trim="modalForm.code"
- placeholder="请输入手机验证码"
- name="code"
- clearable
- >
- </el-input>
- </div>
- </div>
- </el-form-item>
- </template>
- </el-form>
- <div slot="footer">
- <el-button type="primary" :disabled="isSubmit" @click="submit"
- >确认</el-button
- >
- <el-button @click="cancel">取消</el-button>
- </div>
- </el-dialog>
- </template>
- <script>
- import { updatePwd } from "../api";
- import { getSmsCodeForBind } from "@/modules/login/api";
- import { password, strictPassword, phone, smscode } from "@/plugins/formRules";
- import { Base64 } from "@/plugins/crypto";
- import fetchSmsMixins from "@/modules/login/fetchSmsMixins";
- const initModalForm = {
- id: "",
- oldPassword: "",
- password: "",
- rePassword: "",
- mobileNumber: "",
- code: "",
- };
- export default {
- name: "reset-pwd",
- props: {
- userInfo: {
- type: Object,
- default() {
- return {
- userId: "",
- loginName: "",
- schoolCode: "",
- password: "",
- mobileNumber: "",
- pwdCount: "",
- };
- },
- },
- },
- mixins: [fetchSmsMixins],
- data() {
- const passwordRule = [
- ...strictPassword,
- {
- validator: (rule, value, callback) => {
- if (value === this.userInfo.loginName) {
- return callback(new Error("禁止使用用户账户号作为密码"));
- }
- return callback();
- },
- },
- ];
- const equalToOldPswd = (rule, value, callback) => {
- if (value === this.modalForm.oldPassword) {
- callback(new Error("新旧密码不可以相同"));
- } else {
- callback();
- }
- };
- const equalToPswd = (rule, value, callback) => {
- if (value !== this.modalForm.password) {
- callback(new Error("两次输入的密码不一致"));
- } else {
- callback();
- }
- };
- return {
- modalIsShow: false,
- isSubmit: false,
- nameWaitTime: "resetpwd",
- isFetchingCode: false,
- modalForm: { ...initModalForm },
- resetRules: {
- code: smscode,
- mobileNumber: phone,
- oldPassword: password,
- password: [
- ...passwordRule,
- {
- validator: equalToOldPswd,
- trigger: "change",
- },
- ],
- rePassword: [
- ...passwordRule,
- {
- validator: equalToPswd,
- trigger: "change",
- },
- ],
- },
- schoolInfo: {},
- };
- },
- computed: {
- needBindMobile() {
- return !this.userInfo.mobileNumber;
- },
- needResetPwd() {
- return !this.userInfo.pwdCount;
- },
- title() {
- if (this.needBindMobile && this.needResetPwd)
- return "修改密码与绑定手机号";
- if (this.needBindMobile) return "绑定手机号";
- if (this.needResetPwd) return "修改密码";
- return "修改密码";
- },
- },
- methods: {
- initData(val) {
- this.modalForm = { ...initModalForm };
- this.modalForm.oldPassword = this.userInfo.password || "";
- this.modalForm.id = this.userInfo.userId;
- },
- visibleChange() {
- this.initData(this.instance);
- this.schoolInfo = this.$ls.get("schoolInfo", {});
- },
- cancel() {
- this.modalIsShow = false;
- this.$emit("cancel");
- },
- open() {
- this.modalIsShow = true;
- },
- checkField(field) {
- return new Promise((resolve, reject) => {
- this.$refs.modalFormComp.validateField(field, (unvalid) => {
- if (unvalid) {
- reject();
- } else {
- resolve();
- }
- });
- });
- },
- async fetchSmsCode() {
- // console.log("111");
- let result = true;
- await this.checkField("mobileNumber").catch(() => {
- result = false;
- });
- if (!result) return;
- this.isFetchingCode = true;
- const data = await getSmsCodeForBind({
- loginName: this.userInfo.loginName,
- schoolCode: this.userInfo.schoolCode,
- password: Base64(this.modalForm.oldPassword),
- mobileNumber: this.modalForm.mobileNumber,
- }).catch(() => {
- this.isFetchingCode = false;
- });
- if (!data) return;
- this.$message.success(
- `已向手机尾号【${this.modalForm.mobileNumber.slice(
- -4
- )}】成功发送短信,请在2分钟内进行验证!`
- );
- this.changeContent();
- },
- async submit() {
- const valid = await this.$refs.modalFormComp.validate().catch(() => {});
- if (!valid) return;
- if (this.isSubmit) return;
- this.isSubmit = true;
- let datas = {
- id: this.modalForm.id,
- oldPassword: Base64(this.modalForm.oldPassword),
- };
- if (this.needBindMobile) {
- datas = {
- ...datas,
- mobileNumber: this.modalForm.mobileNumber,
- verifyCode: this.modalForm.code,
- };
- }
- if (this.needResetPwd) {
- datas = {
- ...datas,
- password: Base64(this.modalForm.password),
- };
- }
- const data = await updatePwd(datas).catch(() => {
- this.isSubmit = false;
- });
- if (!data) return;
- this.isSubmit = false;
- this.$message.success("修改成功!");
- this.$emit("modified", data);
- this.modalIsShow = false;
- },
- },
- };
- </script>
|