From 6c8497f63227d1ebc2c860c5b3de4d9f68aaf841 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期五, 18 十二月 2020 09:45:54 +0800
Subject: [PATCH] Merge branch 'master' of https://gitee.com/y_project/RuoYi-Vue

---
 ruoyi-ui/src/layout/components/Sidebar/index.vue    |    4 
 ruoyi-ui/src/store/modules/permission.js            |   51 ++++++++++--
 ruoyi-ui/package.json                               |    1 
 ruoyi-ui/src/views/tool/gen/editTable.vue           |    2 
 ruoyi-ui/src/views/system/user/profile/resetPwd.vue |    2 
 ruoyi-ui/src/layout/components/AppMain.vue          |  121 ++----------------------------
 ruoyi-ui/src/layout/components/TagsView/index.vue   |    5 -
 ruoyi-ui/src/store/getters.js                       |    3 
 ruoyi-ui/src/views/system/user/profile/userInfo.vue |    2 
 ruoyi-ui/src/components/Breadcrumb/index.vue        |    9 --
 10 files changed, 56 insertions(+), 144 deletions(-)

diff --git a/ruoyi-ui/package.json b/ruoyi-ui/package.json
index cb10a63..b64fab0 100644
--- a/ruoyi-ui/package.json
+++ b/ruoyi-ui/package.json
@@ -49,7 +49,6 @@
     "js-cookie": "2.2.1",
     "jsencrypt": "3.0.0-rc.1",
     "nprogress": "0.2.0",
-    "path-to-regexp": "6.2.0",
     "quill": "1.3.7",
     "screenfull": "5.0.2",
     "sortablejs": "1.10.2",
diff --git a/ruoyi-ui/src/components/Breadcrumb/index.vue b/ruoyi-ui/src/components/Breadcrumb/index.vue
index 97603c4..b313fdd 100644
--- a/ruoyi-ui/src/components/Breadcrumb/index.vue
+++ b/ruoyi-ui/src/components/Breadcrumb/index.vue
@@ -10,8 +10,6 @@
 </template>
 
 <script>
-import pathToRegexp from 'path-to-regexp'
-
 export default {
   data() {
     return {
@@ -49,18 +47,13 @@
       }
       return name.trim() === '棣栭〉'
     },
-    pathCompile(path) {
-      const { params } = this.$route
-      var toPath = pathToRegexp.compile(path)
-      return toPath(params)
-    },
     handleLink(item) {
       const { redirect, path } = item
       if (redirect) {
         this.$router.push(redirect)
         return
       }
-      this.$router.push(this.pathCompile(path))
+      this.$router.push(path)
     }
   }
 }
diff --git a/ruoyi-ui/src/layout/components/AppMain.vue b/ruoyi-ui/src/layout/components/AppMain.vue
index f172fc2..a897638 100644
--- a/ruoyi-ui/src/layout/components/AppMain.vue
+++ b/ruoyi-ui/src/layout/components/AppMain.vue
@@ -1,8 +1,7 @@
-<!-- @author ruoyi 20201128 鏀寔涓夌骇浠ヤ笂鑿滃崟缂撳瓨 -->
 <template>
   <section class="app-main">
     <transition name="fade-transform" mode="out-in">
-      <keep-alive :max="20" :exclude="notCacheName">
+      <keep-alive :include="cachedViews">
         <router-view :key="key" />
       </keep-alive>
     </transition>
@@ -10,119 +9,17 @@
 </template>
 
 <script>
