123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import { asyncRoutes, constantRoutes } from '@/router'
- import { getSilder } from '@/api/silder'
- // import { stat } from 'fs'
- // import { removeToken } from '@/utils/auth'
- // import Layout from '@/layout'
- /**
- * 遍历接口菜单添加页面
- * @param asyncRoutes
- * @param getMenu
- */
- function generateAsyncRouter (asyncRoutes, data) {
- if (!data) {
- return []
- }
- // console.log(data)
- data.forEach((item) => {
- item.component = asyncRoutes[item.component]
- if (item.children && item.children.length > 0) {
- generateAsyncRouter(asyncRoutes, item.children)
- }
- })
- return data
- }
- /**
- * 判断平台端添加首页
- * @param type
- */
- // const type = getters.type
- const state = {
- routes: [],
- addRoutes: [],
- type: '', // 登录的平台类型
- permission: [] // 权限
- }
- const mutations = {
- SET_ROUTES: (state, routes) => {
- state.addRoutes = routes
- state.routes = constantRoutes.concat(routes)
- },
- SET_PERMISSION: (state, permission) => {
- state.permission = permission
- }
- }
- // 路由
- // 递归遍历数组
- function recursionRouter (arr) {
- if (arr.length > 0) {
- let newArr = [];
- for (let i = 0; i < arr.length; i++) {
- if (arr[i].type == 1) {
- continue
- }
- let obj = {};
- obj.component = arr[i].component;
- obj.name = arr[i].component;
- // if (item.type != '1' && item.component) {
- // if (!item.path.startsWith('/') && item.component != 'Layout') {
- // obj.names = item.name
- // }
- // }
- arr[i].hid == 0 ? obj.hidden = false : obj.hidden = true
- // console.log('高亮标签'+arr[i].parentPermission,'普通路径'+arr[i].path)
- obj.path = arr[i].path;
- obj.meta = { 'title': arr[i].name, 'icon': arr[i].icon, 'noCache': arr[i].keepAlive, 'activeMenu': arr[i].parentPermission }
- if (arr[i].sysMenus && arr[i].sysMenus.length > 0) {
- obj.children = recursionRouter(arr[i].sysMenus);
- }
- newArr.push(obj)
- }
- return newArr
- }
- }
- // 权限
- // 递归遍历数组
- let tempArr = []
- function recursionPermission (arr) {
- arr.map(item => {
- tempArr.push(item.memo)
- if (item.sysMenus && item.sysMenus.length > 0) {
- recursionPermission(item.sysMenus)
- }
- })
- }
- const actions = {
- generateRoutes ({ commit }) {
- return new Promise(resolve => {
- // 获取接口返回的权限菜单
- getSilder().then(res => {
- if (res.code == 200) {
- let newData = recursionRouter(res.data);
- recursionPermission(res.data)
- let accessedRoutes
- // 生成异步路由表
- accessedRoutes = generateAsyncRouter(asyncRoutes, newData)
- // var result = accessedRoutes.concat({ path: '*', redirect: '/404', hidden: true })
- commit('SET_ROUTES', accessedRoutes)
- // commit('SET_PERMISSION', recursionPermission(res.data).flat(Infinity))
- window.localStorage.removeItem('permission')
- window.localStorage.setItem('permission', tempArr)
- resolve(accessedRoutes)
- this.dispatch('app/setDotStatus')
- }
- })
- })
- },
- removePermission ({ commit }) {
- window.localStorage.removeItem('permission')
- commit('SET_PERMISSION', [])
- }
- }
- export default {
- namespaced: true,
- state,
- mutations,
- actions
- }
|