You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.8 KiB
65 lines
1.8 KiB
import {Component, OnInit} from '@angular/core';
|
|
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
|
import {Commons} from '../../commons';
|
|
import {Router} from '@angular/router';
|
|
import {LoginService} from './login.service';
|
|
import {Result} from '../../interface/Result';
|
|
import {environment} from '../../../environments/environment';
|
|
import {CookieService} from 'ngx-cookie-service';
|
|
import {MessageService} from '../../message/message.service';
|
|
|
|
@Component({
|
|
selector: 'app-login',
|
|
templateUrl: './login.component.html',
|
|
styleUrls: ['./login.component.scss']
|
|
})
|
|
// 登陆模块
|
|
export class LoginComponent extends Commons implements OnInit {
|
|
// 登陆表单
|
|
loginForm = this.fb.group({
|
|
// 管理员名
|
|
managerName: this.fb.control('', Validators.required),
|
|
// 密码
|
|
password: this.fb.control('', Validators.required)
|
|
});
|
|
|
|
|
|
constructor(private fb: FormBuilder,
|
|
private router: Router,
|
|
private loginService: LoginService,
|
|
private cookieService: CookieService,
|
|
private messageService: MessageService
|
|
|
|
) {
|
|
super();
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
|
|
}
|
|
|
|
// 登陆方法
|
|
login() {
|
|
console.debug(this.loginForm.value);
|
|
|
|
// 发送登陆请求
|
|
this.loginService.login(JSON.stringify(this.loginForm.value)).subscribe(r => {
|
|
if (r.result === Result.OK) {
|
|
this.messageService.info('登陆成功');
|
|
const time = new Date();
|
|
time.setTime(r.body.useTime);
|
|
this.cookieService.set(environment.managerKey, r.body.managerName, time);
|
|
this.cookieService.set(environment.tokenKey, r.body.token, time);
|
|
this.router.navigateByUrl('/forum');
|
|
} else {
|
|
this.messageService.danger('登陆失败');
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
form(): FormGroup {
|
|
return this.loginForm;
|
|
}
|
|
|
|
}
|
|
|