-import Global from "@/layout/components/global.js";
-
 export default {
   name: 'AppMain',
   computed: {
-    notCacheName() {
-      var visitedViews = this.$store.state.tagsView.visitedViews;
-      var noCacheViews = [];
-      Object.keys(visitedViews).some((index) => {
-        if (visitedViews[index].meta.noCache) {
-          noCacheViews.push(visitedViews[index].name);
-        }
-      });
-      return noCacheViews;
+    cachedViews() {
+      return this.$store.state.tagsView.cachedViews
     },
     key() {
-      return this.$route.path;
-    },
-  },
-  mounted() {
-    // 鍏抽棴鏍囩瑙﹀彂
-    Global.$on("removeCache", (name, view) => {
-      this.removeCache(name, view);
-    });
-  },
-  methods: {
-    // 鑾峰彇鏈塳eep-alive瀛愯妭鐐圭殑Vnode
-    getVnode() {
-      // 鍒ゆ柇瀛愰泦闈炵┖
-      if (this.$children.length == 0) return false;
-      let vnode;
-      for (let item of this.$children) {
-        // 濡傛灉data涓湁key鍒欎唬琛ㄦ壘鍒颁簡keep-alive涓嬮潰鐨勫瓙闆嗭紝杩欎釜key灏辨槸router-view涓婄殑key
-        if (item.$vnode.data.key) {
-          vnode = item.$vnode;
-          break;
-        }
-      }
-      return vnode ? vnode : false;
-    },
-    // 绉婚櫎keep-alive缂撳瓨
-    removeCache(name, view = {}) {
-      let vnode = this.getVnode();
-      if (!vnode) return false;
-      let componentInstance = vnode.parent.componentInstance;
-      // 杩欎釜key鏄敤鏉ヨ幏鍙栧墠缂�鐢ㄦ潵鍚庨潰姝e垯鍖归厤鐢ㄧ殑
-      let keyStart = vnode.key.split("/")[0];
-      let thisKey = `${keyStart}${view.fullPath}`;
-      let regKey = `${keyStart}${view.path}`;
-
-      this[name]({ componentInstance, thisKey, regKey });
-    },
-    // 绉婚櫎鍏朵粬
-    closeOthersTags({ componentInstance, thisKey }) {
-      Object.keys(componentInstance.cache).forEach((key, index) => {
-        if (key != thisKey) {
-          // 閿�姣佸疄渚�(杩欓噷瀛樺湪澶氫釜key鎸囧悜涓�涓紦瀛樼殑鎯呭喌鍙兘鍓嶉潰涓�涓凡缁忔竻闄ゆ帀浜嗘墍鏈夎鍔犲垽鏂�)
-          if (componentInstance.cache[key]) {
-            componentInstance.cache[key].componentInstance.$destroy();
-          }
-          // 鍒犻櫎缂撳瓨
-          delete componentInstance.cache[key];
-          // 绉婚櫎key涓搴旂殑key
-          componentInstance.keys.splice(index, 1);
-        }
-      });
-    },
-    // 绉婚櫎鎵�鏈夌紦瀛�
-    closeAllTags({ componentInstance }) {
-      // 閿�姣佸疄渚�
-      Object.keys(componentInstance.cache).forEach((key) => {
-        if (componentInstance.cache[key]) {
-          componentInstance.cache[key].componentInstance.$destroy();
-        }
-      });
-      // 鍒犻櫎缂撳瓨
-      componentInstance.cache = {};
-      // 绉婚櫎key涓搴旂殑key
-      componentInstance.keys = [];
-    },
-    // 绉婚櫎鍗曚釜缂撳瓨
-    closeSelectedTag({ componentInstance, regKey }) {
-      let reg = new RegExp(`^${regKey}`);
-      Object.keys(componentInstance.cache).forEach((key, i) => {
-        if (reg.test(key)) {
-          // 閿�姣佸疄渚�
-          if (componentInstance.cache[key]) {
-            componentInstance.cache[key].componentInstance.$destroy();
-          }
-          // 鍒犻櫎缂撳瓨
-          delete componentInstance.cache[key];
-          // 绉婚櫎key涓搴旂殑key
-          componentInstance.keys.splice(i, 1);
-        }
-      });
-    },
-    // 鍒锋柊鍗曚釜缂撳瓨
-    refreshSelectedTag({ componentInstance, thisKey }) {
-      Object.keys(componentInstance.cache).forEach((key, index) => {
-        if (null != thisKey && key.replace("/redirect", "") == thisKey) {
-          // 1 閿�姣佸疄渚�(杩欓噷瀛樺湪澶氫釜key鎸囧悜涓�涓紦瀛樼殑鎯呭喌鍙兘鍓嶉潰涓�涓凡缁忔竻闄ゆ帀浜嗘墍鏈夎鍔犲垽鏂�)
-          if (componentInstance.cache[key]) {
-            componentInstance.cache[key].componentInstance.$destroy();
-          }
-          // 2 鍒犻櫎缂撳瓨
-          delete componentInstance.cache[key];
-          // 3 绉婚櫎key涓搴旂殑key
-          componentInstance.keys.splice(index, 1);
-        }
-      });
-    },
-  },
-};
+      return this.$route.path
+    }
+  }
+}
 </script>
 
 <style lang="scss" scoped>
@@ -134,7 +31,7 @@
   overflow: hidden;
 }
 
-.fixed-header + .app-main {
+.fixed-header+.app-main {
   padding-top: 50px;
 }
 
@@ -144,7 +41,7 @@
     min-height: calc(100vh - 84px);
   }
 
-  .fixed-header + .app-main {
+  .fixed-header+.app-main {
     padding-top: 84px;
   }
 }
