2021-11-27 22:21:52 +08:00
|
|
|
|
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, OnDestroy, Optional } from '@angular/core';
|
|
|
|
|
|
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
|
import { StartupService } from '@core';
|
|
|
|
|
|
import { ReuseTabService } from '@delon/abc/reuse-tab';
|
|
|
|
|
|
import { DA_SERVICE_TOKEN, ITokenService, SocialOpenType, SocialService } from '@delon/auth';
|
|
|
|
|
|
import { SettingsService, _HttpClient } from '@delon/theme';
|
2021-12-23 15:59:49 +08:00
|
|
|
|
import { User } from '@delon/theme/src/services/settings/types';
|
2021-11-27 22:21:52 +08:00
|
|
|
|
import { NzTabChangeEvent } from 'ng-zorro-antd/tabs';
|
|
|
|
|
|
import { finalize } from 'rxjs/operators';
|
2021-12-23 15:59:49 +08:00
|
|
|
|
|
|
|
|
|
|
import { Message } from '../../../pojo/Message';
|
|
|
|
|
|
import { LocalStorageService } from '../../../service/local-storage.service';
|
2021-11-27 22:21:52 +08:00
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
|
selector: 'passport-login',
|
|
|
|
|
|
templateUrl: './login.component.html',
|
|
|
|
|
|
styleUrls: ['./login.component.less'],
|
|
|
|
|
|
providers: [SocialService],
|
|
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
|
|
|
|
})
|
|
|
|
|
|
export class UserLoginComponent implements OnDestroy {
|
|
|
|
|
|
constructor(
|
|
|
|
|
|
fb: FormBuilder,
|
|
|
|
|
|
private router: Router,
|
|
|
|
|
|
private settingsService: SettingsService,
|
|
|
|
|
|
private socialService: SocialService,
|
|
|
|
|
|
@Optional()
|
|
|
|
|
|
@Inject(ReuseTabService)
|
|
|
|
|
|
private reuseTabService: ReuseTabService,
|
|
|
|
|
|
@Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
|
|
|
|
|
|
private startupSrv: StartupService,
|
|
|
|
|
|
private http: _HttpClient,
|
2021-12-03 09:15:53 +08:00
|
|
|
|
private cdr: ChangeDetectorRef,
|
|
|
|
|
|
private storageSvc: LocalStorageService
|
2021-11-27 22:21:52 +08:00
|
|
|
|
) {
|
|
|
|
|
|
this.form = fb.group({
|
2021-12-03 09:15:53 +08:00
|
|
|
|
userName: [null, [Validators.required]],
|
|
|
|
|
|
password: [null, [Validators.required]],
|
2021-11-27 22:21:52 +08:00
|
|
|
|
mobile: [null, [Validators.required, Validators.pattern(/^1\d{10}$/)]],
|
|
|
|
|
|
captcha: [null, [Validators.required]],
|
|
|
|
|
|
remember: [true]
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #region fields
|
|
|
|
|
|
|
|
|
|
|
|
get userName(): AbstractControl {
|
|
|
|
|
|
return this.form.controls.userName;
|
|
|
|
|
|
}
|
|
|
|
|
|
get password(): AbstractControl {
|
|
|
|
|
|
return this.form.controls.password;
|
|
|
|
|
|
}
|
|
|
|
|
|
get mobile(): AbstractControl {
|
|
|
|
|
|
return this.form.controls.mobile;
|
|
|
|
|
|
}
|
|
|
|
|
|
get captcha(): AbstractControl {
|
|
|
|
|
|
return this.form.controls.captcha;
|
|
|
|
|
|
}
|
|
|
|
|
|
form: FormGroup;
|
|
|
|
|
|
error = '';
|
|
|
|
|
|
type = 0;
|
|
|
|
|
|
loading = false;
|
|
|
|
|
|
|
|
|
|
|
|
// #region get captcha
|
|
|
|
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
interval$: any;
|
|
|
|
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
switch({ index }: NzTabChangeEvent): void {
|
|
|
|
|
|
this.type = index!;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
submit(): void {
|
|
|
|
|
|
this.error = '';
|
|
|
|
|
|
if (this.type === 0) {
|
|
|
|
|
|
this.userName.markAsDirty();
|
|
|
|
|
|
this.userName.updateValueAndValidity();
|
|
|
|
|
|
this.password.markAsDirty();
|
|
|
|
|
|
this.password.updateValueAndValidity();
|
|
|
|
|
|
if (this.userName.invalid || this.password.invalid) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.mobile.markAsDirty();
|
|
|
|
|
|
this.mobile.updateValueAndValidity();
|
|
|
|
|
|
this.captcha.markAsDirty();
|
|
|
|
|
|
this.captcha.updateValueAndValidity();
|
|
|
|
|
|
if (this.mobile.invalid || this.captcha.invalid) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 默认配置中对所有HTTP请求都会强制 [校验](https://ng-alain.com/auth/getting-started) 用户 Token
|
|
|
|
|
|
// 然一般来说登录请求不需要校验,因此可以在请求URL加上:`/login?_allow_anonymous=true` 表示不触发用户 Token 校验
|
|
|
|
|
|
this.loading = true;
|
|
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
|
|
this.http
|
2021-12-03 09:15:53 +08:00
|
|
|
|
.post<Message<any>>('/account/auth/form', {
|
2021-11-27 22:21:52 +08:00
|
|
|
|
type: this.type,
|
2021-12-03 09:15:53 +08:00
|
|
|
|
identifier: this.userName.value,
|
2022-01-30 12:34:20 +08:00
|
|
|
|
credential: this.password.value
|
2021-11-27 22:21:52 +08:00
|
|
|
|
})
|
|
|
|
|
|
.pipe(
|
|
|
|
|
|
finalize(() => {
|
2021-12-03 09:15:53 +08:00
|
|
|
|
this.loading = false;
|
2021-11-27 22:21:52 +08:00
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
|
|
})
|
|
|
|
|
|
)
|
2021-12-03 09:15:53 +08:00
|
|
|
|
.subscribe(message => {
|
|
|
|
|
|
if (message.code !== 0) {
|
|
|
|
|
|
this.error = message.msg;
|
2021-11-27 22:21:52 +08:00
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 清空路由复用信息
|
|
|
|
|
|
this.reuseTabService.clear();
|
|
|
|
|
|
// 设置用户Token信息
|
2021-12-03 09:15:53 +08:00
|
|
|
|
this.storageSvc.storageAuthorizationToken(message.data.token);
|
|
|
|
|
|
this.storageSvc.storageRefreshToken(message.data.refreshToken);
|
2021-12-23 15:59:49 +08:00
|
|
|
|
let user: User = {
|
2021-12-03 17:56:01 +08:00
|
|
|
|
name: this.userName.value,
|
2021-12-23 15:59:49 +08:00
|
|
|
|
avatar: './assets/img/avatar.svg',
|
|
|
|
|
|
email: '管理员'
|
|
|
|
|
|
};
|
2021-12-03 17:56:01 +08:00
|
|
|
|
this.settingsService.setUser(user);
|
2021-11-27 22:21:52 +08:00
|
|
|
|
// 重新获取 StartupService 内容,我们始终认为应用信息一般都会受当前用户授权范围而影响
|
|
|
|
|
|
this.startupSrv.load().subscribe(() => {
|
|
|
|
|
|
let url = this.tokenService.referrer!.url || '/';
|
|
|
|
|
|
if (url.includes('/passport')) {
|
|
|
|
|
|
url = '/';
|
|
|
|
|
|
}
|
|
|
|
|
|
this.router.navigateByUrl(url);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
|
if (this.interval$) {
|
|
|
|
|
|
clearInterval(this.interval$);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|