| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import { CommonModule } from '@angular/common';
- import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
- import { FormsModule } from '@angular/forms';
- import { ActivatedRoute, Router } from '@angular/router';
- import { Eye, EyeOff, LoaderCircle, LockKeyhole, LucideAngularModule, ShieldCheck, UserRound } from 'lucide-angular';
- import { AdminSessionService } from '../admin-session.service';
- import { CloudFunctionsService } from '../cloud-functions.service';
- @Component({
- selector: 'app-admin-login',
- standalone: true,
- imports: [CommonModule, FormsModule, LucideAngularModule],
- templateUrl: './admin-login.component.html',
- styleUrl: './admin-login.component.scss',
- changeDetection: ChangeDetectionStrategy.OnPush,
- })
- export class AdminLoginComponent {
- username = '';
- password = '';
- readonly showPassword = signal(false);
- readonly loading = signal(false);
- readonly error = signal('');
- readonly icons = { Eye, EyeOff, LoaderCircle, LockKeyhole, ShieldCheck, UserRound };
- private readonly sessions = inject(AdminSessionService);
- private readonly functions = inject(CloudFunctionsService);
- private readonly router = inject(Router);
- private readonly route = inject(ActivatedRoute);
- constructor() { document.title = '后台登录 · 小树陪练'; }
- async submit(): Promise<void> {
- if (!this.username.trim() || !this.password) {
- this.error.set('请输入管理员账号和密码');
- return;
- }
- this.loading.set(true);
- this.error.set('');
- try {
- await this.sessions.login(this.username, this.password);
- await this.functions.admin('meta');
- const returnUrl = this.route.snapshot.queryParamMap.get('returnUrl') || '/admin/dashboard';
- await this.router.navigateByUrl(returnUrl);
- } catch (error) {
- this.sessions.logout(false);
- this.error.set(error instanceof Error ? error.message : '登录失败');
- } finally {
- this.loading.set(false);
- }
- }
- }
|