소스 검색

canActivate优化

seimin 1 년 전
부모
커밋
8efdd7eb8a

+ 2 - 6
src/app/guard/DisinfectionSupplyGuard.guard.ts

@@ -8,14 +8,10 @@ export class DisinfectionSupplyGuard implements CanActivate  {
8 8
   constructor(public router: Router) { }
9 9
 
10 10
   canActivate(): boolean {
11
-    let menus = JSON.parse(localStorage.getItem('menu'));
11
+    const menus = JSON.parse(localStorage.getItem('menu'));
12 12
     let can = false;
13 13
     if (menus) {
14
-      menus.forEach((e) => {
15
-        if (e.link == 'disinfectionSupply') {
16
-          can = true;
17
-        }
18
-      });
14
+      can = menus.some(e => e.link == 'disinfectionSupply');
19 15
     }
20 16
     if (!can) {
21 17
       this.router.navigate(['login']);

+ 2 - 6
src/app/guard/SpecimenView2Guard.guard.ts

@@ -8,14 +8,10 @@ export class SpecimenView2Guard implements CanActivate {
8 8
   constructor(public router: Router) { }
9 9
 
10 10
   canActivate(): boolean {
11
-    let menus = JSON.parse(localStorage.getItem('menu'));
11
+    const menus = JSON.parse(localStorage.getItem('menu'));
12 12
     let can = false;
13 13
     if (menus) {
14
-      menus.forEach((e) => {
15
-        if (e.link == 'specimenView2') {
16
-          can = true;
17
-        }
18
-      });
14
+      can = menus.some(e => e.link == 'specimenView2');
19 15
     }
20 16
     if (!can) {
21 17
       this.router.navigate(['login']);

+ 3 - 8
src/app/guard/fuwutai.guard.ts

@@ -1,6 +1,5 @@
1 1
 import { Injectable } from '@angular/core';
2
-import { Router, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, CanActivate } from '@angular/router';
3
-import { Observable } from 'rxjs';
2
+import { Router, CanActivate } from '@angular/router';
4 3
 
5 4
 @Injectable({
6 5
   providedIn: 'root'
@@ -10,14 +9,10 @@ export class FuwutaiGuard implements CanActivate {
10 9
   constructor(public router: Router) { }
11 10
 
12 11
   canActivate(): boolean {
13
-    let menus = JSON.parse(localStorage.getItem('menu'));
12
+    const menus = JSON.parse(localStorage.getItem('menu'));
14 13
     let can = false;
15 14
     if (menus) {
16
-      menus.forEach((e) => {
17
-        if (e.link == 'dispatchingDesk') {
18
-          can = true;
19
-        }
20
-      });
15
+      can = menus.some(e => e.link == 'dispatchingDesk');
21 16
     }
22 17
     if (!can) {
23 18
       this.router.navigate(['login']);

+ 3 - 9
src/app/guard/main.guard.ts

@@ -1,6 +1,5 @@
1 1
 import { Injectable } from '@angular/core';
2
-import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, CanActivate, Router } from '@angular/router';
3
-import { Observable } from 'rxjs';
2
+import { CanActivate, Router } from '@angular/router';
4 3
 
5 4
 @Injectable({
6 5
   providedIn: 'root'
@@ -9,15 +8,10 @@ export class MainGuard implements CanActivate {
9 8
   constructor(public router: Router) { }
10 9
 
11 10
   canActivate(): boolean {
12
-    let menus = JSON.parse(localStorage.getItem('menu'));
11
+    const menus = JSON.parse(localStorage.getItem('menu'));
13 12
     let can = false;
14 13
     if (menus) {
15
-      console.log(menus,'守卫');
16
-      menus.forEach((e) => {
17
-        if (!e.parentid && !e.link && e.event == 1) {
18
-          can = true;
19
-        }
20
-      });
14
+      can = menus.some(e => !e.parentid && !e.link && e.event == 1);
21 15
     }
22 16
     if (!can) {
23 17
       this.router.navigate(['login']);

+ 3 - 8
src/app/guard/nurse.guard.ts

@@ -1,6 +1,5 @@
1 1
 import { Injectable } from '@angular/core';
2
-import { Router, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, CanActivate } from '@angular/router';
3
-import { Observable } from 'rxjs';
2
+import { Router, CanActivate } from '@angular/router';
4 3
 
5 4
 @Injectable({
6 5
   providedIn: 'root'
@@ -10,14 +9,10 @@ export class NurseGuard implements CanActivate {
10 9
   constructor(public router: Router) { }
11 10
 
12 11
   canActivate(): boolean {
13
-    let menus = JSON.parse(localStorage.getItem('menu'));
12
+    const menus = JSON.parse(localStorage.getItem('menu'));
14 13
     let can = false;
15 14
     if(menus){
16
-      menus.forEach((e) => {
17
-        if (e.link == 'nurse') {
18
-          can = true;
19
-        }
20
-      });
15
+      can = menus.some(e => e.link == 'nurse');
21 16
     }
22 17
     if (!can) {
23 18
       this.router.navigate(['login']);

+ 3 - 8
src/app/guard/pharmacy.guard.ts

@@ -1,6 +1,5 @@
1 1
 import { Injectable } from '@angular/core';
2
-import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
3
-import { Observable } from 'rxjs';
2
+import { CanActivate, Router } from '@angular/router';
4 3
 
5 4
 @Injectable({
6 5
   providedIn: 'root'
@@ -9,14 +8,10 @@ export class PharmacyGuard implements CanActivate {
9 8
   constructor(public router: Router) { }
10 9
 
11 10
   canActivate(): boolean {
12
-    let menus = JSON.parse(localStorage.getItem('menu'));
11
+    const menus = JSON.parse(localStorage.getItem('menu'));
13 12
     let can = false;
14 13
     if (menus) {
15
-      menus.forEach((e) => {
16
-        if (e.link == 'pharmacy') {
17
-          can = true;
18
-        }
19
-      });
14
+      can = menus.some(e => e.link == 'pharmacy');
20 15
     }
21 16
     if (!can) {
22 17
       this.router.navigate(['login']);