permission.js 3.87 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 34 35 36 37 38 39 40 41 42
      // const index = [{
      //   path: 'index',
      //   meta: { title: '统计报表', icon: 'dashboard'}
      // }]
      // 首页是gis地图
      const index = [
        {
          path: "",
          meta: { title: "Gis地图", icon: "dashboard" },
          children: [
            {
              path: "enterprise/mapView",
              meta: { title: "Gis地图", icon: "dashboard" }
            }
          ]
        }
      ];
43
      // state.topbarRouters = routes.concat(index);
纪泽龙's avatar
纪泽龙 committed
44 45 46
      const arr = [...index, ...routes];
      state.topbarRouters = arr;
      // state.topbarRouters = routes;
耿迪迪's avatar
耿迪迪 committed
47 48
    },
    SET_SIDEBAR_ROUTERS: (state, routes) => {
纪泽龙's avatar
纪泽龙 committed
49 50
      state.sidebarRouters = routes;
      state.mySidebarRouters = routes;
纪泽龙's avatar
纪泽龙 committed
51
    }
耿迪迪's avatar
耿迪迪 committed
52 53 54 55 56 57 58
  },
  actions: {
    // 生成路由
    GenerateRoutes({ commit }) {
      return new Promise(resolve => {
        // 向后端请求路由数据
        getRouters().then(res => {
纪泽龙's avatar
纪泽龙 committed
59 60 61 62 63 64 65 66 67 68 69 70
          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
71 72
    }
  }
纪泽龙's avatar
纪泽龙 committed
73
};
耿迪迪's avatar
耿迪迪 committed
74 75 76 77 78

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

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

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

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