diff --git a/ruoyi-ui/src/layout/components/Sidebar/index.vue b/ruoyi-ui/src/layout/components/Sidebar/index.vue
index 54670a3..9e5d2bb 100644
--- a/ruoyi-ui/src/layout/components/Sidebar/index.vue
+++ b/ruoyi-ui/src/layout/components/Sidebar/index.vue
@@ -13,7 +13,7 @@
                 mode="vertical"
             >
                 <sidebar-item
-                    v-for="(route, index) in permission_routes"
+                    v-for="(route, index) in sidebarRouters"
                     :key="route.path  + index"
                     :item="route"
                     :base-path="route.path"
@@ -33,7 +33,7 @@
     components: { SidebarItem, Logo },
     computed: {
         ...mapState(["settings"]),
-        ...mapGetters(["permission_routes", "sidebar"]),
+        ...mapGetters(["sidebarRouters", "sidebar"]),
         activeMenu() {
             const route = this.$route;
             const { meta, path } = route;
diff --git a/ruoyi-ui/src/layout/components/TagsView/index.vue b/ruoyi-ui/src/layout/components/TagsView/index.vue
index 81c2007..bc6ce18 100644
--- a/ruoyi-ui/src/layout/components/TagsView/index.vue
+++ b/ruoyi-ui/src/layout/components/TagsView/index.vue
@@ -29,7 +29,6 @@
 <script>
 import ScrollPane from './ScrollPane'
 import path from 'path'
-import Global from "@/layout/components/global.js";
 
 export default {
   components: { ScrollPane },
@@ -145,7 +144,6 @@
           })
         })
       })
-      Global.$emit("removeCache", "refreshSelectedTag", this.selectedTag);
     },
     closeSelectedTag(view) {
       this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
@@ -153,14 +151,12 @@
           this.toLastView(visitedViews, view)
         }
       })
-      Global.$emit("removeCache", "closeSelectedTag", view);
     },
     closeOthersTags() {
       this.$router.push(this.selectedTag)
       this.$store.dispatch('tagsView/delOthersViews', this.selectedTag).then(() => {
         this.moveToCurrentTag()
       })
-      Global.$emit("removeCache", "closeOthersTags", this.selectedTag);
     },
     closeAllTags(view) {
       this.$store.dispatch('tagsView/delAllViews').then(({ visitedViews }) => {
@@ -169,7 +165,6 @@
         }
         this.toLastView(visitedViews, view)
       })
-      Global.$emit("removeCache", "closeAllTags");
     },
     toLastView(visitedViews, view) {
       const latestView = visitedViews.slice(-1)[0]
diff --git a/ruoyi-ui/src/store/getters.js b/ruoyi-ui/src/store/getters.js
index df51465..00c4ebf 100644
--- a/ruoyi-ui/src/store/getters.js
+++ b/ruoyi-ui/src/store/getters.js
@@ -10,6 +10,7 @@
   introduction: state => state.user.introduction,
   roles: state => state.user.roles,
   permissions: state => state.user.permissions,
-  permission_routes: state => state.permission.routes
+  permission_routes: state => state.permission.routes,
+  sidebarRouters:state => state.permission.sidebarRouters,
 }
 export default getters
diff --git a/ruoyi-ui/src/store/modules/permission.js b/ruoyi-ui/src/store/modules/permission.js
index 5ade2ed..d22d6df 100644
--- a/ruoyi-ui/src/store/modules/permission.js
+++ b/ruoyi-ui/src/store/modules/permission.js
@@ -6,13 +6,17 @@
 const permission = {
   state: {
     routes: [],
-    addRoutes: []
+    addRoutes: [],
+    sidebarRouters: []
   },
   mutations: {
     SET_ROUTES: (state, routes) => {
       state.addRoutes = routes
       state.routes = constantRoutes.concat(routes)
-    }
+    },
+    SET_SIDEBAR_ROUTERS: (state, routers) => {
+      state.sidebarRouters = routers
+    },
   },
   actions: {
     // 鐢熸垚璺敱
@@ -20,10 +24,14 @@
       return new Promise(resolve => {
         // 鍚戝悗绔姹傝矾鐢辨暟鎹�
         getRouters().then(res => {
-          const accessedRoutes = filterAsyncRouter(res.data)
-          accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
-          commit('SET_ROUTES', accessedRoutes)
-          resolve(accessedRoutes)
+          const sdata = JSON.parse(JSON.stringify(res.data))
+          const rdata = JSON.parse(JSON.stringify(res.data))
+          const sidebarRoutes = filterAsyncRouter(sdata)
+          const rewriteRoutes = filterAsyncRouter(rdata, true)
+          rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
+          commit('SET_ROUTES', rewriteRoutes)
+          commit('SET_SIDEBAR_ROUTERS', sidebarRoutes)
+          resolve(rewriteRoutes)
         })
       })
     }
