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