useNav.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { storeToRefs } from "pinia";
  2. import { getConfig } from "@/config";
  3. import { emitter } from "@/utils/mitt";
  4. import userAvatar from "@/assets/user.jpg";
  5. import { getTopMenu } from "@/router/utils";
  6. import { useFullscreen } from "@vueuse/core";
  7. import { useGlobal } from "@pureadmin/utils";
  8. import type { routeMetaType } from "../types";
  9. import { useRouter, useRoute } from "vue-router";
  10. import { router, remainingPaths } from "@/router";
  11. import { computed, type CSSProperties } from "vue";
  12. import { useAppStoreHook } from "@/store/modules/app";
  13. import { useUserStoreHook } from "@/store/modules/user";
  14. import { usePermissionStoreHook } from "@/store/modules/permission";
  15. import ExitFullscreen from "@iconify-icons/ri/fullscreen-exit-fill";
  16. import Fullscreen from "@iconify-icons/ri/fullscreen-fill";
  17. const errorInfo = "当前路由配置不正确,请检查配置";
  18. export function useNav() {
  19. const route = useRoute();
  20. const pureApp = useAppStoreHook();
  21. const routers = useRouter().options.routes;
  22. const { isFullscreen, toggle } = useFullscreen();
  23. const { wholeMenus } = storeToRefs(usePermissionStoreHook());
  24. /** 平台`layout`中所有`el-tooltip`的`effect`配置,默认`light` */
  25. const tooltipEffect = getConfig()?.TooltipEffect ?? "light";
  26. const getDivStyle = computed((): CSSProperties => {
  27. return {
  28. width: "100%",
  29. display: "flex",
  30. alignItems: "center",
  31. justifyContent: "space-between",
  32. overflow: "hidden"
  33. };
  34. });
  35. /** 用户名 */
  36. const username = computed(() => {
  37. return useUserStoreHook()?.username;
  38. });
  39. const avatarsStyle = computed(() => {
  40. return username.value ? { marginRight: "10px" } : "";
  41. });
  42. const isCollapse = computed(() => {
  43. return !pureApp.getSidebarStatus;
  44. });
  45. const device = computed(() => {
  46. return pureApp.getDevice;
  47. });
  48. const { $storage, $config } = useGlobal<GlobalPropertiesApi>();
  49. const layout = computed(() => {
  50. return $storage?.layout?.layout;
  51. });
  52. const title = computed(() => {
  53. return $config.Title;
  54. });
  55. /** 动态title */
  56. function changeTitle(meta: routeMetaType) {
  57. const Title = getConfig().Title;
  58. if (Title) document.title = `${meta.title} | ${Title}`;
  59. else document.title = meta.title;
  60. }
  61. /** 退出登录 */
  62. function logout() {
  63. useUserStoreHook().logOut();
  64. }
  65. function backTopMenu() {
  66. router.push(getTopMenu()?.path);
  67. }
  68. function onPanel() {
  69. emitter.emit("openPanel");
  70. }
  71. function toggleSideBar() {
  72. pureApp.toggleSideBar();
  73. }
  74. function handleResize(menuRef) {
  75. menuRef?.handleResize();
  76. }
  77. function resolvePath(route) {
  78. if (!route.children) return console.error(errorInfo);
  79. const httpReg = /^http(s?):\/\//;
  80. const routeChildPath = route.children[0]?.path;
  81. if (httpReg.test(routeChildPath)) {
  82. return route.path + "/" + routeChildPath;
  83. } else {
  84. return routeChildPath;
  85. }
  86. }
  87. function menuSelect(indexPath: string) {
  88. if (wholeMenus.value.length === 0 || isRemaining(indexPath)) return;
  89. emitter.emit("changLayoutRoute", indexPath);
  90. }
  91. /** 判断路径是否参与菜单 */
  92. function isRemaining(path: string) {
  93. return remainingPaths.includes(path);
  94. }
  95. /** 获取`logo` */
  96. function getLogo() {
  97. // return new URL("/logo.svg", import.meta.url).href;
  98. return new URL("/logo.png", import.meta.url).href;
  99. }
  100. return {
  101. route,
  102. title,
  103. device,
  104. layout,
  105. logout,
  106. routers,
  107. $storage,
  108. isFullscreen,
  109. Fullscreen,
  110. ExitFullscreen,
  111. toggle,
  112. backTopMenu,
  113. onPanel,
  114. getDivStyle,
  115. changeTitle,
  116. toggleSideBar,
  117. menuSelect,
  118. handleResize,
  119. resolvePath,
  120. getLogo,
  121. isCollapse,
  122. pureApp,
  123. username,
  124. userAvatar,
  125. avatarsStyle,
  126. tooltipEffect
  127. };
  128. }