|
|
|
import {Component, OnInit} from '@angular/core';
|
|
|
|
// 路由
|
|
|
|
import {Router, RouterEvent} from '@angular/router';
|
|
|
|
// 国际化服务
|
|
|
|
import {TranslateService} from '@ngx-translate/core';
|
|
|
|
// 路由事件
|
|
|
|
// cookie操作
|
|
|
|
import {CookieService} from 'ngx-cookie-service';
|
|
|
|
// 环境变量
|
|
|
|
import {LoginService} from './account/login/login.service';
|
|
|
|
import {Result} from './interface/Result';
|
|
|
|
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, MessageInterface {
|
|
|
|
|
|
|
|
// 登陆状态
|
|
|
|
isLogin = false;
|
|
|
|
// 当前登陆账户
|
|
|
|
managerName = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param translate 国际化
|
|
|
|
* @param router 路由器
|
|
|
|
* @param cookieService cookie管理服务
|
|
|
|
* @param loginService 登陆服务
|
|
|
|
* @param messageUtil 弹窗信息管理
|
|
|
|
* @param abstractRoute 路由事件处理器
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
public translate: TranslateService,
|
|
|
|
private router: Router,
|
|
|
|
private cookieService: CookieService,
|
|
|
|
private loginService: LoginService,
|
|
|
|
private messageUtil: MessageUtil,
|
|
|
|
private abstractRoute: AbstractRoute
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 注销登录
|
|
|
|
*/
|
|
|
|
logout() {
|
|
|
|
this.loginService.logout({managerName: this.managerName}).subscribe(r => {
|
|
|
|
if (r.result === Result.OK) {
|
|
|
|
this.cookieService.deleteAll();
|
|
|
|
this.router.navigateByUrl('/login');
|
|
|
|
this.messageUtil.info(this.prefix(r.message));
|
|
|
|
} else {
|
|
|
|
this.messageUtil.danger(this.prefix(r.message));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
|
|
|
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 = this.translate.getBrowserCultureLang();
|
|
|
|
// 当在assets/i18n中找不到对应的语言翻译时,使用'zh-CN'作为默认语言
|
|
|
|
this.translate.setDefaultLang('zh-CN');
|
|
|
|
this.translate.use(currentLanguage);
|
|
|
|
// 记录当前设置的语言
|
|
|
|
localStorage.setItem('currentLanguage', currentLanguage);
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix(key: string): string {
|
|
|
|
return 'server.logout.' + key;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|