123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- // import { asyncRoutes, constantRoutes } from '@/router'
- import { constantRoutes } from '@/router'
- import { validatenull } from '@/utils/validate'
- // for get menus from server
- // import { getMenuByRole } from '@/api/menu'
- import { getRecuData,transData,GetDataByName } from '@/api/common'
- import Layout from '@/layout'
- function buildRouter(aMenu) {
- const aRouter = []
- aMenu.forEach(item => {
- if (!validatenull(item.component)) {
- const oRouter = {
- meta: { 'title': '', 'icon': '' },
- children: []
- }
- if (item.component !== 'Iframe') {
- if (item.component === 'Layout') {
- oRouter.component = Layout
- } else {
- oRouter.component = require('@/views/' + item.component + '.vue').default
- }
- oRouter.name = item.path
- oRouter.id = item.id || null
- if (item.redirect !== '') {
- oRouter.redirect = item.redirect || null
- oRouter.path = '/' + item.path
- } else {
- oRouter.path = item.path
- }
- // console.log(item)
- if(item.seebutton == 1 || item.seebutton == 2){
- oRouter.meta.icon = item.icon
- oRouter.meta.title = item.title
- // oRouter.meta.noCache = item.noCache || false
- oRouter.meta.noCache = true
- // oRouter.meta.breadcrumb = item.breadcrumb || true
- oRouter.children = validatenull(item.children) ? [] : buildRouter(item.children)
- aRouter.push(oRouter)
- }
- }
- }
- })
- return aRouter
- }
- /**
- * Use meta.role to determine if the current user has permission
- * @param roles
- * @param route
- */
- function hasPermission(roles, route) {
- if (route.meta && route.meta.roles) {
- return roles.some(role => route.meta.roles.includes(role))
- } else {
- return true
- }
- }
- /**
- * Filter asynchronous routing tables by recursion
- * @param routes asyncRoutes
- * @param roles
- */
- export function filterAsyncRoutes(routes, roles) {
- const res = []
- routes.forEach(route => {
- const tmp = { ...route }
- if (hasPermission(roles, tmp)) {
- if (tmp.children) {
- tmp.children = filterAsyncRoutes(tmp.children, roles)
- }
- res.push(tmp)
- }
- })
- return res
- }
- const state = {
- routes: [],
- addRoutes: [],
- data1: {
- name: 'getmenu',
- idname: 'id',
- parammaps: { id: -1 }
- }
- }
- const mutations = {
- SET_ROUTES: (state, routes) => {
- // console.log(state,routes)
- state.addRoutes = routes
- state.routes = constantRoutes.concat(routes)
- }
- }
- const actions = {
- generateRoutes({ commit }, roles) {
- return new Promise((resolve, reject) => {
- GetDataByName(state.data1).then(response => {
- if(response.data.list !== null){
- for(let i=0;i<response.data.list.length;i++){
- response.data.list[i].affix = true
- response.data.list[i].keepAlive = true
- if(response.data.list[i].children !== undefined){
- for(let j=0;j<response.data.list[i].children.length;j++){
- response.data.list[i].children[j].affix = true
- response.data.list[i].children[j].keepAlive = true
- }
- }
- }
- console.log(response.data.list,'==========9999')
- const menus = transData(response.data.list, 'id', 'parentId', 'children')
- // const menus = response.data
- if (menus.length === 0) {
- reject('menus data is null1')
- }
- const accessedRoutes = buildRouter(menus)
- // final add 404
- accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
- // commit to stores
- commit('SET_ROUTES', accessedRoutes)
- // return for add to router
- resolve(accessedRoutes)
- } else {
- const menus = []
- // const menus = response.data
- if (menus.length == 0) {
- reject('当前用户角色未分配页面权限,无法登录')
- }
- const accessedRoutes = buildRouter(menus)
- // final add 404
- accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
- // commit to stores
- commit('SET_ROUTES', accessedRoutes)
- // return for add to router
- resolve(accessedRoutes)
- }
- })
- })
- }
- }
- export default {
- namespaced: true,
- state,
- mutations,
- actions
- }
|