瀏覽代碼

canActivate优化

seimin 1 年之前
父節點
當前提交
8efdd7eb8a

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

@@ -8,14 +8,10 @@ export class DisinfectionSupplyGuard implements CanActivate  {
8
   constructor(public router: Router) { }
8
   constructor(public router: Router) { }
9
 
9
 
10
   canActivate(): boolean {
10
   canActivate(): boolean {
11
-    let menus = JSON.parse(localStorage.getItem('menu'));
11
+    const menus = JSON.parse(localStorage.getItem('menu'));
12
     let can = false;
12
     let can = false;
13
     if (menus) {
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
     if (!can) {
16
     if (!can) {
21
       this.router.navigate(['login']);
17
       this.router.navigate(['login']);

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

@@ -8,14 +8,10 @@ export class SpecimenView2Guard implements CanActivate {
8
   constructor(public router: Router) { }
8
   constructor(public router: Router) { }
9
 
9
 
10
   canActivate(): boolean {
10
   canActivate(): boolean {
11
-    let menus = JSON.parse(localStorage.getItem('menu'));
11
+    const menus = JSON.parse(localStorage.getItem('menu'));
12
     let can = false;
12
     let can = false;
13
     if (menus) {
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
     if (!can) {
16
     if (!can) {
21
       this.router.navigate(['login']);
17
       this.router.navigate(['login']);

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

@@ -1,6 +1,5 @@
1
 import { Injectable } from '@angular/core';
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
 @Injectable({
4
 @Injectable({
6
   providedIn: 'root'
5
   providedIn: 'root'
@@ -10,14 +9,10 @@ export class FuwutaiGuard implements CanActivate {
10
   constructor(public router: Router) { }
9
   constructor(public router: Router) { }
11
 
10
 
12
   canActivate(): boolean {
11
   canActivate(): boolean {
13
-    let menus = JSON.parse(localStorage.getItem('menu'));
12
+    const menus = JSON.parse(localStorage.getItem('menu'));
14
     let can = false;
13
     let can = false;
15
     if (menus) {
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
     if (!can) {
17
     if (!can) {
23
       this.router.navigate(['login']);
18
       this.router.navigate(['login']);

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

@@ -1,6 +1,5 @@
1
 import { Injectable } from '@angular/core';
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
 @Injectable({
4
 @Injectable({
6
   providedIn: 'root'
5
   providedIn: 'root'
@@ -9,15 +8,10 @@ export class MainGuard implements CanActivate {
9
   constructor(public router: Router) { }
8
   constructor(public router: Router) { }
10
 
9
 
11
   canActivate(): boolean {
10
   canActivate(): boolean {
12
-    let menus = JSON.parse(localStorage.getItem('menu'));
11
+    const menus = JSON.parse(localStorage.getItem('menu'));
13
     let can = false;
12
     let can = false;
14
     if (menus) {
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
     if (!can) {
16
     if (!can) {
23
       this.router.navigate(['login']);
17
       this.router.navigate(['login']);

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

@@ -1,6 +1,5 @@
1
 import { Injectable } from '@angular/core';
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
 @Injectable({
4
 @Injectable({
6
   providedIn: 'root'
5
   providedIn: 'root'
@@ -10,14 +9,10 @@ export class NurseGuard implements CanActivate {
10
   constructor(public router: Router) { }
9
   constructor(public router: Router) { }
11
 
10
 
12
   canActivate(): boolean {
11
   canActivate(): boolean {
13
-    let menus = JSON.parse(localStorage.getItem('menu'));
12
+    const menus = JSON.parse(localStorage.getItem('menu'));
14
     let can = false;
13
     let can = false;
15
     if(menus){
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
     if (!can) {
17
     if (!can) {
23
       this.router.navigate(['login']);
18
       this.router.navigate(['login']);

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

@@ -1,6 +1,5 @@
1
 import { Injectable } from '@angular/core';
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
 @Injectable({
4
 @Injectable({
6
   providedIn: 'root'
5
   providedIn: 'root'
@@ -9,14 +8,10 @@ export class PharmacyGuard implements CanActivate {
9
   constructor(public router: Router) { }
8
   constructor(public router: Router) { }
10
 
9
 
11
   canActivate(): boolean {
10
   canActivate(): boolean {
12
-    let menus = JSON.parse(localStorage.getItem('menu'));
11
+    const menus = JSON.parse(localStorage.getItem('menu'));
13
     let can = false;
12
     let can = false;
14
     if (menus) {
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
     if (!can) {
16
     if (!can) {
22
       this.router.navigate(['login']);
17
       this.router.navigate(['login']);