backEnd.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { RouteRecordRaw } from 'vue-router';
  2. import { storeToRefs } from 'pinia';
  3. import pinia from '/@/stores/index';
  4. import { useUserInfo } from '/@/stores/userInfo';
  5. import { useRequestOldRoutes } from '/@/stores/requestOldRoutes';
  6. import { Session } from '/@/utils/storage';
  7. import { NextLoading } from '/@/utils/loading';
  8. import { dynamicRoutes, notFoundAndNoPower } from '/@/router/route';
  9. import { formatTwoStageRoutes, formatFlatteningRoutes, router } from '/@/router/index';
  10. import { useRoutesList } from '/@/stores/routesList';
  11. import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
  12. import { useMenuApi } from '/@/api/menu/index';
  13. import { handleMenu } from '../utils/menu';
  14. import { BtnPermissionStore } from '/@/plugin/permission/store.permission';
  15. import {SystemConfigStore} from "/@/stores/systemConfig";
  16. import {useDeptInfoStore} from "/@/stores/modules/dept";
  17. import {DictionaryStore} from "/@/stores/dictionary";
  18. import {useFrontendMenuStore} from "/@/stores/frontendMenu";
  19. import {toRaw} from "vue";
  20. const menuApi = useMenuApi();
  21. const layouModules: any = import.meta.glob('../layout/routerView/*.{vue,tsx}');
  22. const viewsModules: any = import.meta.glob('../views/**/*.{vue,tsx}');
  23. /**
  24. * 获取目录下的 .vue、.tsx 全部文件
  25. * @method import.meta.glob
  26. * @link 参考:https://cn.vitejs.dev/guide/features.html#json
  27. */
  28. const dynamicViewsModules: Record<string, Function> = Object.assign({}, { ...layouModules }, { ...viewsModules });
  29. /**
  30. * 后端控制路由:初始化方法,防止刷新时路由丢失
  31. * @method NextLoading 界面 loading 动画开始执行
  32. * @method useUserInfo().setUserInfos() 触发初始化用户信息 pinia
  33. * @method useRequestOldRoutes().setRequestOldRoutes() 存储接口原始路由(未处理component),根据需求选择使用
  34. * @method setAddRoute 添加动态路由
  35. * @method setFilterMenuAndCacheTagsViewRoutes 设置路由到 vuex routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  36. */
  37. export async function initBackEndControlRoutes() {
  38. // 界面 loading 动画开始执行
  39. if (window.nextLoading === undefined) NextLoading.start();
  40. // 无 token 停止执行下一步
  41. if (!Session.get('token')) return false;
  42. // 触发初始化用户信息 pinia
  43. // https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
  44. await useUserInfo().setUserInfos();
  45. // 获取路由菜单数据
  46. const res = await getBackEndControlRoutes();
  47. // 无登录权限时,添加判断
  48. // https://gitee.com/lyt-top/vue-next-admin/issues/I64HVO
  49. // if (res.data.length <= 0) return Promise.resolve(true);
  50. // 处理路由(component),替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
  51. const {frameIn,frameOut} = handleMenu(res.data)
  52. dynamicRoutes[0].children = await backEndComponent(frameIn);
  53. // 添加动态路由
  54. await setAddRoute();
  55. // 设置路由到 vuex routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  56. await setFilterMenuAndCacheTagsViewRoutes();
  57. }
  58. export async function setRouters(){
  59. const {frameInRoutes,frameOutRoutes} = await useFrontendMenuStore().getRouter()
  60. const frameInRouter = toRaw(frameInRoutes)
  61. const frameOutRouter = toRaw(frameOutRoutes)
  62. dynamicRoutes[0].children = frameInRouter
  63. dynamicRoutes.forEach((item:any)=>{
  64. router.addRoute(item)
  65. })
  66. frameOutRouter.forEach((item:any)=>{
  67. router.addRoute(item)
  68. })
  69. const storesRoutesList = useRoutesList(pinia);
  70. storesRoutesList.setRoutesList([...dynamicRoutes[0].children,...frameOutRouter]);
  71. const storesTagsView = useTagsViewRoutes(pinia);
  72. storesTagsView.setTagsViewRoutes([...dynamicRoutes[0].children,...frameOutRouter])
  73. }
  74. /**
  75. * 设置路由到 vuex routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  76. * @description 用于左侧菜单、横向菜单的显示
  77. * @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
  78. */
  79. export function setFilterMenuAndCacheTagsViewRoutes() {
  80. const storesRoutesList = useRoutesList(pinia);
  81. storesRoutesList.setRoutesList(dynamicRoutes[0].children as any);
  82. setCacheTagsViewRoutes();
  83. }
  84. /**
  85. * 缓存多级嵌套数组处理后的一维数组
  86. * @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
  87. */
  88. export function setCacheTagsViewRoutes() {
  89. const storesTagsView = useTagsViewRoutes(pinia);
  90. storesTagsView.setTagsViewRoutes(formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes))[0].children);
  91. }
  92. /**
  93. * 处理路由格式及添加捕获所有路由或 404 Not found 路由
  94. * @description 替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
  95. * @returns 返回替换后的路由数组
  96. */
  97. export function setFilterRouteEnd() {
  98. let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes));
  99. // notFoundAndNoPower 防止 404、401 不在 layout 布局中,不设置的话,404、401 界面将全屏显示
  100. // 关联问题 No match found for location with path 'xxx'
  101. filterRouteEnd[0].children = [...filterRouteEnd[0].children, ...notFoundAndNoPower];
  102. return filterRouteEnd;
  103. }
  104. /**
  105. * 添加动态路由
  106. * @method router.addRoute
  107. * @description 此处循环为 dynamicRoutes(/@/router/route)第一个顶级 children 的路由一维数组,非多级嵌套
  108. * @link 参考:https://next.router.vuejs.org/zh/api/#addroute
  109. */
  110. export async function setAddRoute() {
  111. await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
  112. router.addRoute(route);
  113. });
  114. }
  115. /**
  116. * 请求后端路由菜单接口
  117. * @description isRequestRoutes 为 true,则开启后端控制路由
  118. * @returns 返回后端路由菜单数据
  119. */
  120. export function getBackEndControlRoutes() {
  121. //获取所有的按钮权限
  122. BtnPermissionStore().getBtnPermissionStore();
  123. // 获取系统配置
  124. SystemConfigStore().getSystemConfigs()
  125. // 获取所有部门信息
  126. useDeptInfoStore().requestDeptInfo()
  127. // 获取字典信息
  128. DictionaryStore().getSystemDictionarys()
  129. return menuApi.getSystemMenu();
  130. }
  131. /**
  132. * 重新请求后端路由菜单接口
  133. * @description 用于菜单管理界面刷新菜单(未进行测试)
  134. * @description 路径:/src/views/system/menu/component/addMenu.vue
  135. */
  136. export function setBackEndControlRefreshRoutes() {
  137. getBackEndControlRoutes();
  138. }
  139. /**
  140. * 后端路由 component 转换
  141. * @param routes 后端返回的路由表数组
  142. * @returns 返回处理成函数后的 component
  143. */
  144. export function backEndComponent(routes: any) {
  145. if (!routes) return;
  146. return routes.map((item: any) => {
  147. if (item.component) item.component = dynamicImport(dynamicViewsModules, item.component as string);
  148. if(item.is_catalog){
  149. // 对目录的处理
  150. item.component = dynamicImport(dynamicViewsModules, 'layout/routerView/parent')
  151. }
  152. if(item.is_link){
  153. // 对外链接的处理
  154. if(item.is_iframe){
  155. item.component = dynamicImport(dynamicViewsModules, 'layout/routerView/iframes')
  156. }else {
  157. item.component = dynamicImport(dynamicViewsModules, 'layout/routerView/link')
  158. }
  159. }else{
  160. if(item.is_iframe){
  161. // const iframeRoute:RouteRecordRaw = {
  162. // ...item
  163. // }
  164. // router.addRoute(iframeRoute)
  165. item.meta.isLink = item.link_url
  166. // item.path = `${item.path}Link`
  167. // item.name = `${item.name}Link`
  168. // item.meta.isIframe = item.is_iframe
  169. // item.meta.isKeepAlive = false
  170. // item.meta.isIframeOpen = true
  171. item.component = dynamicImport(dynamicViewsModules, 'layout/routerView/link.vue')
  172. }
  173. }
  174. item.children && backEndComponent(item.children);
  175. return item;
  176. });
  177. }
  178. /**
  179. * 后端路由 component 转换函数
  180. * @param dynamicViewsModules 获取目录下的 .vue、.tsx 全部文件
  181. * @param component 当前要处理项 component
  182. * @returns 返回处理成函数后的 component
  183. */
  184. export function dynamicImport(dynamicViewsModules: Record<string, Function>, component: string) {
  185. const keys = Object.keys(dynamicViewsModules);
  186. const matchKeys = keys.filter((key) => {
  187. const k = key.replace(/..\/views|../, '');
  188. return k.startsWith(`${component}`) || k.startsWith(`/${component}`);
  189. });
  190. if (matchKeys?.length === 1) {
  191. const matchKey = matchKeys[0];
  192. return dynamicViewsModules[matchKey];
  193. }
  194. if (matchKeys?.length > 1) {
  195. return false;
  196. }
  197. }