2 changed files with 70 additions and 81 deletions
@ -1,61 +1,56 @@ |
|||||||
import { Component, Inject, Injector } from '@angular/core'; |
import { Component, Inject, Injector } from "@angular/core"; |
||||||
import { HttpClient } from '@angular/common/http' |
import { HttpClient } from "@angular/common/http"; |
||||||
import { Data } from './interface' |
import { Data } from "./interface"; |
||||||
import { Router, ActivatedRoute } from '@angular/router' |
import { Router, ActivatedRoute } from "@angular/router"; |
||||||
import { CacheTokenService } from './http-interceptors/cache-token.service'//引入服务
|
import { CacheTokenService } from "./http-interceptors/cache-token.service"; //引入服务
|
||||||
import { MaskLayerService } from './mask-layer.service'; |
import { MaskLayerService } from "./mask-layer.service"; |
||||||
import * as global from 'globals'; |
import * as global from "globals"; |
||||||
|
|
||||||
|
|
||||||
@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 { |
export class AppComponent { |
||||||
|
constructor( |
||||||
constructor(private maskLayerService: MaskLayerService, private http: HttpClient, private router: Router, public token: CacheTokenService, private injector: Injector) { |
private maskLayerService: MaskLayerService, |
||||||
|
private http: HttpClient, |
||||||
|
private router: Router, |
||||||
|
public token: CacheTokenService, |
||||||
|
private injector: Injector |
||||||
|
) { |
||||||
global.injector = injector; |
global.injector = injector; |
||||||
} |
} |
||||||
|
|
||||||
isMaskLayerShow: boolean = false |
isMaskLayerShow: boolean = false; |
||||||
|
|
||||||
ngOnInit(): void { |
ngOnInit(): void { |
||||||
|
|
||||||
//监听遮罩层
|
//监听遮罩层
|
||||||
this.maskLayerService.getMessage().subscribe((message: any) => { |
this.maskLayerService.getMessage().subscribe((message: any) => { |
||||||
this.isMaskLayerShow = message |
this.isMaskLayerShow = message; |
||||||
}); |
}); |
||||||
var token = sessionStorage.getItem("token"); |
|
||||||
var refreshToken = sessionStorage.getItem("refreshToken"); |
|
||||||
if (token && refreshToken) { |
|
||||||
this.http.post('/api/Account/RefreshToken', { |
|
||||||
token: token, |
|
||||||
refreshToken: refreshToken |
|
||||||
}).subscribe((data: Data) => { |
|
||||||
sessionStorage.setItem("level", data.level); |
|
||||||
sessionStorage.setItem("token", data.token); |
|
||||||
sessionStorage.setItem("refreshToken", data.refreshToken); |
|
||||||
this.token.startUp() |
|
||||||
}) |
|
||||||
} |
|
||||||
// console.log(1234,document.documentElement.clientWidth)
|
// console.log(1234,document.documentElement.clientWidth)
|
||||||
function addMeta(name, content) {//手动添加mate标签
|
function addMeta(name, content) { |
||||||
|
//手动添加mate标签
|
||||||
|
|
||||||
let meta = document.createElement('meta'); |
let meta = document.createElement("meta"); |
||||||
|
|
||||||
meta.content = content; |
meta.content = content; |
||||||
|
|
||||||
meta.name = name; |
meta.name = name; |
||||||
|
|
||||||
document.getElementsByTagName('head')[0].appendChild(meta); |
document.getElementsByTagName("head")[0].appendChild(meta); |
||||||
|
|
||||||
} |
} |
||||||
if (document.documentElement.clientWidth < 800) { |
if (document.documentElement.clientWidth < 800) { |
||||||
addMeta('viewport', 'initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no,uc-fitscreen=yes,viewport-fit=cover') |
addMeta( |
||||||
|
"viewport", |
||||||
|
"initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no,uc-fitscreen=yes,viewport-fit=cover" |
||||||
|
); |
||||||
} |
} |
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (sessionStorage.getItem("token") && !this.token.timer) { |
||||||
|
//调用服务中的function刷新token
|
||||||
|
this.token.startUp(); |
||||||
|
} |
||||||
|
} |
||||||
} |
} |
||||||
|
@ -1,40 +1,34 @@ |
|||||||
import { Injectable } from '@angular/core'; |
import { Injectable } from "@angular/core"; |
||||||
import { HttpClient } from '@angular/common/http' |
import { HttpClient } from "@angular/common/http"; |
||||||
|
|
||||||
@Injectable({ |
@Injectable({ |
||||||
providedIn: 'root' |
providedIn: "root", |
||||||
}) |
}) |
||||||
|
|
||||||
export class CacheTokenService { |
export class CacheTokenService { |
||||||
|
constructor(private http: HttpClient) {} |
||||||
constructor(private http:HttpClient) { } |
|
||||||
public timer; |
public timer; |
||||||
|
|
||||||
//刷新token令牌定时器
|
//刷新token令牌定时器
|
||||||
startUp = ():void=>{ |
startUp = (): void => { |
||||||
window.clearInterval(this.timer) //清一遍定时器
|
window.clearInterval(this.timer); //清一遍定时器
|
||||||
this.timer = window.setInterval(()=>{ |
this.timer = window.setInterval(() => { |
||||||
var token = sessionStorage.getItem("token"); |
var token = sessionStorage.getItem("token"); |
||||||
var refreshToken = sessionStorage.getItem("refreshToken"); |
var refreshToken = sessionStorage.getItem("refreshToken"); |
||||||
this.http.post('/api/Account/RefreshToken', { |
this.http |
||||||
|
.post("/api/Account/RefreshToken", { |
||||||
token: token, |
token: token, |
||||||
refreshToken: refreshToken |
refreshToken: refreshToken, |
||||||
}).subscribe((data:any) => { |
|
||||||
sessionStorage.setItem("level",data.level); |
|
||||||
sessionStorage.setItem("token",data.token); |
|
||||||
sessionStorage.setItem("refreshToken",data.refreshToken); |
|
||||||
}) |
}) |
||||||
},18*60*1000) |
.subscribe((data: any) => { |
||||||
|
sessionStorage.setItem("level", data.level); |
||||||
} |
sessionStorage.setItem("token", data.token); |
||||||
|
sessionStorage.setItem("refreshToken", data.refreshToken); |
||||||
|
}); |
||||||
|
}, 5 * 60 * 1000); |
||||||
|
}; |
||||||
|
|
||||||
//删除定时器
|
//删除定时器
|
||||||
delete = ():void=> { |
delete = (): void => { |
||||||
window.clearInterval(this.timer) |
window.clearInterval(this.timer); |
||||||
} |
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue