password-find.js 7.04 KB
import api from '@/api/api'
import {
	httpGet,
	httpPost
} from '@/api/fetch-api.js'

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,
			key: 'value',
			type: 1, // 1:输入用户信息 2:找回密码 3:核对信息 4.重置密码
			values: {
				cid: "",
			},
			times: {
				interval: 0, // 索引
				remain: 0, // 剩余时间
				tip: "" // 显示的文字
			},
			modalSimpleVisiable: false,
			modalVisiable: false,
			targetPath: "",
			modalIcon: "succ",
			modalContent: "",
			step1: {
				userId: "",
				error: ""
			},
			step2: {
				idNo: ""
			},
			step3: {
				otp: "",
				error: ""
			},
			step4: {
				pwd: "",
				error1: "",
				pwdRepeat: "",
				error2: "",
				success: false
			}
		}
	},
	components: {},
	computed: {
		s1BtnDisabled() {
			let b1 = !this.step1.userId ? true : false;
			return b1;
		},
		s3BtnDisabled() {
			let b1 = !this.step3.otp ? true : false;
			return b1;
		},
		s4BtnDisabled() {
			let b1 = !this.step4.pwd ? true : false;
			let b2 = !this.step4.pwdRepeat ? true : false;
			return b1 || b2;
		}
	},
	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;
		},
		locateUserInfo() {
			if (this.s1BtnDisabled) {
				return
			}
			// 定位用户信息
			if (!this.step1.userId) {
				this.step1.error = this.i18n().passwordReset.type1.error;
				return;
			}
			httpPost({
				url: api.locateUserInfo,
				data: {
					uid: this.step1.userId
				}
			}).then(response => {
				if (response.returnCode !== "0") {
					this.step1.error = this.i18n().passwordReset.type1.error;
				} else {
					this.values.cid = response.data.cid;
					this.step2.idNo = response.data.idNo;
					if (typeof this.step2.idNo == "undefined") {
						this.type = 3;
					} else {
						this.type = 2;
					}
				}
			});
		},
		sureIdNo() {
			this.type = 3;
		},
		sendOtp() {
			if (this.times.remain > 0) {
				return;
			}
			if (this.loading) {
				return;
			}
			this.loading = true;
			// 定位用户
			httpPost({
				url: api.sendOtp,
				data: {
					cid: this.values.cid
				}
			}).then(response => {
				this.loading = false;
				if (this.responseHandler(response)) {
					switch (response.returnCode) {
						case "COMMON_ERROR_E3":
							this.step2.error = this.i18n().passwordReset.type2.error3;
							return;
						default:
							this.startTimeClick();
							return;
					}
				}
			}).catch(err => {
				this.loading = false;
			})
		},
		checkOtp() {
			if (this.s3BtnDisabled) {
				return;
			}
			// 发送验证码
			if (!this.step3.otp) {
				this.step3.error = this.i18n().passwordReset.type2.error1;
				return;
			}
			// 定位用户
			httpPost({
				url: api.checkOtp,
				data: {
					cid: this.values.cid,
					otp: this.step3.otp
				}
			}).then(response => {
				if (this.responseHandler(response)) {
					switch (response.returnCode) {
						case "0":
							this.type = 4;
							return;
						default:
							// 提示驗證碼錯誤
							this.step3.error = this.i18n().passwordReset.type2.error1;
							return;
					}
				}
			})
		},
		resetPwd() {
			if (this.s4BtnDisabled) {
				return;
			}
			let c1 = this.checkPassword(this.step4.pwd);
			if (c1) {
				this.$set(this.step, 'error1', c1);
				return;
			}
			if (this.step4.pwdRepeat != this.step4.pwd) {
				this.step4.error2 = this.i18n().passwordCheck.error3;
				return;
			}
			let pwd = passwordEncrypt(this.step4.pwd);
			if (this.loading) {
				return;
			}
			this.loading = true;
			httpPost({
				url: api.resetPwd,
				data: {
					cid: this.values.cid,
					password: pwd
				}
			}).then(response => {
				this.loading = false;
				if (this.responseHandler(response)) {
					let message = this.i18n().passwordCheck.success;
					this.step4.success = true;
					switch (response.returnCode) {
						case "0":
							this.showModal(message, "succ");
							break
						default:
							message = this.i18n().passwordCheck.error4;
							this.showModal(message, "info");
							break;
					}
				}
			}).catch(err => {
				this.loading = false;
			})
		},
		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);
			console.log(m1, m2, m3, m4)
			if ((m1 | m2) & m3 & m4) {
				return "";
			} else {
				return this.i18n().passwordCheck.error2
			}
		},
		startTimeClick() {
			// 開始倒計時
			this.times.remain = 120;
			let _this = this;
			let i18n = this.$i18n.messages[this.$i18n.locale] || {};
			let msg = i18n.register.coutTips;
			_this.times.tip = msg.replace("{second}", _this.times.remain);
			this.times.interval = setInterval(function () {
				if (_this.times.remain <= 0) {
					clearInterval(_this.times.interval);
					_this.times.interval = 0;
					_this.times.remain = 0;
					return;
				}
				_this.times.remain--;
				_this.times.tip = msg.replace("{second}", _this.times.remain);
				_this.$set(_this, 'times', _this.times);
			}, 1000);
		},
		responseHandler(response) {
			switch (response.returnCode) {
				case "COMMON_ERROR_E2":
					// 身份过期了,请重新登录
					this.values.cid = "";
					this.showModal(this.i18n().passwordReset.cidExpire, "info");
					this.step2.idNo = "";
					this.step3.otp = "";
					this.step3.error = "";
					this.step4.pwd = "";
					this.step4.pwdRepeat = "";
					this.step4.error1 = "";
					this.step4.error2 = "";
					this.step4.success = false;
					this.type = 1;
					return false;
				default:
					return true;
			}
		},
		modalCallback() {
			this.modalVisiable = false;
			if (this.step4.success) {
				this.$router.push({
					path: "/login"
				})
			} else {

			}
		},
		showSuccessModel() {
			this.showModal("", "succ");
		}
	},
	watch: {
		'step1.userId': function () {
			this.step1.error = "";
		},
		'step3.otp': function () {
			this.step3.error = "";
		},
		'step4.pwd': function () {
			console.log(this.checkPassword(this.step4.pwd));
			this.$set(this.step4, 'error1', this.checkPassword(this.step4.pwd));
		},
		'step4.pwdRepeat': function () {
			if (this.step4.pwdRepeat != this.step4.pwd) {
				this.$set(this.step4, 'error2', this.i18n().passwordCheck.error3);
			} else {
				this.$set(this.step4, 'error2', '');
			}
		},
	},
	mounted() {
		window.showSuccessModelTest = this.showSuccessModel;
	},
	created() { },
	components: {
		modalComp,
		modalSimpleComp
	}
}