login.component.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, OnDestroy, Optional } from '@angular/core';
  2. import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
  3. import { Router } from '@angular/router';
  4. import { StartupService } from '@core';
  5. import { ReuseTabService } from '@delon/abc/reuse-tab';
  6. import { DA_SERVICE_TOKEN, ITokenService, SocialOpenType, SocialService } from '@delon/auth';
  7. import { SettingsService, _HttpClient } from '@delon/theme';
  8. import { User } from '@delon/theme/src/services/settings/types';
  9. import { NzTabChangeEvent } from 'ng-zorro-antd/tabs';
  10. import { finalize } from 'rxjs/operators';
  11. import { Message } from '../../../pojo/Message';
  12. import { LocalStorageService } from '../../../service/local-storage.service';
  13. @Component({
  14. selector: 'passport-login',
  15. templateUrl: './login.component.html',
  16. styleUrls: ['./login.component.less'],
  17. providers: [SocialService],
  18. changeDetection: ChangeDetectionStrategy.OnPush
  19. })
  20. export class UserLoginComponent implements OnDestroy {
  21. constructor(
  22. fb: FormBuilder,
  23. private router: Router,
  24. private settingsService: SettingsService,
  25. private socialService: SocialService,
  26. @Optional()
  27. @Inject(ReuseTabService)
  28. private reuseTabService: ReuseTabService,
  29. @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
  30. private startupSrv: StartupService,
  31. private http: _HttpClient,
  32. private cdr: ChangeDetectorRef,
  33. private storageSvc: LocalStorageService
  34. ) {
  35. this.form = fb.group({
  36. userName: [null, [Validators.required]],
  37. password: [null, [Validators.required]],
  38. mobile: [null, [Validators.required, Validators.pattern(/^1\d{10}$/)]],
  39. captcha: [null, [Validators.required]],
  40. remember: [true]
  41. });
  42. }
  43. // #region fields
  44. get userName(): AbstractControl {
  45. return this.form.controls.userName;
  46. }
  47. get password(): AbstractControl {
  48. return this.form.controls.password;
  49. }
  50. get mobile(): AbstractControl {
  51. return this.form.controls.mobile;
  52. }
  53. get captcha(): AbstractControl {
  54. return this.form.controls.captcha;
  55. }
  56. form: FormGroup;
  57. error = '';
  58. type = 0;
  59. loading = false;
  60. // #region get captcha
  61. count = 0;
  62. interval$: any;
  63. // #endregion
  64. switch({ index }: NzTabChangeEvent): void {
  65. this.type = index!;
  66. }
  67. // #endregion
  68. submit(): void {
  69. this.error = '';
  70. if (this.type === 0) {
  71. this.userName.markAsDirty();
  72. this.userName.updateValueAndValidity();
  73. this.password.markAsDirty();
  74. this.password.updateValueAndValidity();
  75. if (this.userName.invalid || this.password.invalid) {
  76. return;
  77. }
  78. } else {
  79. this.mobile.markAsDirty();
  80. this.mobile.updateValueAndValidity();
  81. this.captcha.markAsDirty();
  82. this.captcha.updateValueAndValidity();
  83. if (this.mobile.invalid || this.captcha.invalid) {
  84. return;
  85. }
  86. }
  87. // 默认配置中对所有HTTP请求都会强制 [校验](https://ng-alain.com/auth/getting-started) 用户 Token
  88. // 然一般来说登录请求不需要校验,因此可以在请求URL加上:`/login?_allow_anonymous=true` 表示不触发用户 Token 校验
  89. this.loading = true;
  90. this.cdr.detectChanges();
  91. this.http
  92. .post<Message<any>>('/account/auth/form', {
  93. type: this.type,
  94. identifier: this.userName.value,
  95. credential: this.password.value
  96. })
  97. .pipe(
  98. finalize(() => {
  99. this.loading = false;
  100. this.cdr.detectChanges();
  101. })
  102. )
  103. .subscribe(message => {
  104. if (message.code !== 0) {
  105. this.error = message.msg;
  106. this.cdr.detectChanges();
  107. return;
  108. }
  109. // 清空路由复用信息
  110. this.reuseTabService.clear();
  111. // 设置用户Token信息
  112. this.storageSvc.storageAuthorizationToken(message.data.token);
  113. this.storageSvc.storageRefreshToken(message.data.refreshToken);
  114. let user: User = {
  115. name: this.userName.value,
  116. avatar: './assets/img/avatar.svg',
  117. email: '管理员'
  118. };
  119. this.settingsService.setUser(user);
  120. // 重新获取 StartupService 内容,我们始终认为应用信息一般都会受当前用户授权范围而影响
  121. this.startupSrv.load().subscribe(() => {
  122. let url = this.tokenService.referrer!.url || '/';
  123. if (url.includes('/passport')) {
  124. url = '/';
  125. }
  126. this.router.navigateByUrl(url);
  127. });
  128. });
  129. }
  130. // #endregion
  131. ngOnDestroy(): void {
  132. if (this.interval$) {
  133. clearInterval(this.interval$);
  134. }
  135. }
  136. }