zhangjie 4 年之前
父节点
当前提交
4a39db62db
共有 3 个文件被更改,包括 66 次插入21 次删除
  1. 39 0
      src/constants/authority.js
  2. 19 19
      src/main.js
  3. 8 2
      src/routers/index.js

+ 39 - 0
src/constants/authority.js

@@ -176,3 +176,42 @@ export const inspection = [
     icon: "ivu-icon-score"
   }
 ];
+
+const getLeadRouter = () => {
+  let leadGrade = grading.map(item => item.name);
+  leadGrade.splice(2, 2);
+  let leadMark = mark.map(item => item.name);
+  leadMark.splice(1, 1);
+  leadMark.splice(2, 1);
+  return [...leadGrade, ...leadMark];
+};
+
+const getAdminRouter = isSuper => {
+  let leadGrade = grading.map(item => item.name).slice(0, -1);
+  let leadMark = mark.map(item => item.name).slice(0, -1);
+  let leadMain = main.map(item => item.name).slice(isSuper ? 0 : 1);
+  return [
+    ...leadMain,
+    ...leadGrade,
+    ...leadMark,
+    ...clientSet.map(item => item.name),
+    ...gradingSet.map(item => item.name),
+    ...markSet.map(item => item.name)
+  ];
+};
+
+export const checkRouterValid = (roleCode, routerName) => {
+  console.log(roleCode, routerName);
+  const func = {
+    SUPER_ADMIN: () => getAdminRouter(1),
+    ADMIN: getAdminRouter,
+    MARKER: () => ["GradingOperation", "MarkOperation"],
+    MARK_LEADER: getLeadRouter,
+    INSPECTION: () => inspection.map(item => item.name),
+    QC: () => ["Quality"]
+  };
+
+  const validList = func[roleCode]();
+  console.log(validList);
+  return validList.includes(routerName);
+};

+ 19 - 19
src/main.js

@@ -14,6 +14,7 @@ import ViewUI from "view-design";
 import "./assets/styles/index.less";
 // regist v-chart
 import "./plugins/VueCharts";
+import { checkRouterValid } from "./constants/authority";
 
 Vue.use(ViewUI);
 ViewUI.Message.config({
@@ -30,24 +31,23 @@ Vue.prototype.GLOBAL = GLOBAL;
 Vue.config.productionTip = false;
 
 // route interceptor
-// router.beforeEach((to, from, next) => {
-//   const token = Vue.ls.get("token");
-//   if (to.meta.noRequire) {
-//     // 不需要登录的页面
-//     next();
-//   } else {
-//     // 需要登录的路由
-//     if (token) {
-//       next();
-//     } else {
-//       // 登录失效的处理
-//       Vue.ls.clear();
-//       // Vue.ls.remove("token");
-//       // Vue.ls.remove("user");
-//       next({ name: "Login" });
-//     }
-//   }
-// });
+router.beforeEach((to, from, next) => {
+  const user = Vue.ls.get("user");
+  if (to.meta.noRequire) {
+    // 不需要登录的页面
+    next();
+  } else {
+    // 需要登录的路由
+    if (user && checkRouterValid(user["role"], to.name)) {
+      next();
+    } else {
+      ViewUI.Notice.error({ title: "错误提示", desc: "权限失效!" });
+      // 登录失效的处理
+      Vue.ls.clear();
+      next({ name: "Login" });
+    }
+  }
+});
 
 // axios interceptors
 var load = "";
@@ -82,7 +82,7 @@ axios.interceptors.request.use(
   },
   error => {
     // 关闭loading提示
-    // 串联并发请求,延时处理是为防止多个loading实例闪屏。
+    // 继发请求,延时处理是为防止出现多个loading实例
     setTimeout(() => {
       queue.shift();
       if (!queue.length) load();

+ 8 - 2
src/routers/index.js

@@ -22,7 +22,10 @@ export default new Router({
     {
       path: "/login",
       name: "Login",
-      component: Login
+      component: Login,
+      meta: {
+        noRequire: true
+      }
     },
     ...main,
     ...grading,
@@ -32,7 +35,10 @@ export default new Router({
     {
       path: "*",
       name: "404",
-      component: NotFound
+      component: NotFound,
+      meta: {
+        noRequire: true
+      }
     }
     // [lazy-loaded] route level code-splitting
     // {