admin-login.component.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { CommonModule } from '@angular/common';
  2. import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
  3. import { FormsModule } from '@angular/forms';
  4. import { ActivatedRoute, Router } from '@angular/router';
  5. import { Eye, EyeOff, LoaderCircle, LockKeyhole, LucideAngularModule, ShieldCheck, UserRound } from 'lucide-angular';
  6. import { AdminSessionService } from '../admin-session.service';
  7. import { CloudFunctionsService } from '../cloud-functions.service';
  8. @Component({
  9. selector: 'app-admin-login',
  10. standalone: true,
  11. imports: [CommonModule, FormsModule, LucideAngularModule],
  12. templateUrl: './admin-login.component.html',
  13. styleUrl: './admin-login.component.scss',
  14. changeDetection: ChangeDetectionStrategy.OnPush,
  15. })
  16. export class AdminLoginComponent {
  17. username = '';
  18. password = '';
  19. readonly showPassword = signal(false);
  20. readonly loading = signal(false);
  21. readonly error = signal('');
  22. readonly icons = { Eye, EyeOff, LoaderCircle, LockKeyhole, ShieldCheck, UserRound };
  23. private readonly sessions = inject(AdminSessionService);
  24. private readonly functions = inject(CloudFunctionsService);
  25. private readonly router = inject(Router);
  26. private readonly route = inject(ActivatedRoute);
  27. constructor() { document.title = '后台登录 · 小树陪练'; }
  28. async submit(): Promise<void> {
  29. if (!this.username.trim() || !this.password) {
  30. this.error.set('请输入管理员账号和密码');
  31. return;
  32. }
  33. this.loading.set(true);
  34. this.error.set('');
  35. try {
  36. await this.sessions.login(this.username, this.password);
  37. await this.functions.admin('meta');
  38. const returnUrl = this.route.snapshot.queryParamMap.get('returnUrl') || '/admin/dashboard';
  39. await this.router.navigateByUrl(returnUrl);
  40. } catch (error) {
  41. this.sessions.logout(false);
  42. this.error.set(error instanceof Error ? error.message : '登录失败');
  43. } finally {
  44. this.loading.set(false);
  45. }
  46. }
  47. }