Files
bbq/src/app/core/guards/auth.guard.ts
Taric Xin e5fd7489cf edit
2022-02-10 09:13:25 +08:00

99 lines
2.9 KiB
TypeScript

/*
* @Description :
* @Version : 1.0
* @Author : Shiming
* @Date : 2022-01-25 16:03:45
* @LastEditors : Shiming
* @LastEditTime : 2022-02-09 17:23:22
* @FilePath : \\tms-obc-web\\src\\app\\core\\guards\\auth.guard.ts
* Copyright (C) 2022 huzhenhong. All rights reserved.
*/
import { Inject, Injectable, Injector } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivate,
CanActivateChild,
CanDeactivate,
CanLoad,
Data,
Route,
Router,
RouterStateSnapshot,
UrlTree
} from '@angular/router';
import { ACLCanType, ACLGuard, ACLGuardType, ACLService } from '@delon/acl';
import { MenuService } from '@delon/theme';
import { BaseService, EAUserService } from '@shared';
import { Observable, of } from 'rxjs';
import { delay, map, switchMap, tap } from 'rxjs/operators';
@Injectable()
export class AuthGuard extends ACLGuard {
constructor(
srv: ACLService,
public srv1: ACLService,
private baseService: BaseService,
private menuService: MenuService,
router: Router,
private inject: Injector
) {
super(srv, router, inject);
}
canActivate(route: ActivatedRouteSnapshot, _state: RouterStateSnapshot): Observable<boolean> {
if (Object.keys(route.params)?.length > 0 || !route.routeConfig?.path) {
return this.handle(route, _state, 1);
} else {
return this.handle(route, _state, 1, _state?.url);
}
}
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.handle(childRoute, state, 2, this.settingRoute(childRoute.params, state.url));
}
private handle(route: ActivatedRouteSnapshot, state: RouterStateSnapshot, type: 1 | 2, router?: string): Observable<boolean> {
if (!router) {
if (type === 1) {
return super.canActivate(route, state);
} else {
return super.canActivateChild(route, state);
}
}
// console.log(route, state, type, router);
return this.baseService.request('/api/mdc/cuc/functionButton/getUserFunctionButton', { link: router }).pipe(
switchMap(res => {
if (res) {
// if (router === '/ticket/invoice-requested') {
// this.srv1.setAbility(['TICKET-INVOICE-REQUESTED-list']);
// } else {
// this.srv1.setAbility(res.abilities || []);
// }
this.srv1.setAbility(res.abilities || []);
// this.menuService.resume();
}
if (type === 1) {
return super.canActivate(route, state);
} else {
return super.canActivateChild(route, state);
}
})
);
}
private settingRoute(params: any, route: string) {
let _route = route;
if (_route.indexOf('?') > -1) {
_route = route.split('?')[0];
}
for (const key of Object.keys(params)) {
if (_route.indexOf(params[key]) > -1) {
_route = _route.replace(params[key], ':id');
}
}
return _route;
}
}