diff --git a/src/app/account/account/account.module.ts b/src/app/account/account/account.module.ts
index e59873c..a04c49c 100644
--- a/src/app/account/account/account.module.ts
+++ b/src/app/account/account/account.module.ts
@@ -1,5 +1,5 @@
-import { NgModule } from '@angular/core';
-import { CommonModule } from '@angular/common';
+import {NgModule} from '@angular/core';
+import {CommonModule} from '@angular/common';
// 登陆组件
import {LoginComponent} from '../login/login.component';
// 注册组件
@@ -26,4 +26,5 @@ import {InputComponent} from '../../input/input.component';
ReactiveFormsModule,
]
})
-export class AccountModule { }
+export class AccountModule {
+}
diff --git a/src/app/account/login/login.component.html b/src/app/account/login/login.component.html
index 0cb64d3..599c425 100644
--- a/src/app/account/login/login.component.html
+++ b/src/app/account/login/login.component.html
@@ -8,7 +8,13 @@
{{ 'login.manager_name' | translate }}
+ [class.is-invalid]="getValue('managerName').invalid"
+ [placeholder]="'tip.input' | translate:{value:'login.manager_name'|translate}"
+ formControlName="managerName">
+
+
+ {{'tip.notnull' | translate:{value: 'login.manager_name'|translate} }}
+
@@ -17,17 +23,24 @@
{{ 'login.password' | translate }}
+
+
+ {{'tip.notnull' | translate:{value: 'login.password'|translate} }}
+
+
-
+
diff --git a/src/app/account/login/login.component.ts b/src/app/account/login/login.component.ts
index 9c7b6de..fe948f6 100644
--- a/src/app/account/login/login.component.ts
+++ b/src/app/account/login/login.component.ts
@@ -1,5 +1,5 @@
import {Component, OnInit} from '@angular/core';
-import {FormBuilder} from '@angular/forms';
+import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {Commons} from '../../commons';
import {Router} from '@angular/router';
import {LoginService} from './login.service';
@@ -13,8 +13,10 @@ import {LoginService} from './login.service';
export class LoginComponent extends Commons implements OnInit {
// 登陆表单
loginForm = this.fb.group({
- managerName: [''],
- password: ['']
+ // 管理员名
+ managerName: this.fb.control('', Validators.required),
+ // 密码
+ password: this.fb.control('', Validators.required)
});
@@ -39,4 +41,8 @@ export class LoginComponent extends Commons implements OnInit {
}
+ form(): FormGroup {
+ return this.loginForm;
+ }
+
}
diff --git a/src/app/account/login/login.service.ts b/src/app/account/login/login.service.ts
index 5d7850d..c047bd8 100644
--- a/src/app/account/login/login.service.ts
+++ b/src/app/account/login/login.service.ts
@@ -3,6 +3,13 @@ import {CookieService} from 'ngx-cookie-service';
import {Router} from '@angular/router';
import {MessageService} from '../../message/message.service';
import {JSONRequest} from '../../interface/JSONRequest';
+import {environment} from '../../../environments/environment';
+import {HttpClient} from '@angular/common/http';
+import {HttpInterface} from '../../interface/HttpInterface';
+import {JSONResponse} from '../../interface/JSONResponse';
+import {catchError} from 'rxjs/operators';
+import {Result} from '../../interface/Result';
+import {Token} from '../../interface/vo/Token';
@Injectable({
providedIn: 'root'
@@ -12,7 +19,8 @@ export class LoginService extends JSONRequest {
constructor(
private cookieService: CookieService,
private router: Router,
- private messageService: MessageService
+ private messageService: MessageService,
+ private http: HttpClient,
) {
super();
}
@@ -21,7 +29,7 @@ export class LoginService extends JSONRequest {
* 检查登陆令牌是否有效
*/
checkToken(): boolean {
- return false;
+ return this.cookieService.check(environment.tokenKey);
}
/**
@@ -35,22 +43,45 @@ export class LoginService extends JSONRequest {
}
}
+ /**
+ * 注销登录
+ */
+ logout() {
+ this.http.post
>(HttpInterface.logout, {}, this.httpOptions)
+ .pipe(
+ catchError(this.handleError('注销'))
+ )
+ .subscribe(r => {
+ if (r.result === Result.OK) {
+ this.cookieService.deleteAll();
+ this.messageService.info('注销成功');
+ this.router.navigateByUrl('/login');
+ } else {
+ alert('注销失败');
+ }
+ });
+ }
+
/**
*
* @param body 请求表单
*/
- request(body: string) {
- // res => {
- // this.message = res.message;
- // if (res.result === 'OK') {
- // this.messageStyle = 'info';
- // this.cookieService.set(environment.tokenKey, res.body.token, 3600);
- // this.router.navigateByUrl('/forum');
- // } else {
- // this.messageStyle = 'warning';
- // }
- // }
- this.messageService.danger('登陆失败');
- return false;
+ request(body) {
+ this.http.post>(HttpInterface.login, body, this.httpOptions)
+ .pipe(
+ catchError(this.handleError('登陆'))
+ )
+ .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('登陆失败');
+ }
+ });
}
}
diff --git a/src/app/account/register/register.component.html b/src/app/account/register/register.component.html
index 390051d..29a021a 100644
--- a/src/app/account/register/register.component.html
+++ b/src/app/account/register/register.component.html
@@ -7,11 +7,11 @@
{{ 'login.manager_name' | translate }}
-
- {{'tip.notnull' | translate:{value:'login.manager_name'|translate} }}
+ {{'tip.notnull' | translate:{value: 'login.manager_name'|translate} }}
@@ -21,11 +21,11 @@
{{ 'login.password' | translate }}
- {{'tip.notnull' | translate:{value:'login.password'|translate} }}
+ {{'tip.notnull' | translate:{value: 'login.password'|translate} }}
@@ -36,13 +36,17 @@
{{ 'register.confirm_pwd' | translate }}
-
- {{ checkPwd()|async }}
-
+
+
+ {{'tip.notnull' | translate:{value: 'register.confirm_pwd'|translate} }}
+
+
+ {{'tip.password_diff'|translate}}
+
@@ -51,11 +55,16 @@
{{ 'register.mobile' | translate }}
-
-
- {{ checkMobile()|async }}
+
+
+ {{'tip.notnull' | translate:{value: 'register.mobile'|translate} }}
+
+
+ {{'tip.mobile_error'|translate}}
+
@@ -65,22 +74,23 @@
{{ 'register.email' | translate }}
+ [placeholder]="'tip.input' | translate:{value:'register.email'|translate}" formControlName="email"
+ [class.is-invalid]="getValue('email').invalid"/>
- {{'tip.notnull' | translate:{value:'register.email'|translate} }}
+ {{'tip.notnull' | translate:{value: 'register.email'|translate} }}
-
+
diff --git a/src/app/account/register/register.component.ts b/src/app/account/register/register.component.ts
index 093f66c..a3273c1 100644
--- a/src/app/account/register/register.component.ts
+++ b/src/app/account/register/register.component.ts
@@ -1,9 +1,8 @@
import {Component, OnInit} from '@angular/core';
-import {FormBuilder} from '@angular/forms';
+import {AbstractControl, FormBuilder, FormGroup, ValidatorFn, Validators} from '@angular/forms';
import {Commons} from '../../commons';
import {RegisterService} from './register.service';
-import {TranslateService} from '@ngx-translate/core';
-import {Observable, of} from 'rxjs';
+import {AppService} from '../../app.service';
@Component({
selector: 'app-register',
@@ -19,105 +18,58 @@ export class RegisterComponent extends Commons implements OnInit {
// 注册表单
registerForm = this.fb.group({
// 管理员名
- managerName: [],
+ managerName: this.fb.control('', [Validators.required]),
// 密码
- password: [],
+ password: this.fb.control('', [Validators.required]),
// 确认密码
- confirmPassword: [],
+ confirmPassword: this.fb.control('', [this.checkPwd()]),
// 手机号
- mobile: [],
+ mobile: this.fb.control('', [this.checkMobile()]),
// 邮箱
- email: [],
+ email: this.fb.control('', [Validators.required]),
// 邮箱类型
- emailType: this.fb.control('-1')
+ emailType: this.fb.control('', [Validators.required])
});
+
/**
* 邮箱类型
*/
- emailType$ = this.registerService.getEmailType();
+ emailType$ = this.appService.getEmailType();
- // 表单验证
- validForm = {
- confirmPassword: {
- flag: false
- },
- mobile: {
- flag: false
- }
- };
constructor(
private fb: FormBuilder,
private registerService: RegisterService,
- private translateService: TranslateService
+ private appService: AppService
) {
super();
}
- /**
- * 检查单个表单值
- * @param name formControlName
- */
- checkValue(name): boolean {
- if (!this.validForm.hasOwnProperty(name)) {
- this.validForm[name] = {};
- }
- this.validForm[name].flag = this.registerForm.value[name];
- return !this.validForm[name].flag;
- }
-
- /**
- * 检查表单所有值
- */
- checkForm(): boolean {
- for (const key in this.validForm) {
- if (!this.validForm[key].flag) {
- return true;
- }
- }
- return this.registerForm.value.emailType === '-1';
- }
-
- /**
- * 检查确认密码
- */
- checkPwd(): Observable
{
-
- if (!this.registerForm.value.confirmPassword) {
- this.validForm.confirmPassword.flag = false;
- return this.translateService.get('tip.password_null');
- } else if (this.registerForm.value.password !== this.registerForm.value.confirmPassword) {
- this.validForm.confirmPassword.flag = false;
- return this.translateService.get('tip.password_diff');
- } else {
- this.validForm.confirmPassword.flag = true;
- return of('');
- }
- }
-
/**
* 检查手机
*/
- checkMobile(): Observable {
- if (!this.registerForm.value.mobile) {
- this.validForm.mobile.flag = false;
- return this.translateService.get('tip.mobile_null');
- } else if (!/^1[3456789]\d{9}$/.test(this.registerForm.value.mobile)) {
- this.validForm.mobile.flag = false;
- return this.translateService.get('tip.mobile_error');
- } else {
- this.validForm.mobile.flag = true;
- return of('');
- }
+ checkMobile(): ValidatorFn {
+ return (control: AbstractControl): {} => {
+ if (/^1[3456789]\d{9}$/.test(control.value)) {
+ return null;
+ } else {
+ return {required: control.value === null || control.value.length === 0, mobile: true};
+ }
+ };
}
ngOnInit(): void {
-
+ this.validForm = {};
+ for (const key of Object.keys(this.registerForm.value)) {
+ this.validForm[key] = {
+ flag: false
+ };
+ }
}
- register() {
- this.registerService.register(this.registerForm.value);
+ form(): FormGroup {
+ return this.registerForm;
}
}
diff --git a/src/app/account/register/register.service.spec.ts b/src/app/account/register/register.service.spec.ts
index 2ba3960..fff1ac9 100644
--- a/src/app/account/register/register.service.spec.ts
+++ b/src/app/account/register/register.service.spec.ts
@@ -1,6 +1,6 @@
-import { TestBed } from '@angular/core/testing';
+import {TestBed} from '@angular/core/testing';
-import { RegisterService } from './register.service';
+import {RegisterService} from './register.service';
describe('RegisterService', () => {
let service: RegisterService;
diff --git a/src/app/account/register/register.service.ts b/src/app/account/register/register.service.ts
index cacafda..d1969b5 100644
--- a/src/app/account/register/register.service.ts
+++ b/src/app/account/register/register.service.ts
@@ -1,14 +1,14 @@
import {Injectable} from '@angular/core';
import {JSONRequest} from '../../interface/JSONRequest';
import {HttpClient} from '@angular/common/http';
-import {catchError, tap} from 'rxjs/operators';
+import {catchError} from 'rxjs/operators';
import {JSONResponse} from '../../interface/JSONResponse';
import {HttpInterface} from '../../interface/HttpInterface';
-import {Observable} from 'rxjs';
import {Result} from '../../interface/Result';
import {Router} from '@angular/router';
import {MessageService} from '../../message/message.service';
+
@Injectable({
providedIn: 'root'
})
@@ -20,9 +20,6 @@ export class RegisterService extends JSONRequest {
private messageService: MessageService
) {
super();
-
- this.httpError.result = Result.FAIL;
- this.httpError.message = '注册失败';
}
/**
@@ -32,35 +29,14 @@ export class RegisterService extends JSONRequest {
register(body) {
this.http.post>(HttpInterface.register, body, this.httpOptions)
.pipe(
- catchError(this.handleError('注册', this.httpError))
+ catchError(this.handleError('注册'))
).subscribe(r => {
- if (r.result === Result.OK) {
- this.messageService.info('注册成功');
- this.router.navigateByUrl('/login');
- } else {
- this.messageService.danger('注册失败');
- }
+ if (r.result === Result.OK) {
+ this.messageService.info('注册成功');
+ this.router.navigateByUrl('/login');
+ } else {
+ this.messageService.danger('注册失败');
+ }
});
}
-
-
- /**
- * 获取邮箱类型
- */
- getEmailType(): Observable {
- return this.http.get>(HttpInterface.getEmailType)
- .pipe(
- catchError(this.handleError('获取邮箱类型'))
- );
- }
-
- /**
- * 发送验证码
- */
- sendEmail(sender): Observable {
- return this.http.post>(HttpInterface.sendCode, {email: 'sender'})
- .pipe(
- catchError(this.handleError('发送邮件验证码'))
- );
- }
}
diff --git a/src/app/account/resetpwd/resetpwd.component.html b/src/app/account/resetpwd/resetpwd.component.html
index bc67666..e4d6e2b 100644
--- a/src/app/account/resetpwd/resetpwd.component.html
+++ b/src/app/account/resetpwd/resetpwd.component.html
@@ -2,44 +2,127 @@