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.
 
 
 
 
pocketcommunityweb/src/app/account/login/login.component.ts

69 lines
1.9 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 {MessageInterface, MessageUtil} from '../../message/message.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
// 登陆模块
export class LoginComponent extends Commons implements OnInit, MessageInterface {
// 登陆表单
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 messageUtil: MessageUtil
) {
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.messageUtil.info(this.prefix(r.message));
const time = new Date();
time.setTime(r.managerToken.useTime);
this.cookieService.set(environment.managerKey, r.managerToken.managerName, time);
this.cookieService.set(environment.tokenKey, r.managerToken.token, time);
location.href = '/forum';
} else {
this.messageUtil.danger(this.prefix(r.message));
}
});
}
form(): FormGroup {
return this.loginForm;
}
prefix(key: string): string {
return 'server.login.' + key;
}
}