permission.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { defineStore } from 'pinia';
  2. import router, { constantRoutes, dynamicRoutes } from '@/router';
  3. import store from '@/store';
  4. import { getRouters } from '@/api/menu';
  5. import auth from '@/plugins/auth';
  6. import { RouteRecordRaw } from 'vue-router';
  7. import Layout from '@/layout/index.vue';
  8. import ParentView from '@/components/ParentView/index.vue';
  9. import InnerLink from '@/layout/components/InnerLink/index.vue';
  10. // 匹配views里面所有的.vue文件
  11. const modules = import.meta.glob('./../../views/**/*.vue');
  12. export const usePermissionStore = defineStore('permission', () => {
  13. const routes = ref<RouteRecordRaw[]>([]);
  14. const addRoutes = ref<RouteRecordRaw[]>([]);
  15. const defaultRoutes = ref<RouteRecordRaw[]>([]);
  16. const topbarRouters = ref<RouteRecordRaw[]>([]);
  17. const sidebarRouters = ref<RouteRecordRaw[]>([]);
  18. const getRoutes = (): RouteRecordRaw[] => {
  19. return routes.value;
  20. };
  21. const getSidebarRoutes = (): RouteRecordRaw[] => {
  22. return sidebarRouters.value;
  23. };
  24. const getTopbarRoutes = (): RouteRecordRaw[] => {
  25. return topbarRouters.value;
  26. };
  27. const setRoutes = (newRoutes: RouteRecordRaw[]): void => {
  28. addRoutes.value = newRoutes;
  29. routes.value = constantRoutes.concat(newRoutes);
  30. };
  31. const setDefaultRoutes = (routes: RouteRecordRaw[]): void => {
  32. defaultRoutes.value = constantRoutes.concat(routes);
  33. };
  34. const setTopbarRoutes = (routes: RouteRecordRaw[]): void => {
  35. topbarRouters.value = routes;
  36. };
  37. const setSidebarRouters = (routes: RouteRecordRaw[]): void => {
  38. sidebarRouters.value = routes;
  39. };
  40. const generateRoutes = async (): Promise<RouteRecordRaw[]> => {
  41. const res = await getRouters();
  42. const { data } = res;
  43. const sdata = JSON.parse(JSON.stringify(data));
  44. const rdata = JSON.parse(JSON.stringify(data));
  45. const defaultData = JSON.parse(JSON.stringify(data));
  46. const sidebarRoutes = filterAsyncRouter(sdata);
  47. const rewriteRoutes = filterAsyncRouter(rdata, undefined, true);
  48. const defaultRoutes = filterAsyncRouter(defaultData);
  49. const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
  50. asyncRoutes.forEach((route) => {
  51. router.addRoute(route);
  52. });
  53. setRoutes(rewriteRoutes);
  54. setSidebarRouters(constantRoutes.concat(sidebarRoutes));
  55. setDefaultRoutes(sidebarRoutes);
  56. setTopbarRoutes(defaultRoutes);
  57. return new Promise<RouteRecordRaw[]>((resolve) => resolve(rewriteRoutes));
  58. };
  59. /**
  60. * 遍历后台传来的路由字符串,转换为组件对象
  61. * @param asyncRouterMap 后台传来的路由字符串
  62. * @param lastRouter 上一级路由
  63. * @param type 是否是重写路由
  64. */
  65. const filterAsyncRouter = (asyncRouterMap: RouteRecordRaw[], lastRouter?: RouteRecordRaw, type = false): RouteRecordRaw[] => {
  66. return asyncRouterMap.filter((route) => {
  67. if (type && route.children) {
  68. route.children = filterChildren(route.children, undefined);
  69. }
  70. // Layout ParentView 组件特殊处理
  71. if (route.component?.toString() === 'Layout') {
  72. route.component = Layout;
  73. } else if (route.component?.toString() === 'ParentView') {
  74. route.component = ParentView;
  75. } else if (route.component?.toString() === 'InnerLink') {
  76. route.component = InnerLink;
  77. } else {
  78. route.component = loadView(route.component);
  79. }
  80. if (route.children != null && route.children && route.children.length) {
  81. route.children = filterAsyncRouter(route.children, route, type);
  82. } else {
  83. delete route.children;
  84. delete route.redirect;
  85. }
  86. return true;
  87. });
  88. };
  89. const filterChildren = (childrenMap: RouteRecordRaw[], lastRouter?: RouteRecordRaw): RouteRecordRaw[] => {
  90. let children: RouteRecordRaw[] = [];
  91. childrenMap.forEach((el) => {
  92. if (el.children && el.children.length) {
  93. if (el.component?.toString() === 'ParentView' && !lastRouter) {
  94. el.children.forEach((c) => {
  95. c.path = el.path + '/' + c.path;
  96. if (c.children && c.children.length) {
  97. children = children.concat(filterChildren(c.children, c));
  98. return;
  99. }
  100. children.push(c);
  101. });
  102. return;
  103. }
  104. }
  105. if (lastRouter) {
  106. el.path = lastRouter.path + '/' + el.path;
  107. if (el.children && el.children.length) {
  108. children = children.concat(filterChildren(el.children, el));
  109. return;
  110. }
  111. }
  112. children = children.concat(el);
  113. });
  114. return children;
  115. };
  116. return {
  117. routes,
  118. topbarRouters,
  119. sidebarRouters,
  120. defaultRoutes,
  121. getRoutes,
  122. getSidebarRoutes,
  123. getTopbarRoutes,
  124. setRoutes,
  125. generateRoutes,
  126. setSidebarRouters
  127. };
  128. });
  129. // 动态路由遍历,验证是否具备权限
  130. export const filterDynamicRoutes = (routes: RouteRecordRaw[]) => {
  131. const res: RouteRecordRaw[] = [];
  132. routes.forEach((route) => {
  133. if (route.permissions) {
  134. if (auth.hasPermiOr(route.permissions)) {
  135. res.push(route);
  136. }
  137. } else if (route.roles) {
  138. if (auth.hasRoleOr(route.roles)) {
  139. res.push(route);
  140. }
  141. }
  142. });
  143. return res;
  144. };
  145. export const loadView = (view: any) => {
  146. let res;
  147. for (const path in modules) {
  148. const dir = path.split('views/')[1].split('.vue')[0];
  149. if (dir === view) {
  150. res = () => modules[path]();
  151. }
  152. }
  153. return res;
  154. };
  155. // 非setup
  156. export const usePermissionStoreHook = () => {
  157. return usePermissionStore(store);
  158. };
  159. export default usePermissionStore;