import { Component, inject } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { Router, RouterModule } from '@angular/router'; import { FaIconComponent } from '@fortawesome/angular-fontawesome'; import { faEnvelope, faLock, faEye, faEyeSlash, faCircleCheck, } from '@fortawesome/free-solid-svg-icons'; import { AuthService } from '../../../core/auth/auth.service'; @Component({ selector: 'app-forgot-password', standalone: true, imports: [FormsModule, FaIconComponent, RouterModule], templateUrl: './forgot-password.component.html', }) export class ForgotPasswordComponent { private authService = inject(AuthService); private router = inject(Router); protected readonly faEnvelope = faEnvelope; protected readonly faLock = faLock; protected readonly faEye = faEye; protected readonly faEyeSlash = faEyeSlash; protected readonly faCircleCheck = faCircleCheck; email = ''; newPassword = ''; confirmPassword = ''; showPassword = false; showConfirmPassword = false; submitted = false; success = false; errorMessage = ''; protected togglePasswordVisibility(): void { this.showPassword = !this.showPassword; } protected toggleConfirmPasswordVisibility(): void { this.showConfirmPassword = !this.showConfirmPassword; } protected async onSubmit(): Promise { this.submitted = true; this.errorMessage = ''; if (!this.email.trim()) { this.errorMessage = '请输入邮箱地址'; return; } if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.email.trim())) { this.errorMessage = '邮箱格式不正确'; return; } if (!this.newPassword) { this.errorMessage = '请输入新密码'; return; } if (this.newPassword.length < 6) { this.errorMessage = '新密码长度不能少于6位'; return; } if (this.newPassword !== this.confirmPassword) { this.errorMessage = '两次输入的密码不一致'; return; } const result = await this.authService.resetPasswordByEmail( this.email.trim(), this.newPassword, ); if (result.ok) { this.success = true; } else { this.errorMessage = result.error ?? '该邮箱未注册,请检查后重试'; } } protected goToLogin(): void { this.router.navigate(['/login'], { queryParams: { reset: 'true' } }); } }