You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.1 KiB
37 lines
1.1 KiB
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' |
|
}) |
|
export class AuthGuard implements CanActivate { |
|
|
|
constructor(private router: Router,private cookieService: CookieService) { |
|
|
|
} |
|
|
|
// 路由守卫 |
|
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { |
|
// console.log('路由守卫',next.data) |
|
// if(next.data.permission == 'xxxx'){ |
|
// return true; |
|
// } |
|
return this.checkLogin(); |
|
} |
|
|
|
checkLogin(): boolean { |
|
// console.log('xxxxxxxxxxxx') |
|
// 判断本地有没有token |
|
const token = this.cookieService.get("token") || sessionStorage.getItem('token'); |
|
|
|
// 如果有token,允许访问 |
|
if (token) { return true; } |
|
|
|
//如果没有token,跳转登录页 |
|
this.router.navigate(['/login']); |
|
|
|
return false; |
|
} |
|
}
|
|
|