|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
import { HttpClient } from '@angular/common/http'
|
|
|
|
import { Data } from '../../interface'
|
|
|
|
import { Router,ActivatedRoute } from '@angular/router'
|
|
|
|
import {CacheTokenService} from '../../http-interceptors/cache-token.service'//引入服务
|
|
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-login',
|
|
|
|
templateUrl: './login.component.html',
|
|
|
|
styleUrls: ['./login.component.scss'],
|
|
|
|
})
|
|
|
|
export class LoginComponent implements OnInit {
|
|
|
|
|
|
|
|
constructor(private http:HttpClient,private router:Router,private route:ActivatedRoute,public token:CacheTokenService,public snackBar: MatSnackBar) { }
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.automaticLogin()
|
|
|
|
}
|
|
|
|
|
|
|
|
errmsg :string = ''
|
|
|
|
|
|
|
|
onSubmit(e){
|
|
|
|
this.http.post('/api/Account/SignIn',{
|
|
|
|
name: e.name,
|
|
|
|
password: e.password}).subscribe( (data: Data) => {
|
|
|
|
sessionStorage.setItem("level",data.level);
|
|
|
|
sessionStorage.setItem("token",data.token);
|
|
|
|
sessionStorage.setItem("refreshToken",data.refreshToken);
|
|
|
|
this.http.get('/api/Account/NavMenus').subscribe((data:any)=>{
|
|
|
|
|
|
|
|
let isHave = data.find(item=>{ return item.url == "/statisticanalysis/home"})
|
|
|
|
let isHaveGis = data.find(item=>{ return item.url == "/gis"})
|
|
|
|
if (isHave) {
|
|
|
|
this.router.navigate([`/statisticanalysis/home`])
|
|
|
|
} else if(isHaveGis){
|
|
|
|
this.router.navigate([`/gis`])
|
|
|
|
} else if(data.length != 0){
|
|
|
|
data.forEach(item => {
|
|
|
|
if(item.url){
|
|
|
|
this.router.navigate([`/keyUnit`])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
this.snackBar.open('该用户角色未分配任何菜单', '确定', {
|
|
|
|
duration: 3000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
if(e.notlogin){ //7天免登录时
|
|
|
|
localStorage.setItem("isnologin","true")
|
|
|
|
localStorage.setItem("token",data.token)
|
|
|
|
localStorage.setItem("refreshToken",data.refreshToken) }
|
|
|
|
//调用服务中的function刷新token
|
|
|
|
this.token.startUp()
|
|
|
|
},
|
|
|
|
(err) => {this.errmsg = err}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
//7天免登录自动登录
|
|
|
|
automaticLogin () {
|
|
|
|
let isNoLogin = localStorage.getItem("isnologin")
|
|
|
|
if (isNoLogin) { //7天免登录时
|
|
|
|
let token = localStorage.getItem("token");
|
|
|
|
let refreshToken = localStorage.getItem("refreshToken");
|
|
|
|
this.http.post('/api/Account/RefreshToken', {
|
|
|
|
token: token,
|
|
|
|
refreshToken: refreshToken}).subscribe((data:any)=>{
|
|
|
|
sessionStorage.setItem("level",data.level);
|
|
|
|
sessionStorage.setItem("token",data.token);
|
|
|
|
sessionStorage.setItem("refreshToken",data.refreshToken);
|
|
|
|
this.token.startUp()
|
|
|
|
this.router.navigate(['/keyUnit'])
|
|
|
|
this.snackBar.open('已自动登录', '确定', {duration: 3000});
|
|
|
|
})
|
|
|
|
} //if
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//打开弹窗
|
|
|
|
open () {
|
|
|
|
this.snackBar.open('请联系管理员', '确定', {
|
|
|
|
duration: 3000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|