useNav.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { storeToRefs } from "pinia";
  2. import { getConfig } from "@/config";
  3. import { useRouter } from "vue-router";
  4. import { emitter } from "@/utils/mitt";
  5. import Avatar from "@/assets/user.jpg";
  6. import { getTopMenu } from "@/router/utils";
  7. import { useFullscreen } from "@vueuse/core";
  8. import type { routeMetaType } from "../types";
  9. import { transformI18n } from "@/plugins/i18n";
  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 { useGlobal, isAllEmpty } from "@pureadmin/utils";
  15. import { useEpThemeStoreHook } from "@/store/modules/epTheme";
  16. import { usePermissionStoreHook } from "@/store/modules/permission";
  17. import ExitFullscreen from "~icons/ri/fullscreen-exit-fill";
  18. import Fullscreen from "~icons/ri/fullscreen-fill";
  19. const errorInfo =
  20. "The current routing configuration is incorrect, please check the configuration";
  21. export function useNav() {
  22. const pureApp = useAppStoreHook();
  23. const routers = useRouter().options.routes;
  24. const { isFullscreen, toggle } = useFullscreen();
  25. const { wholeMenus } = storeToRefs(usePermissionStoreHook());
  26. /** 平台`layout`中所有`el-tooltip`的`effect`配置,默认`light` */
  27. const tooltipEffect = getConfig()?.TooltipEffect ?? "light";
  28. const getDivStyle = computed((): CSSProperties => {
  29. return {
  30. width: "100%",
  31. display: "flex",
  32. alignItems: "center",
  33. justifyContent: "space-between",
  34. overflow: "hidden"
  35. };
  36. });
  37. /** 头像(如果头像为空则使用 src/assets/user.jpg ) */
  38. const userAvatar = computed(() => {
  39. return isAllEmpty(useUserStoreHook()?.avatar)
  40. ? Avatar
  41. : useUserStoreHook()?.avatar;
  42. });
  43. /** 昵称(如果昵称为空则显示用户名) */
  44. const username = computed(() => {
  45. return isAllEmpty(useUserStoreHook()?.nickname)
  46. ? useUserStoreHook()?.username
  47. : useUserStoreHook()?.nickname;
  48. });
  49. /** 设置国际化选中后的样式 */
  50. const getDropdownItemStyle = computed(() => {
  51. return (locale, t) => {
  52. return {
  53. background: locale === t ? useEpThemeStoreHook().epThemeColor : "",
  54. color: locale === t ? "#f4f4f5" : "#000"
  55. };
  56. };
  57. });
  58. const getDropdownItemClass = computed(() => {
  59. return (locale, t) => {
  60. return locale === t ? "" : "dark:hover:text-primary!";
  61. };
  62. });
  63. const avatarsStyle = computed(() => {
  64. return username.value ? { marginRight: "10px" } : "";
  65. });
  66. const isCollapse = computed(() => {
  67. return !pureApp.getSidebarStatus;
  68. });
  69. const device = computed(() => {
  70. return pureApp.getDevice;
  71. });
  72. const { $storage, $config } = useGlobal<GlobalPropertiesApi>();
  73. const layout = computed(() => {
  74. return $storage?.layout?.layout;
  75. });
  76. const title = computed(() => {
  77. return $config.Title;
  78. });
  79. /** 动态title */
  80. function changeTitle(meta: routeMetaType) {
  81. const Title = getConfig().Title;
  82. if (Title) document.title = `${transformI18n(meta.title)} | ${Title}`;
  83. else document.title = transformI18n(meta.title);
  84. }
  85. /** 退出登录 */
  86. function logout() {
  87. useUserStoreHook().logOut();
  88. }
  89. function backTopMenu() {
  90. router.push(getTopMenu()?.path);
  91. }
  92. function onPanel() {
  93. emitter.emit("openPanel");
  94. }
  95. function toAccountSettings() {
  96. router.push({ name: "AccountSettings" });
  97. }
  98. function toggleSideBar() {
  99. pureApp.toggleSideBar();
  100. }
  101. function handleResize(menuRef) {
  102. menuRef?.handleResize();
  103. }
  104. function resolvePath(route) {
  105. if (!route.children) return console.error(errorInfo);
  106. const httpReg = /^http(s?):\/\//;
  107. const routeChildPath = route.children[0]?.path;
  108. if (httpReg.test(routeChildPath)) {
  109. return route.path + "/" + routeChildPath;
  110. } else {
  111. return routeChildPath;
  112. }
  113. }
  114. function menuSelect(indexPath: string) {
  115. if (wholeMenus.value.length === 0 || isRemaining(indexPath)) return;
  116. emitter.emit("changLayoutRoute", indexPath);
  117. }
  118. /** 判断路径是否参与菜单 */
  119. function isRemaining(path: string) {
  120. return remainingPaths.includes(path);
  121. }
  122. /** 获取`logo` */
  123. function getLogo() {
  124. return new URL("/logo.svg", import.meta.url).href;
  125. }
  126. return {
  127. title,
  128. device,
  129. layout,
  130. logout,
  131. routers,
  132. $storage,
  133. isFullscreen,
  134. Fullscreen,
  135. ExitFullscreen,
  136. toggle,
  137. backTopMenu,
  138. onPanel,
  139. getDivStyle,
  140. changeTitle,
  141. toggleSideBar,
  142. menuSelect,
  143. handleResize,
  144. resolvePath,
  145. getLogo,
  146. isCollapse,
  147. pureApp,
  148. username,
  149. userAvatar,
  150. avatarsStyle,
  151. tooltipEffect,
  152. toAccountSettings,
  153. getDropdownItemStyle,
  154. getDropdownItemClass
  155. };
  156. }