tangjinzhou 6 years ago
parent
commit
a589d78263
4 changed files with 59 additions and 3 deletions
  1. 7 2
      src/layouts/SiderMenu.vue
  2. 30 1
      src/router.js
  3. 13 0
      src/utils/auth.js
  4. 9 0
      src/views/403.vue

+ 7 - 2
src/layouts/SiderMenu.vue

@@ -27,6 +27,7 @@
  * SubMenu1.vue https://github.com/vueComponent/ant-design-vue/blob/master/components/menu/demo/SubMenu1.vue
  * SubMenu1.vue https://github.com/vueComponent/ant-design-vue/blob/master/components/menu/demo/SubMenu1.vue
  * */
  * */
 import SubMenu from "./SubMenu";
 import SubMenu from "./SubMenu";
+import { check } from "../utils/auth";
 export default {
 export default {
   props: {
   props: {
     theme: {
     theme: {
@@ -60,7 +61,10 @@ export default {
     },
     },
     getMenuData(routes = [], parentKeys = [], selectedKey) {
     getMenuData(routes = [], parentKeys = [], selectedKey) {
       const menuData = [];
       const menuData = [];
-      routes.forEach(item => {
+      for (let item of routes) {
+        if (item.meta && item.meta.authority && !check(item.meta.authority)) {
+          break;
+        }
         if (item.name && !item.hideInMenu) {
         if (item.name && !item.hideInMenu) {
           this.openKeysMap[item.path] = parentKeys;
           this.openKeysMap[item.path] = parentKeys;
           this.selectedKeysMap[item.path] = [selectedKey || item.path];
           this.selectedKeysMap[item.path] = [selectedKey || item.path];
@@ -88,7 +92,8 @@ export default {
             ...this.getMenuData(item.children, [...parentKeys, item.path])
             ...this.getMenuData(item.children, [...parentKeys, item.path])
           );
           );
         }
         }
-      });
+      }
+
       return menuData;
       return menuData;
     }
     }
   }
   }

+ 30 - 1
src/router.js

@@ -1,8 +1,12 @@
 import Vue from "vue";
 import Vue from "vue";
 import Router from "vue-router";
 import Router from "vue-router";
+import findLast from "lodash/findLast";
+import { notification } from "ant-design-vue";
 import NProgress from "nprogress";
 import NProgress from "nprogress";
 import "nprogress/nprogress.css";
 import "nprogress/nprogress.css";
 import NotFound from "./views/404";
 import NotFound from "./views/404";
+import Forbidden from "./views/403";
+import { check, isLogin } from "./utils/auth";
 
 
 Vue.use(Router);
 Vue.use(Router);
 
 
@@ -36,6 +40,7 @@ const router = new Router({
     },
     },
     {
     {
       path: "/",
       path: "/",
+      meta: { authority: ["user", "admin"] },
       component: () =>
       component: () =>
         import(/* webpackChunkName: "layout" */ "./layouts/BasicLayout"),
         import(/* webpackChunkName: "layout" */ "./layouts/BasicLayout"),
       children: [
       children: [
@@ -64,7 +69,7 @@ const router = new Router({
           path: "/form",
           path: "/form",
           name: "form",
           name: "form",
           component: { render: h => h("router-view") },
           component: { render: h => h("router-view") },
-          meta: { icon: "form", title: "表单" },
+          meta: { icon: "form", title: "表单", authority: ["admin"] },
           children: [
           children: [
             {
             {
               path: "/form/basic-form",
               path: "/form/basic-form",
@@ -109,6 +114,12 @@ const router = new Router({
         }
         }
       ]
       ]
     },
     },
+    {
+      path: "/403",
+      name: "403",
+      hideInMenu: true,
+      component: Forbidden
+    },
     {
     {
       path: "*",
       path: "*",
       name: "404",
       name: "404",
@@ -122,6 +133,24 @@ router.beforeEach((to, from, next) => {
   if (to.path !== from.path) {
   if (to.path !== from.path) {
     NProgress.start();
     NProgress.start();
   }
   }
+  const record = findLast(to.matched, record => record.meta.authority);
+  if (record && !check(record.meta.authority)) {
+    if (!isLogin() && to.path !== "/user/login") {
+      next({
+        path: "/user/login"
+      });
+    } else if (to.path !== "/403") {
+      notification.error({
+        message: "403",
+        description: "你没有权限访问,请联系管理员咨询。"
+      });
+      next({
+        path: "/403"
+      });
+    }
+    NProgress.done();
+  }
+
   next();
   next();
 });
 });
 
 

+ 13 - 0
src/utils/auth.js

@@ -0,0 +1,13 @@
+export function getCurrentAuthority() {
+  return ["user"];
+}
+
+export function check(authority) {
+  const current = getCurrentAuthority();
+  return current.some(item => authority.includes(item));
+}
+
+export function isLogin() {
+  const current = getCurrentAuthority();
+  return current && current[0] !== "guest";
+}

+ 9 - 0
src/views/403.vue

@@ -0,0 +1,9 @@
+<template>
+  <div>403</div>
+</template>
+
+<script>
+export default {};
+</script>
+
+<style></style>