diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 7e1f764..76ba4c8 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,61 +1,56 @@ -import { Component, Inject, Injector } 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 { MaskLayerService } from './mask-layer.service'; -import * as global from 'globals'; - +import { Component, Inject, Injector } 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 { MaskLayerService } from "./mask-layer.service"; +import * as global from "globals"; @Component({ - selector: 'app-root', - templateUrl: './app.component.html', - styleUrls: ['./app.component.scss'] + selector: "app-root", + templateUrl: "./app.component.html", + styleUrls: ["./app.component.scss"], }) export class AppComponent { - - constructor(private maskLayerService: MaskLayerService, private http: HttpClient, private router: Router, public token: CacheTokenService, private injector: Injector) { - global.injector = injector; + constructor( + private maskLayerService: MaskLayerService, + private http: HttpClient, + private router: Router, + public token: CacheTokenService, + private injector: Injector + ) { + global.injector = injector; + } + + isMaskLayerShow: boolean = false; + + ngOnInit(): void { + //监听遮罩层 + this.maskLayerService.getMessage().subscribe((message: any) => { + this.isMaskLayerShow = message; + }); + // console.log(1234,document.documentElement.clientWidth) + function addMeta(name, content) { + //手动添加mate标签 + + let meta = document.createElement("meta"); + + meta.content = content; + + meta.name = name; + + document.getElementsByTagName("head")[0].appendChild(meta); } - - isMaskLayerShow: boolean = false - - ngOnInit(): void { - - //监听遮罩层 - this.maskLayerService.getMessage().subscribe((message: any) => { - 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) - function addMeta(name, content) {//手动添加mate标签 - - let meta = document.createElement('meta'); - - meta.content = content; - - meta.name = name; - - document.getElementsByTagName('head')[0].appendChild(meta); - - } - 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') - } + 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" + ); } - - + if (sessionStorage.getItem("token") && !this.token.timer) { + //调用服务中的function刷新token + this.token.startUp(); + } + } } diff --git a/src/app/http-interceptors/cache-token.service.ts b/src/app/http-interceptors/cache-token.service.ts index 54f9a35..c37c72e 100644 --- a/src/app/http-interceptors/cache-token.service.ts +++ b/src/app/http-interceptors/cache-token.service.ts @@ -1,40 +1,34 @@ -import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http' +import { Injectable } from "@angular/core"; +import { HttpClient } from "@angular/common/http"; @Injectable({ - providedIn: 'root' + providedIn: "root", }) - export class CacheTokenService { - - constructor(private http:HttpClient) { } + constructor(private http: HttpClient) {} public timer; //刷新token令牌定时器 - startUp = ():void=>{ - window.clearInterval(this.timer) //清一遍定时器 - this.timer = window.setInterval(()=>{ + startUp = (): void => { + window.clearInterval(this.timer); //清一遍定时器 + this.timer = window.setInterval(() => { var token = sessionStorage.getItem("token"); var refreshToken = sessionStorage.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); - }) - },18*60*1000) - - } - - + 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); + }); + }, 5 * 60 * 1000); + }; //删除定时器 - delete = ():void=> { - window.clearInterval(this.timer) - } - - - + delete = (): void => { + window.clearInterval(this.timer); + }; }