login.component.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { SettingsService, _HttpClient } from '@delon/theme';
  2. import { Component, OnDestroy, Inject, Optional, Renderer, ElementRef } from '@angular/core';
  3. import { Router } from '@angular/router';
  4. import { FormGroup, FormBuilder, Validators } from '@angular/forms';
  5. import { NzMessageService, NzModalService, NzNotificationService } from 'ng-zorro-antd';
  6. import { SocialService, SocialOpenType, ITokenService, DA_SERVICE_TOKEN } from '@delon/auth';
  7. import { ReuseTabService } from '@delon/abc';
  8. import { environment } from '@env/environment';
  9. import { StartupService } from '@core';
  10. @Component({
  11. selector: 'passport-login',
  12. templateUrl: './login.component.html',
  13. styleUrls: ['./login.component.less'],
  14. providers: [SocialService],
  15. })
  16. export class UserLoginComponent implements OnDestroy {
  17. form: FormGroup;
  18. error = '';
  19. type = 0;
  20. md5 = require('js-md5');
  21. constructor(
  22. fb: FormBuilder,
  23. modalSrv: NzModalService,
  24. private router: Router,
  25. private settingsService: SettingsService,
  26. private socialService: SocialService,
  27. @Optional()
  28. @Inject(ReuseTabService)
  29. private reuseTabService: ReuseTabService,
  30. @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
  31. private startupSrv: StartupService,
  32. public http: _HttpClient,
  33. public msg: NzMessageService,
  34. private notification: NzNotificationService,
  35. private eleRef :ElementRef
  36. ) {
  37. this.form = fb.group({
  38. userName: [null, []],
  39. password: [null, Validators.required],
  40. mobile: [null, [Validators.required, Validators.pattern(/^1\d{10}$/)]],
  41. captcha: [null, [Validators.required]],
  42. remember: [true],
  43. });
  44. modalSrv.closeAll();
  45. }
  46. ngAfterViewInit() {
  47. //这里是重点,需要一个定时器,因为一般操作dom都是异步检查详情我会留下参考的链接
  48. setTimeout(() => {
  49. this.eleRef.nativeElement.querySelector('.input-left').focus();
  50. }, 300);
  51. }
  52. // #region fields
  53. get userName() {
  54. return this.form.controls.userName;
  55. }
  56. get password() {
  57. return this.form.controls.password;
  58. }
  59. get mobile() {
  60. return this.form.controls.mobile;
  61. }
  62. get captcha() {
  63. return this.form.controls.captcha;
  64. }
  65. // #endregion
  66. switch(ret: any) {
  67. this.type = ret.index;
  68. }
  69. // #region get captcha
  70. count = 0;
  71. interval$: any;
  72. getCaptcha() {
  73. if (this.mobile.invalid) {
  74. this.mobile.markAsDirty({ onlySelf: true });
  75. this.mobile.updateValueAndValidity({ onlySelf: true });
  76. return;
  77. }
  78. this.count = 59;
  79. this.interval$ = setInterval(() => {
  80. this.count -= 1;
  81. if (this.count <= 0) clearInterval(this.interval$);
  82. }, 1000);
  83. }
  84. // #endregion
  85. submit() {
  86. console.log(this.password)
  87. this.error = '';
  88. if (this.type === 0) {
  89. this.userName.markAsDirty();
  90. this.userName.updateValueAndValidity();
  91. this.password.markAsDirty();
  92. this.password.updateValueAndValidity();
  93. if (this.userName.invalid || this.password.invalid) return;
  94. } else {
  95. this.mobile.markAsDirty();
  96. this.mobile.updateValueAndValidity();
  97. this.captcha.markAsDirty();
  98. this.captcha.updateValueAndValidity();
  99. if (this.mobile.invalid || this.captcha.invalid) return;
  100. }
  101. // 默认配置中对所有HTTP请求都会强制 [校验](https://ng-alain.com/auth/getting-started) 用户 Token
  102. // 然一般来说登录请求不需要校验,因此可以在请求URL加上:`/login?_allow_anonymous=true` 表示不触发用户 Token 校验
  103. let url = "sys/login";//'/login/account?_allow_anonymous=true'
  104. this.http
  105. .post(url, {
  106. type: this.type,
  107. username: this.userName.value,
  108. password: this.md5(this.password.value),
  109. passwordNotencryption:this.password.value
  110. })
  111. .subscribe((res: any) => {
  112. //if (res.msg !== 'ok') {
  113. if (res.code !== 200) {
  114. this.error = res.message;
  115. return;
  116. }
  117. // 清空路由复用信息
  118. this.reuseTabService.clear();
  119. // 设置用户Token信息
  120. //this.tokenService.set(res.user);
  121. this.tokenService.set(res.result);
  122. //console.log(res);
  123. // 重新获取 StartupService 内容,我们始终认为应用信息一般都会受当前用户授权范围而影响
  124. this.startupSrv.load().then(() => {
  125. /*
  126. let url = this.tokenService.referrer!.url || '/dashboard/analysis';
  127. if (url.includes('/passport')) url = '/';
  128. this.router.navigateByUrl(url);
  129. this.notification.remove();
  130. this.notification.success("欢迎 "+res.result.userInfo.username,res.result.timeTag+"好!欢迎回来");*/
  131. if(this.settingsService.user.name==="quality"){
  132. this.router.navigateByUrl('/fbs-quality-qualification-rate/list');
  133. }else{
  134. this.router.navigateByUrl('/dashboard/analysis');
  135. }
  136. });
  137. });
  138. }
  139. ngOnDestroy(): void {
  140. if (this.interval$) clearInterval(this.interval$);
  141. }
  142. }