import { Component, OnInit } from '@angular/core'; import {PublicService} from '../../../PublicService'; import {Constants} from '../../../Constant'; import {NavController} from '@ionic/angular'; import {ActivatedRoute} from '@angular/router'; export enum ResetPasswordSteps { GetCode = 0, ResetPassword = 1 } @Component({ selector: 'app-change-pwd', templateUrl: './change-pwd.page.html', styleUrls: ['./change-pwd.page.scss'], }) export class ChangePwdPage implements OnInit { private user; private userInfo; private tel; private interval = null; private isFromLogin = false; private verificationCodeBtnText = '获取验证码'; private title = '修改密码'; private waitingTime = 59; private isWaiting = false; private telCode = ''; private realName = ''; private steps: ResetPasswordSteps = ResetPasswordSteps.GetCode; private newPsw = ''; private reNewPsw = ''; private checkCodeData; constructor(private readonly navController: NavController, private readonly route: ActivatedRoute, private readonly publicService: PublicService) { } ngOnInit() { const params = this.route.snapshot.queryParams; if (!params.from){ this.user = this.publicService.getCurrentUser(); this.userInfo = this.user.userInfo; this.tel = this.user.userInfo.tel; this.realName = this.userInfo.realName; }else{ this.isFromLogin = true; this.title = '重置密码'; } } getTelCode(){ if (this.isWaiting) { return; } if (!this.realName){ this.publicService.presentToast('请输入联系人真实姓名'); return; } if (!this.tel){ this.publicService.presentToast('请输入联系电话'); return; } this.publicService.getTelCode(this.tel, this.realName).then( res => { if (res.code === Constants.SUCCESS){ this.interval = setInterval(() => { if (this.waitingTime === 0) { this.verificationCodeBtnText = '获取验证码'; this.waitingTime = 59; this.isWaiting = false; return clearInterval(this.interval); } this.verificationCodeBtnText = `(${this.waitingTime}s)重新获取`; this.waitingTime -= 1; }, 1000); this.isWaiting = true; }else{ this.publicService.presentToast(res.msg); } }); } toNextStep(){ if (!this.telCode || !this.realName || !this.tel){ this.publicService.presentToast('请输入验证消息'); return; } this.publicService.checkPhoneCode(this.telCode, this.tel, this.realName).then(res => { if (res.code === Constants.SUCCESS){ this.checkCodeData = res.data; this.steps = ResetPasswordSteps.ResetPassword; }else{ this.publicService.presentToast(res.msg); } }); } changePwd(){ const passWordRegExp = new RegExp('^(?![0-9]+$)(?![a-zA-Z]+$)(?!\\W+$)(?![0-9\\W]+$)([0-9A-Za-z\\W]|[A-Za-z\\W]|[0-9A-Za-z]){6,}$'); if (!this.newPsw || this.newPsw.length < 6){ this.publicService.presentToast('密码总长度不足6位'); return; } if (!passWordRegExp.test(this.newPsw)){ this.publicService.presentToast('密码格式错误,需包含字母、符号或数字'); return; } if (this.newPsw !== this.reNewPsw){ this.publicService.presentToast('两次输入密码不一致'); return; } if (this.isFromLogin){ const param = { id: this.checkCodeData.id, operUserId: this.checkCodeData.id, password: this.newPsw }; this.publicService.resetPasswordByUser(param).then(res => { if (res.code === Constants.SUCCESS){ this.publicService.presentToast('密码重置成功'); setTimeout(() => { this.navController.navigateRoot('/login'); }, 1000); }else{ this.publicService.presentToast(res.msg); } }); }else{ const param = { id: this.userInfo.id, oldPassWord: this.user.passWord, newPassWord: this.newPsw, confirmNewPassWord: this.reNewPsw }; this.publicService.updatePassword(param).then(res => { if (res.code === Constants.SUCCESS){ this.publicService.presentToast('密码修改成功'); this.user.passWord = this.newPsw; this.publicService.setCurrentUser(this.user); setTimeout(() => { this.navController.navigateRoot('/tabs/mine'); }, 1000); }else{ this.publicService.presentToast(res.msg); } }); } } back(){ this.navController.back(); } }