説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

login.component.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Component, OnInit, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
  4. import { Router } from '@angular/router';
  5. import { MatCardModule } from '@angular/material/card';
  6. import { MatFormFieldModule } from '@angular/material/form-field';
  7. import { MatInputModule } from '@angular/material/input';
  8. import { MatButtonModule } from '@angular/material/button';
  9. import { MatIconModule } from '@angular/material/icon';
  10. import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
  11. import { AuthService } from '../../services/auth.service';
  12. import { EnterNavigationDirective } from 'base-core';
  13. @Component({
  14. selector: 'app-login',
  15. standalone: true,
  16. imports: [
  17. CommonModule,
  18. ReactiveFormsModule,
  19. MatCardModule,
  20. MatFormFieldModule,
  21. MatInputModule,
  22. MatButtonModule,
  23. MatIconModule,
  24. MatProgressSpinnerModule,
  25. EnterNavigationDirective
  26. ],
  27. templateUrl: './login.component.html',
  28. styleUrl: './login.component.scss'
  29. })
  30. export class LoginComponent implements OnInit, AfterViewInit {
  31. loginForm: FormGroup;
  32. isLoading = false;
  33. errorMessage = '';
  34. hidePassword = true;
  35. @ViewChild('usernameInput') usernameInput!: ElementRef<HTMLInputElement>;
  36. constructor(
  37. private fb: FormBuilder,
  38. private authService: AuthService,
  39. private router: Router
  40. ) {
  41. this.loginForm = this.fb.group({
  42. username: ['', [Validators.required]],
  43. password: ['', [Validators.required]]
  44. });
  45. }
  46. ngOnInit(): void {
  47. if (this.authService.isAuthenticated()) {
  48. this.router.navigate(['/']);
  49. }
  50. }
  51. ngAfterViewInit(): void {
  52. setTimeout(() => {
  53. if (this.usernameInput?.nativeElement) {
  54. this.usernameInput.nativeElement.focus();
  55. }
  56. }, 100);
  57. }
  58. onSubmit(): void {
  59. if (this.loginForm.invalid) {
  60. this.loginForm.markAllAsTouched();
  61. return;
  62. }
  63. this.isLoading = true;
  64. this.errorMessage = '';
  65. const { username, password } = this.loginForm.value;
  66. this.authService.login(username, password).subscribe({
  67. next: (response) => {
  68. this.isLoading = false;
  69. if (response.success) {
  70. this.router.navigate(['/']);
  71. } else {
  72. this.errorMessage = response.message || response.error || '登录失败';
  73. }
  74. },
  75. error: (error) => {
  76. this.isLoading = false;
  77. this.errorMessage = error.message || '网络错误,请检查连接';
  78. console.error('登录错误:', error);
  79. }
  80. });
  81. }
  82. get username() {
  83. return this.loginForm.get('username');
  84. }
  85. get password() {
  86. return this.loginForm.get('password');
  87. }
  88. }