permission.js 3.9 KB
Newer Older
纪泽龙's avatar
纪泽龙 committed
1 2 3 4 5
import { constantRoutes } from "@/router";
import { getRouters } from "@/api/menu";
import Layout from "@/layout/index";
import ParentView from "@/components/ParentView";
import router from "../../router";
耿迪迪's avatar
耿迪迪 committed
6 7 8 9 10 11 12

const permission = {
  state: {
    routes: [],
    addRoutes: [],
    defaultRoutes: [],
    topbarRouters: [],
纪泽龙's avatar
纪泽龙 committed
13
    sidebarRouters: [],
纪泽龙's avatar
纪泽龙 committed
14
    mySidebarRouters: []
耿迪迪's avatar
耿迪迪 committed
15 16 17
  },
  mutations: {
    SET_ROUTES: (state, routes) => {
纪泽龙's avatar
纪泽龙 committed
18 19
      state.addRoutes = routes;
      state.routes = constantRoutes.concat(routes);
耿迪迪's avatar
耿迪迪 committed
20 21
    },
    SET_DEFAULT_ROUTES: (state, routes) => {
纪泽龙's avatar
纪泽龙 committed
22
      state.defaultRoutes = constantRoutes.concat(routes);
耿迪迪's avatar
耿迪迪 committed
23 24 25
    },
    SET_TOPBAR_ROUTES: (state, routes) => {
      // 顶部导航菜单默认添加统计报表栏指向首页
纪泽龙's avatar
纪泽龙 committed
26 27 28 29 30 31 32 33
      // const index = [{
      //   path: 'index',
      //   meta: { title: '统计报表', icon: 'dashboard'}
      // }]
      // 首页是gis地图
      const index = [
        {
          path: "",
纪泽龙's avatar
纪泽龙 committed
34
          meta: { title: "GIS地图", icon: "dashboard" },
纪泽龙's avatar
纪泽龙 committed
35 36 37
          children: [
            {
              path: "enterprise/mapView",
纪泽龙's avatar
纪泽龙 committed
38
              meta: { title: "GIS地图", icon: "dashboard" }
纪泽龙's avatar
纪泽龙 committed
39 40 41 42
            }
          ]
        }
      ];
43
      // state.topbarRouters = routes.concat(index);
44 45
      // const arr = [...index, ...routes];
      const arr = [...routes];
纪泽龙's avatar
纪泽龙 committed
46 47
      state.topbarRouters = arr;
      // state.topbarRouters = routes;
耿迪迪's avatar
耿迪迪 committed
48 49
    },
    SET_SIDEBAR_ROUTERS: (state, routes) => {
纪泽龙's avatar
纪泽龙 committed
50 51
      state.sidebarRouters = routes;
      state.mySidebarRouters = routes;
纪泽龙's avatar
纪泽龙 committed
52
    }
耿迪迪's avatar
耿迪迪 committed
53 54 55 56 57 58 59
  },
  actions: {
    // 生成路由
    GenerateRoutes({ commit }) {
      return new Promise(resolve => {
        // 向后端请求路由数据
        getRouters().then(res => {
纪泽龙's avatar
纪泽龙 committed
60 61 62 63 64 65 66 67 68 69 70 71
          const sdata = JSON.parse(JSON.stringify(res.data));
          const rdata = JSON.parse(JSON.stringify(res.data));
          const sidebarRoutes = filterAsyncRouter(sdata);
          const rewriteRoutes = filterAsyncRouter(rdata, false, true);
          rewriteRoutes.push({ path: "*", redirect: "/404", hidden: true });
          commit("SET_ROUTES", rewriteRoutes);
          commit("SET_SIDEBAR_ROUTERS", constantRoutes.concat(sidebarRoutes));
          commit("SET_DEFAULT_ROUTES", sidebarRoutes);
          commit("SET_TOPBAR_ROUTES", sidebarRoutes);
          resolve(rewriteRoutes);
        });
      });
耿迪迪's avatar
耿迪迪 committed
72 73
    }
  }
纪泽龙's avatar
纪泽龙 committed
74
};
耿迪迪's avatar
耿迪迪 committed
75 76 77 78 79

// 遍历后台传来的路由字符串,转换为组件对象
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  return asyncRouterMap.filter(route => {
    if (type && route.children) {
纪泽龙's avatar
纪泽龙 committed
80
      route.children = filterChildren(route.children);
耿迪迪's avatar
耿迪迪 committed
81 82 83
    }
    if (route.component) {
      // Layout ParentView 组件特殊处理
纪泽龙's avatar
纪泽龙 committed
84 85 86 87
      if (route.component === "Layout") {
        route.component = Layout;
      } else if (route.component === "ParentView") {
        route.component = ParentView;
耿迪迪's avatar
耿迪迪 committed
88
      } else {
纪泽龙's avatar
纪泽龙 committed
89
        route.component = loadView(route.component);
耿迪迪's avatar
耿迪迪 committed
90 91 92
      }
    }
    if (route.children != null && route.children && route.children.length) {
纪泽龙's avatar
纪泽龙 committed
93
      route.children = filterAsyncRouter(route.children, route, type);
耿迪迪's avatar
耿迪迪 committed
94
    } else {
纪泽龙's avatar
纪泽龙 committed
95 96
      delete route["children"];
      delete route["redirect"];
耿迪迪's avatar
耿迪迪 committed
97
    }
纪泽龙's avatar
纪泽龙 committed
98 99
    return true;
  });
耿迪迪's avatar
耿迪迪 committed
100 101 102
}

function filterChildren(childrenMap, lastRouter = false) {
纪泽龙's avatar
纪泽龙 committed
103
  var children = [];
耿迪迪's avatar
耿迪迪 committed
104 105
  childrenMap.forEach((el, index) => {
    if (el.children && el.children.length) {
纪泽龙's avatar
纪泽龙 committed
106
      if (el.component === "ParentView") {
耿迪迪's avatar
耿迪迪 committed
107
        el.children.forEach(c => {
纪泽龙's avatar
纪泽龙 committed
108
          c.path = el.path + "/" + c.path;
耿迪迪's avatar
耿迪迪 committed
109
          if (c.children && c.children.length) {
纪泽龙's avatar
纪泽龙 committed
110 111
            children = children.concat(filterChildren(c.children, c));
            return;
耿迪迪's avatar
耿迪迪 committed
112
          }
纪泽龙's avatar
纪泽龙 committed
113 114 115
          children.push(c);
        });
        return;
耿迪迪's avatar
耿迪迪 committed
116 117 118
      }
    }
    if (lastRouter) {
纪泽龙's avatar
纪泽龙 committed
119
      el.path = lastRouter.path + "/" + el.path;
耿迪迪's avatar
耿迪迪 committed
120
    }
纪泽龙's avatar
纪泽龙 committed
121 122 123
    children = children.concat(el);
  });
  return children;
耿迪迪's avatar
耿迪迪 committed
124 125
}

纪泽龙's avatar
纪泽龙 committed
126 127 128 129
export const loadView = view => {
  // 路由懒加载
  return resolve => require([`@/views/${view}`], resolve);
};
耿迪迪's avatar
耿迪迪 committed
130

纪泽龙's avatar
纪泽龙 committed
131
export default permission;