password-reset.js 5.28 KB

import { mapGetters, mapActions, mapState } from "vuex";

import api from '@/api/api'
import {
	httpGet,
	httpPost
} from '@/api/fetch-api.js'

import Auth from '@components/auth/auth.vue';
import modalComp from '@/components/modal-comp/modal-comp.vue';
import modalSimpleComp from '@/components/modal-simple-comp/modal-simple-comp.vue';
import { passwordEncrypt } from '@/utils/encrypt.js';
import Vue from 'vue';
import { Loading } from 'vant';
Vue.use(Loading);

export default {
	data() {
		return {
			loading: false,
			showForm: false,
			key: 'value',
			type: 1, // 1:输入用户信息 2:找回密码 3:核对信息 4.重置密码
			values: {
				cid: "",
				oldPwd: "",
				pwd: "",
				pwdRepeat: ""
			},
			step: {
				error0: "",
				error1: "",
				error2: "",
			},
			times: {
				interval: 0, // 索引
				remain: 0, // 剩余时间
				tip: "" // 显示的文字
			},
			modalSimpleVisiable: false,
			modalVisiable: false,
			targetPath: "",
			modalIcon: "succ",
			modalContent: ""
		}
	},
	methods: {
		initData() { },
		i18n() {
			let i18n = this.$i18n.messages[this.$i18n.locale] || {};
			return i18n;
		},
		showModal(content, icon) {
			icon = !icon || typeof icon === "undefined" ? "succ" : icon;
			this.modalIcon = icon;
			this.modalContent = content;
			if (icon == "succ") {
				this.modalVisiable = true;
			} else {
				this.modalSimpleVisiable = true;
			}
		},
		closeModal() {
			this.modalVisiable = false;
			this.modalSimpleVisiable = false;
		},
		resetPwd() {
			if (this.submitBtnDisabled) {
				return;
			}
			if (!this.values.oldPwd) {
				this.step.error0 = this.i18n().passwordReset.oldPwdPlaceholde;
				return;
			}
			let c1 = this.checkPassword(this.values.pwd);
			if (c1) {
				this.$set(this.step, 'error1', c1);
				return;
			}
			if (this.checkSamePassword()) {
				this.$set(this.step, 'error1', this.i18n().passwordCheck.error6);
				return;
			}
			if (this.values.pwdRepeat != this.values.pwd) {
				this.step.error2 = this.i18n().passwordCheck.error3;
				return;
			}
			let newPwd = passwordEncrypt(this.values.pwd);
			let oldPwd = passwordEncrypt(this.values.oldPwd);

			if (this.loading) {
				return;
			}
			this.loading = true;

			httpPost({
				url: api.changePassword,
				data: {
					oldPwd: oldPwd,
					newPwd: newPwd
				},
				sid: true
			}).then(response => {
				this.loading = false;
				this.targetPath = "";
				let message = this.i18n().passwordCheck.success;
				switch (response.returnCode) {
					case "0":
						this.targetPath = "";
						this.showModal(message, "succ");
						break
					case "COMMON_ERROR_E0":
						message = this.i18n().session.sidExpire;
						this.targetPath = "/login?callback=/password/reset";
						this.showModal(message, "info");
						break;
					default:
						this.step.error0 = this.i18n().passwordCheck.error5;
						break;
				}

			}).catch(err => {
				this.loading = false;
				let message = this.i18n().session.sidExpire;
				this.targetPath = "/login?callback=/password/reset";
				this.showModal(message, "info");
			});
		},
		checkPassword(password) {
			if (password.length < 8) {
				return this.i18n().passwordCheck.error1;
			}
			// 匹配字母
			let m1 = /([a-z])+/.test(password);
			let m2 = /([A-Z])+/.test(password);
			// 匹配数字
			let m3 = /([0-9])+/.test(password);
			// 匹配特殊字符
			let m4 = /[^a-zA-Z0-9]+/.test(password);
			if ((m1 | m2) & m3 & m4) {
				return "";
			} else {
				return this.i18n().passwordCheck.error2
			}
		},
		checkSamePassword() {
			// 新旧密码相同的判断
			let b1 = this.values.oldPwd ? true : false;
			let b2 = this.values.pwd ? true : false;
			let res = (b1 && b2) ? this.values.oldPwd == this.values.pwd : false;
			return res;
		},
		modalCallback() {
			this.modalVisiable = false;
			if (this.targetPath) {
				this.$router.push({
					"path": this.targetPath
				});
			}
		},
		userLogout() {
			this.$router.push({
				path: "/"
			});
			this.showForm = false;
		},
		userLogin(data) {
			this.showForm = true;
		},
		showSuccessModel() {
			this.showModal("", "succ");
		}
	},
	watch: {
		'values.oldPwd': function () {
			if (this.checkSamePassword()) {
				this.$set(this.step, 'error1', this.i18n().passwordCheck.error6);
				return;
			} else {
				this.$set(this.step, 'error1', '');
			}
			this.$set(this.step, 'error0', '');
		},
		'values.pwd': function () {
			if (this.checkSamePassword()) {
				this.$set(this.step, 'error1', this.i18n().passwordCheck.error6);
				return;
			}
			let c1 = this.checkPassword(this.values.pwd);
			if (c1) {
				this.$set(this.step, 'error1', c1);
			} else {
				this.$set(this.step, 'error1', '');
			}
		},
		'values.pwdRepeat': function () {
			if (this.values.pwdRepeat != this.values.pwd) {
				this.$set(this.step, 'error2', this.i18n().passwordCheck.error3);
			} else {
				this.$set(this.step, 'error2', '');
			}
		},
	},
	computed: {
		submitBtnDisabled() {
			let b1 = !this.values.oldPwd ? true : false;
			let c1 = this.checkPassword(this.values.pwd);
			let b2 = c1 ? true : false;
			let b3 = this.values.pwd != this.values.pwdRepeat;
			let b4 = this.values.oldPwd == this.values.pwd;
			return b1 || b2 || b3 || b4;
		}
	},
	mounted() {
		// this.showModal("success", "succ");
		window.showSuccessModelTest = this.showSuccessModel;
	},
	created() { },
	components: {
		modalComp,
		modalSimpleComp,
		Auth
	}
}