permission.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { asyncRoutes, constantRoutes } from '@/router'
  2. import { getSilder } from '@/api/silder'
  3. import { removeToken } from '@/utils/auth'
  4. import Layout from '@/layout'
  5. /**
  6. * 遍历接口菜单添加页面
  7. * @param asyncRoutes
  8. * @param getMenu
  9. */
  10. function generateAsyncRouter (asyncRoutes, data) {
  11. if (!data) {
  12. return []
  13. }
  14. data.forEach((item, index) => {
  15. // console.log(item);
  16. item.component = asyncRoutes[item.component]
  17. if (item.children && item.children.length > 0) {
  18. generateAsyncRouter(asyncRoutes, item.children)
  19. }
  20. })
  21. return data
  22. }
  23. /**
  24. * 判断平台端添加首页
  25. * @param type
  26. */
  27. // const type = getters.type
  28. const state = {
  29. routes: [],
  30. addRoutes: [],
  31. type: '' // 登录的平台类型
  32. }
  33. const mutations = {
  34. SET_ROUTES: (state, routes) => {
  35. state.addRoutes = routes
  36. state.routes = constantRoutes.concat(routes)
  37. }
  38. }
  39. // 递归遍历数组
  40. function recursionRouter (arr) {
  41. let newArr = arr.map(item => {
  42. let obj = {};
  43. obj.component = item.component;
  44. obj.hidden = false;
  45. obj.path = item.path;
  46. obj.meta = { 'title': item.name, 'icon': item.icon }
  47. if (item.sysMenus && item.sysMenus.length > 0) {
  48. obj.children = recursionRouter(item.sysMenus);
  49. }
  50. return obj
  51. })
  52. return newArr
  53. }
  54. const actions = {
  55. generateRoutes ({ commit }, roles) {
  56. return new Promise(resolve => {
  57. // 获取接口返回的权限菜单
  58. getSilder().then(res => {
  59. // console.log(res.data);
  60. // 这里递归 整体修改路由数据
  61. // console.log(res.data.rows);
  62. // let newData = res.data.map(item => {
  63. // let obj = {};
  64. // obj.component = item.component;
  65. // obj.hidden = false;
  66. // obj.path = item.path;
  67. // obj.meta = { 'title': item.name, 'icon': item.icon }
  68. // if (item.sysMenus && item.sysMenus.length > 0) {
  69. // obj.children = item.sysMenus.map(sub => {
  70. // let obj = {};
  71. // obj.component = sub.component;
  72. // obj.hidden = false;
  73. // obj.path = sub.path;
  74. // obj.meta = { 'title': sub.name, 'icon': sub.icon }
  75. // return obj
  76. // });
  77. // }
  78. // return obj
  79. // });
  80. // console.log(newData);
  81. let newData = recursionRouter(res.data);
  82. var accessedRoutes
  83. // 生成异步路由表
  84. accessedRoutes = generateAsyncRouter(asyncRoutes, newData)
  85. // var result = accessedRoutes.concat({ path: '*', redirect: '/404', hidden: true })
  86. commit('SET_ROUTES', accessedRoutes)
  87. resolve(accessedRoutes)
  88. })
  89. })
  90. }
  91. }
  92. export default {
  93. namespaced: true,
  94. state,
  95. mutations,
  96. actions
  97. }