import { Inject , Injectable , Injector } from '@angular/core' ; import { ActivatedRouteSnapshot , CanActivate , CanActivateChild , CanLoad , Route , RouterStateSnapshot , UrlSegment } from '@angular/router' ; import { AlainAuthConfig } from '@delon/util/config' ; import { CheckSimple , ToLogin } from '../helper' ; import { DA_SERVICE_TOKEN , ITokenService } from '../interface' ; import { SimpleTokenModel } from './simple.model' ; /**
 * Simple 路由守卫, [ACL Document](https://ng-alain.com/auth/guard).
 *
 * ```ts
 * data: {
 *  path: 'home',
 *  canActivate: [ SimpleGuard ]
 * },
 * {
 *   path: 'my',
 *   canActivateChild: [SimpleGuard],
 *   children: [
 *     { path: 'profile', component: MockComponent }
 *   ],
 * },
 * ```
 */ @ Injectable ( { providedIn : 'root' } ) export class SimpleGuard implements CanActivate , CanActivateChild , CanLoad { private url ? : string ; private get cog ( ) : AlainAuthConfig { return this . srv . options ; } constructor ( @ Inject ( DA_SERVICE_TOKEN ) private srv , private injector ) { } private process ( ) { const res = CheckSimple ( this . srv . get ( ) as SimpleTokenModel ) ; if ( ! res ) { ToLogin ( this . cog , this . injector , this . url ) ; } return res ; } canLoad ( route , _segments ) { this . url = route . path ; return this . process ( ) ; } canActivateChild ( _childRoute , state ) { this . url = state . url ; return this . process ( ) ; } canActivate ( _route , state ) { this . url = state . url ; return this . process ( ) ; } }