完善账号接口逻辑

change
panqihua 5 years ago
parent a50c014f6d
commit 93c75399a4
  1. 39
      src/app/AbstractRoute.ts
  2. 18
      src/app/RouteInterface.ts
  3. 32
      src/app/account/login/login.component.ts
  4. 10
      src/app/account/login/login.service.ts
  5. 16
      src/app/account/register/register.component.ts
  6. 9
      src/app/account/register/register.service.ts
  7. 4
      src/app/account/resetpwd/resetpwd.component.html
  8. 36
      src/app/account/resetpwd/resetpwd.component.ts
  9. 22
      src/app/account/resetpwd/resetpwd.service.ts
  10. 54
      src/app/app.component.ts
  11. 6
      src/app/app.service.ts
  12. 7
      src/app/interface/EmailType.ts
  13. 42
      src/app/interface/Response.ts
  14. 10
      src/app/interface/Token.ts
  15. 36
      src/app/message/message.service.ts
  16. 35
      src/assets/i18n/en-US.json
  17. 32
      src/assets/i18n/zh-CN.json

@ -0,0 +1,39 @@
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {NavigationStart, Router} from '@angular/router';
import {filter} from 'rxjs/operators';
import {environment} from '../environments/environment';
import {CookieService} from 'ngx-cookie-service';
import {RouteInterface} from './RouteInterface';
/**
*
*/
@Injectable({
providedIn: 'root'
})
export class AbstractRoute {
// 登陆状态
isLogin = false;
// 当前登陆账户
managerName = null;
navStart: Observable<NavigationStart>;
constructor(
private router: Router,
private cookieService: CookieService
) {
this.navStart = router.events.pipe(
filter(evt => evt instanceof NavigationStart)
) as Observable<NavigationStart>;
}
checkUser(routeInterface: RouteInterface): void {
this.isLogin = this.cookieService.check(environment.tokenKey);
this.managerName = this.cookieService.get(environment.managerKey);
this.navStart.subscribe(evt => routeInterface.doNavigationStart(evt, this.isLogin, this.managerName));
}
}

@ -0,0 +1,18 @@
import {RouterEvent} from '@angular/router';
import {Injectable} from '@angular/core';
/**
*
*/
export interface RouteInterface {
doNavigationStart(evt: RouterEvent, isLogin: boolean, managerName: string);
}
@Injectable({
providedIn: 'root'
})
export class RouteInterfaceImpl implements RouteInterface {
doNavigationStart(evt: RouterEvent, isLogin: boolean, managerName: string) {
console.debug(evt.url);
}
}

@ -6,7 +6,7 @@ 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';
import {MessageInterface, MessageUtil} from '../../message/message.service';
@Component({
selector: 'app-login',
@ -14,7 +14,7 @@ import {MessageService} from '../../message/message.service';
styleUrls: ['./login.component.scss']
})
// 登陆模块
export class LoginComponent extends Commons implements OnInit {
export class LoginComponent extends Commons implements OnInit, MessageInterface {
// 登陆表单
loginForm = this.fb.group({
// 管理员名
@ -28,7 +28,7 @@ export class LoginComponent extends Commons implements OnInit {
private router: Router,
private loginService: LoginService,
private cookieService: CookieService,
private messageService: MessageService
private messageUtil: MessageUtil
) {
super();
@ -44,17 +44,17 @@ export class LoginComponent extends Commons implements OnInit {
// 发送登陆请求
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('登陆失败');
}
});
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));
}
});
}
@ -62,4 +62,8 @@ export class LoginComponent extends Commons implements OnInit {
return this.loginForm;
}
prefix(key: string): string {
return 'server.login.' + key;
}
}

