import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http' import { CookieService } from 'ngx-cookie-service'; @Injectable({ providedIn: 'root' }) export class CacheTokenService { constructor(private http: HttpClient, private cookieService: CookieService) { } public timer: number | undefined; //刷新token令牌定时器 startUp = (): void => { window.clearInterval(this.timer) this.timer = window.setInterval(() => { var token = this.cookieService.get("token"); var refreshToken = this.cookieService.get("refreshToken"); this.http.post('/api/CompanyAccount/RefreshToken', { token: token, refreshToken: refreshToken }).subscribe((data: any) => { sessionStorage.setItem("token", data.token); this.cookieService.set("token", data.token, undefined, '/'); this.cookieService.set("refreshToken", data.refreshToken, undefined, '/'); }) }, 18 * 60 * 1000) } //删除定时器 delete = (): void => { window.clearInterval(this.timer) } createTime = (time: string) => { var newtime = time.substr(0, 4) + '年' + time.substr(5, 2) + '月' + time.substr(8, 2) + '日' + time.substr(11, 8) } }