From 4617a1912110436abd42ec0722541ca2a7d54d09 Mon Sep 17 00:00:00 2001 From: qq55341701 <55341701@qq.com> Date: Fri, 18 Mar 2022 17:41:00 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=96=B0=E5=A2=9E]=E6=91=84=E5=83=8F=E5=A4=B4?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E5=90=8E=E7=AB=AF;=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E5=88=B7=E6=96=B0token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- angular.json | 4 ++-- src/app/app.module.ts | 3 +-- src/app/auth.guard.ts | 7 +++---- src/app/http-interceptors/base-interceptor.ts | 8 ++----- src/app/pages/login/login.component.ts | 12 ++++------- src/app/service/cache-token.service.ts | 12 +++++------ .../host-config/host-config.component.ts | 21 ++++++++++++++++--- .../navigation/navigation.component.ts | 4 +++- 8 files changed, 38 insertions(+), 33 deletions(-) diff --git a/angular.json b/angular.json index 778e1bd..a26ea44 100644 --- a/angular.json +++ b/angular.json @@ -46,8 +46,8 @@ "budgets": [ { "type": "initial", - "maximumWarning": "500kb", - "maximumError": "1mb" + "maximumWarning": "5mb", + "maximumError": "5mb" }, { "type": "anyComponentStyle", diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 7fe27bd..fb8575f 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -8,7 +8,6 @@ import { FormsModule } from '@angular/forms'; import { HttpClientModule } from '@angular/common/http'; import { httpInterceptorProviders } from './http-interceptors/index' import { CacheTokenService } from './service/cache-token.service' -import { CookieService } from 'ngx-cookie-service';//cookie插件 import { NzNotificationModule } from 'ng-zorro-antd/notification'; import { NzMessageModule } from 'ng-zorro-antd/message'; import { TreeService } from './service/tree.service'; @@ -28,7 +27,7 @@ import { CustomReuseStrategy } from './CustomReuseStrategy'; NzNotificationModule, NzMessageModule ], - providers: [httpInterceptorProviders, CacheTokenService, TreeService, CookieService, + providers: [httpInterceptorProviders, CacheTokenService, TreeService, { provide: RouteReuseStrategy, useClass: CustomReuseStrategy }], bootstrap: [AppComponent] }) diff --git a/src/app/auth.guard.ts b/src/app/auth.guard.ts index a906efa..10df7d8 100644 --- a/src/app/auth.guard.ts +++ b/src/app/auth.guard.ts @@ -1,14 +1,13 @@ import { Component, OnInit, Inject } from '@angular/core'; import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router'; -import { CookieService } from 'ngx-cookie-service'; @Injectable({ - providedIn: 'root' + providedIn: 'root' }) export class AuthGuard implements CanActivate { - constructor(private router: Router,private cookieService: CookieService) { + constructor(private router: Router) { } @@ -24,7 +23,7 @@ export class AuthGuard implements CanActivate { checkLogin(): boolean { // console.log('xxxxxxxxxxxx') // 判断本地有没有token - const token = this.cookieService.get("token") || sessionStorage.getItem('token'); + const token = sessionStorage.getItem('token'); // 如果有token,允许访问 if (token) { return true; } diff --git a/src/app/http-interceptors/base-interceptor.ts b/src/app/http-interceptors/base-interceptor.ts index 7229f32..37d33a3 100644 --- a/src/app/http-interceptors/base-interceptor.ts +++ b/src/app/http-interceptors/base-interceptor.ts @@ -7,7 +7,6 @@ import { throwError } from 'rxjs' import { catchError } from 'rxjs/operators'; import { Router } from '@angular/router' import { CacheTokenService } from '../service/cache-token.service' -import { CookieService } from 'ngx-cookie-service'; import { NzMessageService } from 'ng-zorro-antd/message'; //baseurl // const baseurl = 'http://39.106.78.171:8008'; @@ -15,7 +14,7 @@ import { NzMessageService } from 'ng-zorro-antd/message'; @Injectable() export class BaseInterceptor implements HttpInterceptor { - constructor(private router: Router, public token: CacheTokenService, private cookieService: CookieService, private message: NzMessageService) { } + constructor(private router: Router, public token: CacheTokenService, private message: NzMessageService) { } intercept(req: any, next: HttpHandler) { @@ -33,7 +32,7 @@ export class BaseInterceptor implements HttpInterceptor { }); if (!req.cancelToken) { /*获取token*/ - let token = this.cookieService.get("token") + let token = sessionStorage.getItem('token') /*此处设置额外请求头,token令牌*/ newReq.headers = newReq.headers.set('Authorization', `Bearer ${token}`) @@ -56,10 +55,7 @@ export class BaseInterceptor implements HttpInterceptor { if (error.status === 401 || error.status === 614) { this.token.delete() sessionStorage.clear() - // window.localStorage.clear() localStorage.removeItem("isautologin") - this.cookieService.set("token", '', new Date(new Date().getTime() + 1), '/') - this.cookieService.set("refreshToken", '', new Date(new Date().getTime() + 1), '/') this.message.create('error', `用户认证信息过期,请重新登录!`); this.router.navigate(['/login']) } diff --git a/src/app/pages/login/login.component.ts b/src/app/pages/login/login.component.ts index e493d0e..2d77fd6 100644 --- a/src/app/pages/login/login.component.ts +++ b/src/app/pages/login/login.component.ts @@ -2,7 +2,6 @@ import { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/common/http' import { Router, ActivatedRoute } from '@angular/router' import { CacheTokenService } from '../../service/cache-token.service'//引入服务 -import { CookieService } from 'ngx-cookie-service';//cookie插件 import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { NzMessageService } from 'ng-zorro-antd/message'; import { Base64 } from 'js-base64'; @@ -17,7 +16,7 @@ import { NzNotificationService } from 'ng-zorro-antd/notification'; export class LoginComponent implements OnInit { validateForm!: FormGroup; - constructor(private http: HttpClient, private router: Router, public token: CacheTokenService, private cookieService: CookieService, private fb: FormBuilder, private message: NzMessageService) { } + constructor(private http: HttpClient, private router: Router, public token: CacheTokenService, private fb: FormBuilder, private message: NzMessageService) { } ngOnInit() { this.validateForm = this.fb.group({ @@ -107,14 +106,11 @@ export class LoginComponent implements OnInit { this.autoLogin() sessionStorage.setItem("token", data.token); sessionStorage.setItem("refreshToken", data.refreshToken); - this.cookieService.set("token", data.token, null, '/'); - this.cookieService.set("refreshToken", data.refreshToken, null, '/'); this.router.navigate(['/system/organization']) this.message.create('success', `登录成功`); - }, - (err) => { - this.isLoading = false; - this.message.create('error', err.error.error.details); + + //调用服务中的function刷新token + this.token.startUp() } ) } diff --git a/src/app/service/cache-token.service.ts b/src/app/service/cache-token.service.ts index 092b63d..fb466be 100644 --- a/src/app/service/cache-token.service.ts +++ b/src/app/service/cache-token.service.ts @@ -1,13 +1,12 @@ 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) { } + constructor(private http: HttpClient) { } public timer: number | undefined; @@ -15,15 +14,14 @@ export class CacheTokenService { 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', { + var token = sessionStorage.getItem("token"); + var refreshToken = sessionStorage.getItem("refreshToken"); + this.http.post('/api/Accounts/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, '/'); + sessionStorage.setItem("refreshToken", data.refreshToken); }) }, 18 * 60 * 1000) } diff --git a/src/app/system-management/host-config/host-config.component.ts b/src/app/system-management/host-config/host-config.component.ts index 1675c28..96fe572 100644 --- a/src/app/system-management/host-config/host-config.component.ts +++ b/src/app/system-management/host-config/host-config.component.ts @@ -5,8 +5,9 @@ import { NzFormTooltipIcon } from 'ng-zorro-antd/form'; import { NzModalService } from 'ng-zorro-antd/modal'; import { AddcameraComponent } from './addcamera/addcamera.component'; import { NzMessageService } from 'ng-zorro-antd/message'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpErrorResponse, HttpResponse } from '@angular/common/http'; import { EditcameraComponent } from './editcamera/editcamera.component'; +import { catchError, tap } from 'rxjs/operators'; interface Camera { name: string; user: string; @@ -132,7 +133,21 @@ export class HostConfigComponent implements OnInit { } connect() { - this.router.navigate(['/system/host/camera/imageList']) - } + let ids = [] + this.listOfData.forEach((item: any) => { + ids.push(item.id) + }) + this.http.get('/api/Cameras/Statuses', { + params: { ids: ids } + }).subscribe({ + next: (data) => { + console.log('连接状态', data) + }, + error: (err) => { + console.log('连接失败', err) + }, + // complete: () => console.log('complete!'), // not called + }) + } } diff --git a/src/app/system-management/navigation/navigation.component.ts b/src/app/system-management/navigation/navigation.component.ts index d5ca598..03a2b1e 100644 --- a/src/app/system-management/navigation/navigation.component.ts +++ b/src/app/system-management/navigation/navigation.component.ts @@ -1,5 +1,6 @@ import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; +import { CacheTokenService } from 'src/app/service/cache-token.service'; @Component({ selector: 'app-navigation', @@ -8,11 +9,12 @@ import { Router } from '@angular/router'; }) export class NavigationComponent implements OnInit { - constructor(private router: Router) { } + constructor(private router: Router, public token: CacheTokenService) { } ngOnInit(): void { } signOut() { + this.token.delete() this.router.navigate(['/login']) } }