import { Injectable } from '@angular/core';
import { User, Role } from './user/user.type';
import { HttpClient } from '@angular/common/http';
import { tap } from 'rxjs/operators';
import {JwtHelperService} from '@auth0/angular-jwt';
import { ActivatedRouteSnapshot, Router } from '@angular/router'
import {BehaviorSubject, Observable} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(private http: HttpClient, private router: Router) {
}
private user: User;
private authCodeArray$ = new BehaviorSubject<string[]>([]);
public auth(params: { username: string; password: string }) {
// tslint:disable-next-line:no-shadowed-variable
return this.http.get<any>('/user/login', { params }).pipe(tap(user => {
this.user = user.data;
this.router.navigate(['/home/user'])
}
)
); /// login and get the user and save it on the client side
}
public isLoggin(): boolean {
return this.user != null;
}
public getCurrUser(): any {
return this.user;
}
// res: {code: number, data: User[]}
public isAnyRoles(roles: string[]): boolean {
if (this.user != null) {
return roles.includes(this.user.role.roleName);
} else {
this.router.navigate([''])
}
}
parsToken(token: string): string[] {
const helper = new JwtHelperService();
try {
const {rol} = helper.decodeToken(token);
console.log('权限码' + rol.split(','));
return rol.split(',');
} catch (e) {
return [];
}
}
setAuthCode(authArr: string[]): void {
this.authCodeArray$.next(authArr);
}
getAuthCode(): Observable<string[]> {
return this.authCodeArray$.asObservable();
}
// public hasAnyPermission(permission:string[]): boolean{
// if(this.user) {
// return roles.includes(this.user.permissions.roleName);
// }else{
// return false;
// }
// }
// public hasSomePermission(permission:string[]): boolean{
// if(this.user) {
// return roles.includes(this.user.permissions.roleName);
// }else{
// return false;
// }
// }
}
还要做什么操作呢 大佬