|
|
|
import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
|
|
|
|
import { HttpClient } from '@angular/common/http'
|
|
|
|
import { Router, ActivatedRoute } from '@angular/router'
|
|
|
|
import { CacheTokenService } from '../../service/cache-token.service'//引入服务
|
|
|
|
import { CookieService } from 'ngx-cookie-service';//cookie插件
|
|
|
|
|
|
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
|
|
import { NzMessageService } from 'ng-zorro-antd/message';
|
|
|
|
@Component({
|
|
|
|
selector: 'app-login',
|
|
|
|
templateUrl: './login.component.html',
|
|
|
|
styleUrls: ['./login.component.scss'],
|
|
|
|
|
|
|
|
})
|
|
|
|
export class LoginComponent implements OnInit {
|
|
|
|
|
|
|
|
validateForm!: FormGroup;
|
|
|
|
|
|
|
|
constructor(private http: HttpClient, private router: Router, private route: ActivatedRoute, public token: CacheTokenService, private cookieService: CookieService, private fb: FormBuilder, private message: NzMessageService) { }
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.validateForm = this.fb.group({
|
|
|
|
userName: [null, [Validators.required]],
|
|
|
|
password: [null, [Validators.required]],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
errmsg: string = ''; //错误信息
|
|
|
|
|
|
|
|
|
|
|
|
//跳转注册页面
|
|
|
|
toRegister() {
|
|
|
|
this.router.navigate(['/register'])
|
|
|
|
}
|
|
|
|
|
|
|
|
submitForm(): void {
|
|
|
|
for (const i in this.validateForm.controls) {
|
|
|
|
this.validateForm.controls[i].markAsDirty();
|
|
|
|
this.validateForm.controls[i].updateValueAndValidity();
|
|
|
|
}
|
|
|
|
if (!this.validateForm.valid) {
|
|
|
|
this.message.create('error', `请输入账号密码`);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!this.selectedRole) {
|
|
|
|
this.message.create('error', `请选择登录角色`);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.http.post('/api/TokenAuth/Authenticate', {
|
|
|
|
userNameOrEmailAddress: this.validateForm.value.userName,
|
|
|
|
password: this.validateForm.value.password
|
|
|
|
}).subscribe(
|
|
|
|
(data: any) => {
|
|
|
|
this.message.create('success', `登陆成功`);
|
|
|
|
sessionStorage.setItem("token", data.result.accessToken);
|
|
|
|
this.cookieService.set("token", data.result.accessToken, null, '/');
|
|
|
|
this.cookieService.set("refreshToken", data.result.encryptedAccessToken, null, '/');
|
|
|
|
this.router.navigate(['/home/plan'])
|
|
|
|
//调用服务中的function刷新token
|
|
|
|
// this.token.startUp()
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
this.message.create('error', err.error.error.details);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
roleList = [
|
|
|
|
'管理员', '职工'
|
|
|
|
]
|
|
|
|
|
|
|
|
selectedRole: string
|
|
|
|
selecteRole(role) {
|
|
|
|
this.selectedRole = role
|
|
|
|
}
|
|
|
|
forget() {
|
|
|
|
this.message.create('warning', `请联系管理员`);
|
|
|
|
}
|
|
|
|
}
|