login.component.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { CommonModule } from '@angular/common';
  2. import { Component } from '@angular/core';
  3. import { ActivatedRoute, Router } from '@angular/router';
  4. import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
  5. import { firstValueFrom } from 'rxjs';
  6. import { MSQParse } from '../../app/parse';
  7. import { SaasPlatformService } from '../../app/core/services/saas-platform.service';
  8. import { AsinSkuMappingService } from '../shared/services/asin-sku-mapping.service';
  9. import { PermissionService } from '../shared/services/permission.service';
  10. import { ProductDimensionService } from '../shared/services/product-dimension.service';
  11. import { SharedDataCacheService } from '../shared/services/shared-data-cache.service';
  12. @Component({
  13. selector: 'app-login',
  14. standalone: true,
  15. imports: [CommonModule, ReactiveFormsModule],
  16. templateUrl: './login.component.html',
  17. styleUrls: ['./login.component.scss'],
  18. })
  19. export class LoginComponent {
  20. loginForm: FormGroup;
  21. isLoading = false;
  22. errorMessage = '';
  23. returnUrl = '/voc-insight/products';
  24. constructor(
  25. private readonly fb: FormBuilder,
  26. private readonly router: Router,
  27. private readonly route: ActivatedRoute,
  28. private readonly platform: SaasPlatformService,
  29. private readonly permissionService: PermissionService,
  30. private readonly productDimensionService: ProductDimensionService,
  31. private readonly asinSkuMappingService: AsinSkuMappingService,
  32. private readonly sharedDataCache: SharedDataCacheService,
  33. ) {
  34. this.loginForm = this.fb.group({
  35. username: ['', [Validators.required, Validators.minLength(2)]],
  36. password: ['', [Validators.required, Validators.minLength(6)]],
  37. });
  38. this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || this.returnUrl;
  39. }
  40. async onSubmit(): Promise<void> {
  41. if (!this.loginForm.valid) {
  42. this.loginForm.markAllAsTouched();
  43. return;
  44. }
  45. this.isLoading = true;
  46. this.errorMessage = '';
  47. const { username, password } = this.loginForm.value;
  48. try {
  49. await MSQParse.User.logIn(username, password);
  50. const context = await firstValueFrom(this.platform.context());
  51. if (!context.workspaces.length) {
  52. await MSQParse.User.logOut();
  53. throw new Error('当前账号尚未加入 VOC 工作区');
  54. }
  55. this.clearPermissionCaches();
  56. await this.router.navigateByUrl(this.returnUrl);
  57. } catch (error: unknown) {
  58. const message = error instanceof Error ? error.message : '';
  59. console.error('Login request failed:', message);
  60. this.errorMessage = message || '登录失败,请检查账号和密码后重试';
  61. } finally {
  62. this.isLoading = false;
  63. }
  64. }
  65. private clearPermissionCaches(): void {
  66. this.permissionService.clearCache();
  67. this.productDimensionService.clearCache();
  68. this.asinSkuMappingService.clearCache();
  69. this.sharedDataCache.invalidateAll();
  70. }
  71. }