You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
pocketcommunityweb/src/app/account/resetpwd/resetpwd.component.ts

155 lines
4.0 KiB

import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {Commons} from '../../commons';
import {AppService} from '../../app.service';
import {ResetpwdService} from './resetpwd.service';
import {Result} from '../../interface/Result';
import {MessageInterface, MessageUtil} from '../../message/message.service';
import {Router} from '@angular/router';
@Component({
selector: 'app-resetpwd',
templateUrl: './resetpwd.component.html',
styleUrls: ['./resetpwd.component.scss']
})
// 重置密码模块
export class ResetpwdComponent extends Commons implements OnInit, MessageInterface {
// 重置密码表单
resetForm = this.fb.group({
// 管理员名
managerName: this.fb.control('', [Validators.required]),
// 邮箱
email: this.fb.control('', [Validators.required]),
// 验证码
verificationCode: this.fb.control('', [Validators.required]),
// 邮箱类型
emailType: this.fb.control('', [Validators.required]),
// 新密码
password: this.fb.control('', [Validators.required]),
// 确认新密码
confirmPassword: this.fb.control('', [this.checkPwd()]),
});
/**
* 邮箱类型
*/
emailType$ = this.appService.getEmailType();
// 当前步骤
step = 1;
constructor(
private fb: FormBuilder,
private appService: AppService,
private resetpwdService: ResetpwdService,
private messageUtil: MessageUtil,
private router: Router
) {
super();
}
// 检查邮箱
checkEmail() {
this.resetpwdService.checkEmail(this.resetForm.value).subscribe(r => {
if (r.result === Result.OK) {
this.messageUtil.info(this.prefix(r.message));
// 进入第二步
this.step = 2;
// 第二步不允许编辑账户和邮箱
this.disabled();
} else {
this.messageUtil.danger(this.prefix(r.message));
}
});
}
// 第二步不允许编辑账户和邮箱
disabled() {
this.getValue('managerName').disable();
this.getValue('email').disable();
this.getValue('emailType').disable();
}
enabled() {
this.getValue('managerName').enable();
this.getValue('email').enable();
this.getValue('emailType').enable();
}
// 发送验证码
sendCode() {
this.resetpwdService.sendCode({
email: this.getValue('email').value,
emailType: this.getValue('emailType').value
}).subscribe(res => {
if (res.result === Result.OK) {
this.messageUtil.info(this.prefix(res.message));
} else {
this.messageUtil.danger(this.prefix(res.message));
}
});
}
// 返回第一步
backFirst() {
this.step = 1;
this.enabled();
this.getValue('verificationCode').reset();
}
/**
* 检查验证码
*/
checkCode() {
this.resetpwdService.checkCode({
email: this.getValue('email').value,
emailType: this.getValue('emailType').value,
verificationCode: this.getValue('verificationCode').value
}).subscribe(res => {
if (res.result === Result.OK) {
this.messageUtil.info(this.prefix(res.message));
// 进入第三步
this.step = 3;
} else {
this.messageUtil.danger(this.prefix(res.message));
}
});
}
/**
* 重置密码
*/
resetPwd() {
this.resetpwdService.resetPwd({
managerName: this.getValue('managerName').value,
password: this.getValue('password').value
}).subscribe(res => {
if (res.result === Result.OK) {
this.messageUtil.info(this.prefix(res.message));
this.router.navigateByUrl('/login');
} else {
this.messageUtil.danger(this.prefix(res.message));
}
});
}
form(): FormGroup {
return this.resetForm;
}
/**
* 检查是否允许进入第二步
*/
allowCheckEmail() {
return this.getValue('managerName').invalid || this.getValue('email').invalid || this.getValue('emailType').invalid;
}
prefix(key: string): string {
return 'server.resetpwd.' + key;
}
ngOnInit(): void {
}
}