Newer
Older
KaiFengPC / src / store / modules / permission.js
@zhangdeliang zhangdeliang on 18 Jul 3 KB update
  1. import auth from '@/plugins/auth';
  2. import router, { constantRoutes } from '@/router';
  3. import { getRouters } from '@/api/menu';
  4. import Layout from '@/layout/index';
  5. import ParentView from '@/components/ParentView';
  6. import InnerLink from '@/layout/components/InnerLink';
  7. // 匹配views里面所有的.vue文件
  8. const modules = import.meta.glob('./../../views/**/*.vue');
  9.  
  10. const usePermissionStore = defineStore('permission', {
  11. state: () => ({
  12. routes: [],
  13. addRoutes: [],
  14. sidebarRouters: [],
  15. }),
  16. actions: {
  17. setRoutes(routes) {
  18. this.addRoutes = routes;
  19. this.routes = constantRoutes.concat(routes);
  20. },
  21. setSidebarRouters(routes) {
  22. this.sidebarRouters = routes;
  23. },
  24. generateRoutes(roles) {
  25. return new Promise(resolve => {
  26. // 向后端请求路由数据
  27. getRouters('').then(res => {
  28. const sdata = JSON.parse(JSON.stringify(res.data));
  29. const rdata = JSON.parse(JSON.stringify(res.data));
  30. const sidebarRoutes = filterAsyncRouter(sdata);
  31. const rewriteRoutes = filterAsyncRouter(rdata, false, true);
  32. this.setSidebarRouters(constantRoutes.concat(sidebarRoutes));
  33. resolve(rewriteRoutes);
  34. console.log('2222222222222', rewriteRoutes);
  35. // 登录成功之后才默认加载菜单设置
  36. if (localStorage.getItem('fromDoorXG') == 'chengguanweiScreen') {
  37. localStorage.setItem('routerPartXG', rewriteRoutes[1].name); //默认加载第一个菜单
  38. } else {
  39. router.push({ path: '/chengguanweiScreen' });
  40. }
  41. });
  42. });
  43. },
  44. },
  45. });
  46.  
  47. // 遍历后台传来的路由字符串,转换为组件对象
  48. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  49. return asyncRouterMap.filter(route => {
  50. route.componentfullPath = route.component;
  51. if (type && route.children) {
  52. route.children = filterChildren(route.children);
  53. }
  54. if (route.component) {
  55. // Layout ParentView 组件特殊处理
  56. if (route.component === 'Layout') {
  57. route.component = Layout;
  58. } else if (route.component === 'ParentView') {
  59. route.component = ParentView;
  60. } else if (route.component === 'InnerLink') {
  61. route.component = InnerLink;
  62. } else {
  63. route.component = loadView(route.component);
  64. }
  65. }
  66. if (route.children != null && route.children && route.children.length) {
  67. route.children = filterAsyncRouter(route.children, route, type);
  68. } else {
  69. delete route['children'];
  70. delete route['redirect'];
  71. }
  72. return true;
  73. });
  74. }
  75.  
  76. function filterChildren(childrenMap, lastRouter = false) {
  77. var children = [];
  78. childrenMap.forEach((el, index) => {
  79. if (el.children && el.children.length) {
  80. if (el.component === 'ParentView' && !lastRouter) {
  81. el.children.forEach(c => {
  82. c.path = el.path + '/' + c.path;
  83. if (c.children && c.children.length) {
  84. children = children.concat(filterChildren(c.children, c));
  85. return;
  86. }
  87. children.push(c);
  88. });
  89. return;
  90. }
  91. }
  92. if (lastRouter) {
  93. el.path = lastRouter.path + '/' + el.path;
  94. }
  95. children = children.concat(el);
  96. });
  97. return children;
  98. }
  99.  
  100. // 动态路由遍历,验证是否具备权限
  101. export function filterDynamicRoutes(routes) {
  102. const res = [];
  103. routes.forEach(route => {
  104. if (route.permissions) {
  105. if (auth.hasPermiOr(route.permissions)) {
  106. res.push(route);
  107. }
  108. } else if (route.roles) {
  109. if (auth.hasRoleOr(route.roles)) {
  110. res.push(route);
  111. }
  112. }
  113. });
  114. return res;
  115. }
  116.  
  117. export const loadView = view => {
  118. let res;
  119. for (const path in modules) {
  120. const dir = path.split('views/')[1].split('.vue')[0];
  121. if (dir === view) {
  122. res = () => modules[path]();
  123. }
  124. }
  125. return res;
  126. };
  127.  
  128. export default usePermissionStore;