import { Injectable } from '@angular/core';
import {
  CanActivate,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
} from '@angular/router';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
import Parse from 'parse';

@Injectable({
  providedIn: 'root',
})
export class AuthGuard implements CanActivate {
  constructor(
    public router: Router,
  ) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean> | boolean {
    // 当前路由url
    let url: string = state.url;
    return this.checkLogin(url);
  }
  checkLogin(url: string): boolean {
    console.log(url);
    // 如果已登录,直接跳转当前路由 跳出该函数
    let p = localStorage.getItem('profile');
    if(p && JSON.parse(p)?.identyType == 'anchor'){
      if(url == '/tabs/anthirhome'){
        return true;
      }else{
        this.router.navigate(['/tabs/anthirhome'],{ replaceUrl: true });
        return false;
      }
    }
    return true;
  }

}
export { CanActivate };
