route.js 2.58 KB
Newer Older
姜雷 committed
1 2
import { asyncRouterMap, constantRouterMap } from '@/router/index';
import { INIT_ROUTERS, SET_ROUTERS, ROUTERS_DONE } from './mutation-types';
姜雷 committed
3

姜雷 committed
4 5 6 7
const filterAsyncRouter = () => {};

const state = {
  isRouteDone: false, // 是否生成了动态路由
姜雷 committed
8
  routers: constantRouterMap,
姜雷 committed
9 10 11 12 13 14 15 16 17 18 19 20 21
  addRouters: [],
};

const getters = {
  isRouteDone: state => state.isRouteDone,
  permission_routers: state => state.routers,
  addRouters: state => state.addRouters,
};

const actions = {
  initRoute({ commit }, menus) {
    return new Promise(resolve => {
      let authList = [];
姜雷 committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
      menus &&
        menus.length &&
        menus.map(item => {
          const mainMenu = {
            ...asyncRouterMap[0],
          };
          if (mainMenu) {
            const { children, ...routeMenu } = mainMenu;
            let subMenuList = [];
            if (item.childList && item.childList.length) {
              item.childList.map(subItem => {
                const subMenu = asyncRouterMap.find(
                  menu => menu.id === subItem.id
                );
                if (subMenu) {
                  subMenuList.push({
                    ...subMenu,
                    path: subItem.menuUrl,
                    name: subItem.id,
                    meta: {
                      ...subMenu.meta,
                      title: subItem.menuName,
                      buts: { ...subItem.buts },
                    },
                  });
                }
              });
            }
            authList.push({
              ...routeMenu,
              path: item.menuUrl,
              name: item.menuName,
              meta: { ...routeMenu.meta, title: item.menuName },
              children: subMenuList,
姜雷 committed
56 57
            });
          }
姜雷 committed
58
        });
姜雷 committed
59 60 61 62 63 64 65 66 67 68 69 70
      console.log(authList);
      authList.push(asyncRouterMap[asyncRouterMap.length - 1]); // 添加404页面
      commit(SET_ROUTERS, authList);
      resolve();
    });
  },
  GenerateRoutes({ commit, rootGetters, dispatch }, data) {
    return new Promise((resolve, reject) => {
      if (rootGetters.userInfo.loginState) {
        commit(ROUTERS_DONE);
        resolve();
      } else {
姜雷 committed
71
        dispatch('Login', {})
姜雷 committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85
          .then(() => {
            commit(ROUTERS_DONE);
            resolve();
          })
          .catch(err => {
            reject(err);
          });
      }
    });
  },
};

const mutations = {
  [SET_ROUTERS](state, routers) {
姜雷 committed
86
    state.addRouters = routers;
姜雷 committed
87
    state.routers = constantRouterMap.concat(routers);
姜雷 committed
88 89 90 91 92 93 94 95 96 97 98 99
  },
  [ROUTERS_DONE](state) {
    state.isRouteDone = true;
  },
};

export default {
  state,
  getters,
  actions,
  mutations,
};