@@ -31,8 +39,11 @@
 }
 
 // 閬嶅巻鍚庡彴浼犳潵鐨勮矾鐢卞瓧绗︿覆锛岃浆鎹负缁勪欢瀵硅薄
-function filterAsyncRouter(asyncRouterMap) {
+function filterAsyncRouter(asyncRouterMap, isRewrite = false) {
   return asyncRouterMap.filter(route => {
+    if (isRewrite && route.children) {
+      route.children = filterChildren(route.children)
+    }
     if (route.component) {
       // Layout ParentView 缁勪欢鐗规畩澶勭悊
       if (route.component === 'Layout') {
@@ -44,14 +55,36 @@
       }
     }
     if (route.children != null && route.children && route.children.length) {
-      route.children = filterAsyncRouter(route.children)
+      route.children = filterAsyncRouter(route.children, route, isRewrite)
     }
     return true
   })
 }
 
+function filterChildren(childrenMap) {
+  var children = []
+  childrenMap.forEach((el, index) => {
+    if (el.children && el.children.length) {
+      if (el.component === 'ParentView') {
+        el.children.forEach(c => {
+          c.path = el.path + '/' + c.path
+          if (c.children && c.children.length) {
+            children = children.concat(filterChildren(c.children, c))
+            return
+          }
+          children.push(c)
+        })
+        childrenMap.splice(index, 1)
+        return
+      }
+    }
+    children = children.concat(el)
+  })
+  return children
+}
+
 export const loadView = (view) => { // 璺敱鎳掑姞杞�
-  return (resolve) =>  require([`@/views/${view}`], resolve)
+  return (resolve) => require([`@/views/${view}`], resolve)
 }
 
 export default permission
diff --git a/ruoyi-ui/src/views/system/user/profile/resetPwd.vue b/ruoyi-ui/src/views/system/user/profile/resetPwd.vue
index 86073d3..ee65240 100644
--- a/ruoyi-ui/src/views/system/user/profile/resetPwd.vue
+++ b/ruoyi-ui/src/views/system/user/profile/resetPwd.vue
@@ -18,7 +18,6 @@
 
 <script>
 import { updateUserPwd } from "@/api/system/user";
-import Global from "@/layout/components/global.js";
 
 export default {
   data() {
@@ -65,7 +64,6 @@
       });
     },
     close() {
-      Global.$emit("removeCache", "closeSelectedTag", this.$route);
       this.$store.dispatch("tagsView/delView", this.$route);
       this.$router.push({ path: "/index" });
     }
diff --git a/ruoyi-ui/src/views/system/user/profile/userInfo.vue b/ruoyi-ui/src/views/system/user/profile/userInfo.vue
index 6b68311..fbb5308 100644
--- a/ruoyi-ui/src/views/system/user/profile/userInfo.vue
+++ b/ruoyi-ui/src/views/system/user/profile/userInfo.vue
@@ -24,7 +24,6 @@
 
 <script>
 import { updateUserProfile } from "@/api/system/user";
-import Global from "@/layout/components/global.js";
 
 export default {
   props: {
@@ -69,7 +68,6 @@
       });
     },
     close() {
-      Global.$emit("removeCache", "closeSelectedTag", this.$route);
       this.$store.dispatch("tagsView/delView", this.$route);
       this.$router.push({ path: "/index" });
     }
diff --git a/ruoyi-ui/src/views/tool/gen/editTable.vue b/ruoyi-ui/src/views/tool/gen/editTable.vue
index db1c31d..9ecab45 100644
--- a/ruoyi-ui/src/views/tool/gen/editTable.vue
+++ b/ruoyi-ui/src/views/tool/gen/editTable.vue
@@ -127,7 +127,6 @@
 import { getGenTable, updateGenTable } from "@/api/tool/gen";
 import { optionselect as getDictOptionselect } from "@/api/system/dict/type";
 import { listMenu as getMenuTreeselect } from "@/api/system/menu";
-import Global from "@/layout/components/global.js";
 import basicInfoForm from "./basicInfoForm";
 import genInfoForm from "./genInfoForm";
 import Sortable from 'sortablejs'
@@ -208,7 +207,6 @@
     },
     /** 鍏抽棴鎸夐挳 */
     close() {
-      Global.$emit("removeCache", "closeSelectedTag", this.$route);
       this.$store.dispatch("tagsView/delView", this.$route);
       this.$router.push({ path: "/tool/gen", query: { t: Date.now()}})
     }

--
Gitblit v1.9.3