@ -2,9 +2,9 @@ import {Injectable} from '@angular/core';
import {JSONRequest} from '../../interface/JSONRequest';
import {HttpClient} from '@angular/common/http';
import {HttpInterface} from '../../interface/HttpInterface';
import {JSONResponse} from '../../interface/JSONResponse';
import {catchError} from 'rxjs/operators';
import {Observable} from 'rxjs';
import {LoginResponse, LogoutResponse} from '../../interface/Response';
@Injectable({
providedIn: 'root'
@ -20,8 +20,8 @@ export class LoginService extends JSONRequest {
/**
*
*/
logout(): Observable<JSONResponse<any>> {
return this.http.post<JSONResponse<any>>(HttpInterface.logout, {}, this.httpOptions)
logout(body): Observable<LogoutResponse> {
return this.http.post<LogoutResponse>(HttpInterface.logout, body, this.httpOptions)
.pipe(
catchError(this.handleError<any>('注销'))
);
@ -30,8 +30,8 @@ export class LoginService extends JSONRequest {
/**
*
*/
login(body): Observable<JSONResponse<any>> {
return this.http.post<JSONResponse<any>>(HttpInterface.login, body, this.httpOptions)
login(body): Observable<LoginResponse> {
return this.http.post<LoginResponse>(HttpInterface.login, body, this.httpOptions)
.pipe(
catchError(this.handleError<any>('登陆'))
);

@ -5,7 +5,7 @@ import {RegisterService} from './register.service';
import {AppService} from '../../app.service';
import {Result} from '../../interface/Result';
import {Router} from '@angular/router';
import {MessageService} from '../../message/message.service';
import {MessageInterface, MessageUtil} from '../../message/message.service';
@Component({
selector: 'app-register',
@ -15,8 +15,7 @@ import {MessageService} from '../../message/message.service';
// 注册模块
export class RegisterComponent extends Commons implements OnInit {
export class RegisterComponent extends Commons implements OnInit, MessageInterface {
// 注册表单
registerForm = this.fb.group({
@ -40,13 +39,12 @@ export class RegisterComponent extends Commons implements OnInit {
*/
emailType$ = this.appService.getEmailType();
constructor(
private fb: FormBuilder,
private registerService: RegisterService,
private appService: AppService,
private router: Router,
private messageService: MessageService
private messageUtil: MessageUtil
) {
super();
}
@ -70,10 +68,10 @@ export class RegisterComponent extends Commons implements OnInit {
register() {
this.registerService.register(this.registerForm.value).subscribe(r => {
if (r.result === Result.OK) {
this.messageService.info('注册成功');
this.messageUtil.info(this.prefix(r.message));
this.router.navigateByUrl('/login');
} else {
this.messageService.danger('注册失败');
this.messageUtil.danger(this.prefix(r.message));
}
});
}
@ -90,5 +88,9 @@ export class RegisterComponent extends Commons implements OnInit {
form(): FormGroup {
return this.registerForm;
}
prefix(key: string): string {
return 'server.register.' + key;
}
}

@ -2,12 +2,9 @@ import {Injectable} from '@angular/core';
import {JSONRequest} from '../../interface/JSONRequest';
import {HttpClient} from '@angular/common/http';
import {catchError} from 'rxjs/operators';
import {JSONResponse} from '../../interface/JSONResponse';
import {HttpInterface} from '../../interface/HttpInterface';
import {Result} from '../../interface/Result';
import {Router} from '@angular/router';
import {MessageService} from '../../message/message.service';
import {Observable} from 'rxjs';
import {RegisterResponse} from '../../interface/Response';
@Injectable({
@ -25,8 +22,8 @@ export class RegisterService extends JSONRequest {
*
* @param body
*/
register(body): Observable<JSONResponse<any>> {
return this.http.post<JSONResponse<any>>(HttpInterface.register, body, this.httpOptions)
register(body): Observable<RegisterResponse> {
return this.http.post<RegisterResponse>(HttpInterface.register, body, this.httpOptions)
.pipe(
catchError(this.handleError<any>('注册'))
);

@ -15,7 +15,7 @@
</div>
<!-- 邮箱 -->
<div class="input-group mb-3 mx-auto col-8" *ngIf="step!==3">
<div class="input-group mb-3 mx-auto col-8" [class.col-8]="step===1" [class.col-9]="step===2" *ngIf="step!==3">
<div class="input-group-prepend">
<span class="input-group-text">{{ 'register.email' | translate }}</span>
</div>
@ -24,7 +24,7 @@
[class.is-invalid]="getValue('email').invalid"/>
<select class="custom-select" formControlName="emailType">
<option value="">{{'tip.select'|translate:{value: 'register.email_type'|translate} }}</option>
<option *ngFor="let item of (emailType$|async)?.body" [value]="item.suffix">{{item.name}}</option>
<option *ngFor="let item of (emailType$|async)?.emailTypeList" [value]="item.suffix">{{item.name}}</option>
</select>

@ -4,7 +4,7 @@ import {Commons} from '../../commons';
import {AppService} from '../../app.service';
import {ResetpwdService} from './resetpwd.service';
import {Result} from '../../interface/Result';
import {MessageService} from '../../message/message.service';
import {MessageInterface, MessageUtil} from '../../message/message.service';
import {Router} from '@angular/router';
@Component({
@ -13,7 +13,7 @@ import {Router} from '@angular/router';
styleUrls: ['./resetpwd.component.scss']
})
// 重置密码模块
export class ResetpwdComponent extends Commons implements OnInit {
export class ResetpwdComponent extends Commons implements OnInit, MessageInterface {
// 重置密码表单
resetForm = this.fb.group({
@ -43,7 +43,7 @@ export class ResetpwdComponent extends Commons implements OnInit {
private fb: FormBuilder,
private appService: AppService,
private resetpwdService: ResetpwdService,
private messageService: MessageService,
private messageUtil: MessageUtil,
private router: Router
) {
super();
@ -59,15 +59,13 @@ export class ResetpwdComponent extends Commons implements OnInit {
checkEmail() {
this.resetpwdService.checkEmail(this.resetForm.value).subscribe(r => {
if (r.result === Result.OK) {
// this.messageService.info('邮箱正确');
this.messageUtil.info(this.prefix(r.message));
// 进入第二步
this.step = 2;
// 清空提示
this.messageService.message = null;
// 第二步不允许编辑账户和邮箱
this.disabled();
} else {
this.messageService.danger('账号或邮箱错误');
this.messageUtil.danger(this.prefix(r.message));
}
});
}
@ -88,13 +86,13 @@ export class ResetpwdComponent extends Commons implements OnInit {
// 发送验证码
sendCode() {
this.resetpwdService.sendCode({
managerName: this.getValue('managerName').value
email: this.getValue('email').value,
emailType: this.getValue('emailType').value
}).subscribe(res => {
this.disabled();
if (res.result === Result.OK) {
this.messageService.info('验证码发送成功,请查收验证码');
this.messageUtil.info(this.prefix(res.message));
} else {
this.messageService.danger('验证码发送失败,请重试或联系管理员');
this.messageUtil.danger(this.prefix(res.message));
}
});
}
@ -111,15 +109,16 @@ export class ResetpwdComponent extends Commons implements OnInit {
*/
checkCode() {
this.resetpwdService.checkCode({
managerName: this.getValue('managerName').value
email: this.getValue('email').value,
emailType: this.getValue('emailType').value,
verificationCode: this.getValue('verificationCode').value
}).subscribe(res => {
if (res.result === Result.OK) {
this.messageUtil.info(this.prefix(res.message));
// 进入第三步
this.step = 3;
// 清空提示
this.messageService.message = null;
} else {
this.messageService.danger('验证码不正确,请重新输入');
this.messageUtil.danger(this.prefix(res.message));
}
});
}
@ -133,10 +132,10 @@ export class ResetpwdComponent extends Commons implements OnInit {
password: this.getValue('password').value
}).subscribe(res => {
if (res.result === Result.OK) {
this.messageService.info('重制密码成功');
this.messageUtil.info(this.prefix(res.message));
this.router.navigateByUrl('/login');
} else {
this.messageService.danger('重置密码失败');
this.messageUtil.danger(this.prefix(res.message));
}
});
}
@ -152,4 +151,7 @@ export class ResetpwdComponent extends Commons implements OnInit {
return this.getValue('managerName').invalid || this.getValue('email').invalid || this.getValue('emailType').invalid;
}
prefix(key: string): string {
return 'server.resetpwd.' + key;
}
}

@ -1,11 +1,10 @@
import {Injectable} from '@angular/core';
import {JSONRequest} from '../../interface/JSONRequest';
import {Router} from '@angular/router';
import {HttpClient} from '@angular/common/http';
import {HttpInterface} from '../../interface/HttpInterface';
import {catchError} from 'rxjs/operators';
import {JSONResponse} from '../../interface/JSONResponse';
import {Observable} from 'rxjs';
import {ResetpwdResponse} from '../../interface/Response';
@Injectable({
providedIn: 'root'
@ -16,8 +15,7 @@ import {Observable} from 'rxjs';
export class ResetpwdService extends JSONRequest {
constructor(
private http: HttpClient,
private router: Router
private http: HttpClient
) {
super();
}
@ -25,8 +23,8 @@ export class ResetpwdService extends JSONRequest {
/**
*
*/
checkEmail(body): Observable<JSONResponse<any>> {
return this.http.post<JSONResponse<any>>(HttpInterface.checkEmail, body, this.httpOptions)
checkEmail(body): Observable<ResetpwdResponse> {
return this.http.post<ResetpwdResponse>(HttpInterface.checkEmail, body, this.httpOptions)
.pipe(
catchError(this.handleError<any>('检查邮箱'))
);
@ -35,8 +33,8 @@ export class ResetpwdService extends JSONRequest {
/**
*
*/
sendCode(body): Observable<JSONResponse<any>> {
return this.http.post<JSONResponse<any>>(HttpInterface.sendCode, body, this.httpOptions)
sendCode(body): Observable<ResetpwdResponse> {
return this.http.post<ResetpwdResponse>(HttpInterface.sendCode, body, this.httpOptions)
.pipe(
catchError(this.handleError<any>('发送验证码'))
);
@ -46,8 +44,8 @@ export class ResetpwdService extends JSONRequest {
*
* @param body
*/
checkCode(body): Observable<JSONResponse<any>> {
return this.http.post<JSONResponse<any>>(HttpInterface.checkCode, body, this.httpOptions)
checkCode(body): Observable<ResetpwdResponse> {
return this.http.post<ResetpwdResponse>(HttpInterface.checkCode, body, this.httpOptions)
.pipe(
catchError(this.handleError<any>('检查验证码'))
);
@ -56,8 +54,8 @@ export class ResetpwdService extends JSONRequest {
/**
*
*/
resetPwd(body): Observable<JSONResponse<any>> {
return this.http.post<JSONResponse<any>>(HttpInterface.resetPwd, body, this.httpOptions)
resetPwd(body): Observable<ResetpwdResponse> {
return this.http.post<ResetpwdResponse>(HttpInterface.resetPwd, body, this.httpOptions)
.pipe(
catchError(this.handleError<any>('重置密码'))
);

@ -1,85 +1,78 @@
import {Component, OnInit} from '@angular/core';
// 路由
import {NavigationStart, Router} from '@angular/router';
import {Router, RouterEvent} from '@angular/router';
// 国际化服务
import {TranslateService} from '@ngx-translate/core';
// 路由事件
import {Observable} from 'rxjs';
import {filter} from 'rxjs/operators';
// cookie操作
import {CookieService} from 'ngx-cookie-service';
// 环境变量
import {environment} from '../environments/environment';
import {LoginService} from './account/login/login.service';
import {Result} from './interface/Result';
import {MessageService} from './message/message.service';
import {MessageInterface, MessageUtil} from './message/message.service';
import {AbstractRoute} from './AbstractRoute';
import {RouteInterface} from './RouteInterface';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
export class AppComponent implements OnInit, MessageInterface {
// 登陆状态
isLogin = false;
// 当前登陆账户
managerName = null;
navStart: Observable<NavigationStart>;
/**
*
* @param translate
* @param router
* @param cookieService cookie管理服务
* @param loginService
* @param messageService
* @param messageUtil
* @param abstractRoute
*/
constructor(
public translate: TranslateService,
private router: Router,
private cookieService: CookieService,
private loginService: LoginService,
private messageService: MessageService,
private messageUtil: MessageUtil,
private abstractRoute: AbstractRoute
) {
this.navStart = router.events.pipe(
filter(evt => evt instanceof NavigationStart)
) as Observable<NavigationStart>;
}
/**
*
*/
logout() {
this.loginService.logout().subscribe(r => {
this.loginService.logout({managerName: this.managerName}).subscribe(r => {
if (r.result === Result.OK) {
this.cookieService.deleteAll();
this.messageService.info('注销成功');
this.router.navigateByUrl('/login');
location.href = '/login';
this.messageUtil.info(this.prefix(r.message));
} else {
alert('注销失败');
this.messageUtil.danger(this.prefix(r.message));
}
});
}
async ngOnInit() {
this.navStart.subscribe(evt => {
ngOnInit() {
this.isLogin = this.cookieService.check(environment.tokenKey);
this.managerName = this.cookieService.get(environment.managerKey);
if (evt.url !== '/forum' && this.isLogin) {
this.router.navigateByUrl('/forum');
} else if (evt.url !== '/login' && !this.isLogin) {
this.router.navigateByUrl('/login');
} else {
console.debug('当前路由' + evt.url + '不需要重定向');
const that = this;
// tslint:disable-next-line:new-parens
this.abstractRoute.checkUser(new class implements RouteInterface {
doNavigationStart(evt: RouterEvent, isLogin: boolean, managerName: string) {
that.isLogin = isLogin;
that.managerName = managerName;
}
});
// 语言初始化(若未设置语言, 则取浏览器语言)
const currentLanguage = await localStorage.getItem('currentLanguage') || this.translate.getBrowserCultureLang();
const currentLanguage = this.translate.getBrowserCultureLang();
// 当在assets/i18n中找不到对应的语言翻译时,使用'zh-CN'作为默认语言
this.translate.setDefaultLang('zh-CN');
this.translate.use(currentLanguage);
@ -87,4 +80,9 @@ export class AppComponent implements OnInit {
localStorage.setItem('currentLanguage', currentLanguage);
}
prefix(key: string): string {
return 'server.logout.' + key;
}
}

@ -1,10 +1,10 @@
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {JSONResponse} from './interface/JSONResponse';
import {HttpInterface} from './interface/HttpInterface';
import {catchError} from 'rxjs/operators';
import {JSONRequest} from './interface/JSONRequest';
import {HttpClient} from '@angular/common/http';
import {EmailTypeResponse} from './interface/Response';
@Injectable({
providedIn: 'root'
@ -20,8 +20,8 @@ export class AppService extends JSONRequest {
/**
*
*/
getEmailType(): Observable<JSONResponse<any>> {
return this.http.get<JSONResponse<any>>(HttpInterface.getEmailType)
getEmailType(): Observable<EmailTypeResponse> {
return this.http.get<EmailTypeResponse>(HttpInterface.getEmailType)
.pipe(
catchError(this.handleError<any>('获取邮箱类型'))
);

@ -0,0 +1,7 @@
// 邮箱类型
export class EmailType {
// 邮箱标志
name: string;
// 邮箱后缀
suffix: string;
}

@ -0,0 +1,42 @@
import {Result} from './Result';
import {Token} from './Token';
import {EmailType} from './EmailType';
export interface Response {
// 响应结果
result: Result;
// 响应详细结果
message: string;
}
/**
*
*/
// tslint:disable-next-line:no-empty-interface
export interface LogoutResponse extends Response {
}
/**
*
*/
export interface LoginResponse extends Response {
managerToken: Token;
}
// 注册响应
// tslint:disable-next-line:no-empty-interface
export interface RegisterResponse extends Response {
}
// 邮箱类型响应
export interface EmailTypeResponse extends Response {
emailTypeList: Array<EmailType>;
}
// 重置密码响应
// tslint:disable-next-line:no-empty-interface
export interface ResetpwdResponse extends Response {
}

@ -0,0 +1,10 @@
export interface Token {
// 令牌
token: string;
// 创建时间
createTime: number;
// 有效时间
useTime: number;
// 管理员名
managerName: string;
}

@ -1,10 +1,9 @@
import {Injectable} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
@Injectable({
providedIn: 'root'
})
export class MessageService {
// 提示框信息
@ -47,3 +46,36 @@ export class MessageService {
}
@Injectable({
providedIn: 'root'
})
// 获取国际化翻译
export class MessageUtil {
constructor(private translate: TranslateService,
private messageService: MessageService) {
}
// tslint:disable-next-line:ban-types
info(key: string | Array<string>, interpolateParams?: Object) {
this.translate.get(key, interpolateParams).subscribe(s => {
this.messageService.info(s);
});
}
// tslint:disable-next-line:ban-types
danger(key: string | Array<string>, interpolateParams?: Object) {
this.translate.get(key, interpolateParams).subscribe(s => {
this.messageService.danger(s);
});
}
reset() {
this.messageService.message = null;
}
}
// 国际化前缀
export interface MessageInterface {
prefix(key: string): string;
}

@ -31,7 +31,8 @@
"complaint": "time:{{time}}",
"plaintiff": "plaintiff",
"defendant": "defendant",
"load_notices": "Loading advertising information"
"load_notices": "Loading advertising information",
"load_posts": "Loading posts"
},
"tip":{
"input": "please input {{value}}",
@ -54,5 +55,37 @@
"next_page": "Next",
"prev_page": "Previous",
"check_code": "check_code"
},
"server": {
"login": {
"manager": "管理员不能为空!",
"password": "密码不能为空!",
"fail": "账号或密码错误!",
"ok": "登陆成功"
},
"register": {
"manager": "管理员不能为空",
"password": "密码不能为空",
"confirmPassword": "确认密码不能为空",
"passwordNotMatch": "两次密码输入不一致",
"mobie": "手机号不能为空",
"email": "邮箱不能为空",
"username": "用户已存在",
"ok": "注册成功"
},
"logout": {
"ok": "注销成功",
"fail": "注销失败"
},
"resetpwd": {
"check_ok": "校验成功",
"check_fail": "账号或邮箱错误",
"send_code_ok": "验证码发送成功,请查收验证码",
"send_code_fail": "验证码发送失败,请重试或联系管理员",
"valid_code_ok": "校验成功",
"valid_code_fail": "验证码不正确,请重新输入",
"resetpwd_ok": "重置密码成功",
"resetpwd_fail": "重置密码失败"
}
}
}

@ -55,5 +55,37 @@
"next_page": "下一页",
"prev_page": "上一页",
"check_code": "检查验证码"
},
"server": {
"login": {
"manager": "管理员不能为空!",
"password": "密码不能为空!",
"fail": "账号或密码错误!",
"ok": "登陆成功"
},
"register": {
"manager": "管理员不能为空",
"password": "密码不能为空",
"confirmPassword": "确认密码不能为空",
"passwordNotMatch": "两次密码输入不一致",
"mobie": "手机号不能为空",
"email": "邮箱不能为空",
"username": "用户已存在",
"ok": "注册成功"
},
"logout": {
"ok": "注销成功",
"fail": "注销失败"
},
"resetpwd": {
"check_ok": "校验成功",
"check_fail": "账号或邮箱错误",
"send_code_ok": "验证码发送成功,请查收验证码",
"send_code_fail": "验证码发送失败,请重试或联系管理员",
"valid_code_ok": "校验成功",
"valid_code_fail": "验证码不正确,请重新输入",
"resetpwd_ok": "重置密码成功",
"resetpwd_fail": "重置密码失败"
}
}
}

Loading…
Cancel
Save