import { Injectable } from '@angular/core';
import {
  CanActivate,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
} from '@angular/router';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
import { textbookServer } from '../../services/textbook';
@Injectable({
  providedIn: 'root',
})
export class AuthGuard implements CanActivate {
  LoginPage = '/user/login';

  constructor(private tbookSer: textbookServer, private router: Router) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean> | boolean {
    // 当前路由url
    // let url: string = state.url;
    // console.log(url);
    return this.checkLogin();
  }
  checkLogin(): boolean {
    if (this.tbookSer.profile.identity == '国家级管理员') {
      return true;
    }
    // 否则重定向到login页面
    this.router.navigate([this.LoginPage]);
    return false;
  }
}
export { CanActivate };
