Commit 9d616cf0 by 潘建波

提交

1 parent 4288a81d
import axios from '../../util/ajax'
import {asyncRouterMap, constantRouterMap } from '@/router'
/**
* 获取路由列表
*/
function getMenuInfo(token) {
return new Promise((reslove, reject) =>{
axios.get(IP + "/api/v1/auth/apps/" + 'edd4abed322af106f0a06e2204202d79' + "/menus?shape=list",{
headers: {
'authorization': localStorage.getItem('atoken')
},
}).then(response => {
reslove(response);
})
});
}
/**
* 判断是否有权限
* @param {*} router
* @param {*} roles
*/
function hasPerminssion(router, roles) {
let status = false
if (roles) {
roles.forEach(ele => {
if (ele.path == router.path) {
status = true;
return false;
}
})
}
return status
}
/**
* 根据后台返回的路由权限
* 动态匹配路由
* @param {*} asyncRouterMap
* @param {*} roles
*/
function filterAsyncRouter(asyncRouterMap, roles) {
const newData = [];
// return routers
asyncRouterMap.forEach(ele => {
let obj = {};
if (hasPerminssion(ele, roles)) {
obj['path'] = ele.path;
obj['name'] = ele.name;
obj['component'] = ele.component;
if (ele.children && ele.children.length > 0) {
obj['children'] = filterAsyncRouter(ele.children, roles)
} else {
obj['children'] = []
}
} else {
return false
}
newData.push(obj)
});
return newData
}
/**
* 根据路由和当前跳转key(路由路径)
* 获取第三级路由
* @param {*} router
* @param {*} key
*/
function filterTwoMenu(router, key) {
let data = [],
state = true;
router.forEach(e => {
if(e.path == key)
if(e.children) {
data = e.children
} else {
data = [];
}
})
return data;
}
const menu = {
state: {
routers: constantRouterMap,
addRouter: [],
threeMenu:[],
},
mutations:{
SET_ROUTERS: (state, routers) => {
state.addRouter = routers;
state.routers = constantRouterMap.concat(routers);
},
SET_THREEMENU: (state, routers) => {
state.threeMenu = routers;
},
},
actions: {
GetMenuRole({ commit, state}) {
return new Promise((resolve, reject) => {
getMenuInfo().then(response => {
let accessedRouters = filterAsyncRouter(asyncRouterMap,response.data.list_data);
// commit('SET_ROUTERS', accessedRouters);
commit('SET_ROUTERS', accessedRouters);
resolve(accessedRouters);
})
})
},
GetTwoMenu({commit, state},data) {
return new Promise((reslove,reject) => {
const tmenus = filterTwoMenu(state.routers, data);
commit('SET_THREEMENU', tmenus);
reslove(tmenus);
})
},
},
}
export default menu
/**
* 权限模块
* 模块功能
* 1.获取菜单权限
* 2.获取设备信息
* 3.获取websocket信息
* 4.初始化加载所需api
*/
import router from './router'
import store from './store'
import {pushws, facews,devws} from './util/websocket'
import {faceapi} from './util/api.js'
router.beforeEach((to, from, next) => {
if (to.path === '/') {
next()
} else {
if (store.getters.permission_routers.length === 0) { //判断当前用户是否拉取了路由
store.dispatch('GetMenuRole').then(res => { //获取菜单
router.addRoutes(res);
if( !PushWs ){ //菜单拉取成功连接 websocket
pushws();
}
if(!FaceWs) {
facews();
}
if(!DevWs) {
devws();
}
/**
* 初始化页面所需api
**/
faceapi.setBehaviorLocal();
faceapi.getCodeName();
faceapi.getSite();
faceapi.getOrg();
faceapi.getAddress();
faceapi.getFaceDbData();
faceapi.getProvince();
}).catch(() => {
next({
path: '/'
})
})
} else {
}
if (store.getters.dev_unid === ""){ //获取设备信息
store.dispatch('GetDev')
}
}
next()
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!