useNav.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.png";
  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.png ) */
  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. return window.vueGlobal.sysCfg.sysCfgView.webTitle;
  79. });
  80. /** 动态title */
  81. function changeTitle(meta: routeMetaType) {
  82. // const Title = getConfig().Title;
  83. // if (Title) document.title = `${transformI18n(meta.title)} | ${Title}`;
  84. // else document.title = transformI18n(meta.title);
  85. }
  86. /** 退出登录 */
  87. function logout() {
  88. useUserStoreHook().logOut();
  89. }
  90. function backTopMenu() {
  91. router.push(getTopMenu()?.path);
  92. }
  93. function onPanel() {
  94. emitter.emit("openPanel");
  95. }
  96. function toAccountSettings() {
  97. router.push({ name: "AccountSettings" });
  98. }
  99. function toggleSideBar() {
  100. pureApp.toggleSideBar();
  101. }
  102. function handleResize(menuRef) {
  103. menuRef?.handleResize();
  104. }
  105. function resolvePath(route) {
  106. if (!route.children) return console.error(errorInfo);
  107. const httpReg = /^http(s?):\/\//;
  108. const routeChildPath = route.children[0]?.path;
  109. if (httpReg.test(routeChildPath)) {
  110. return route.path + "/" + routeChildPath;
  111. } else {
  112. return routeChildPath;
  113. }
  114. }
  115. function menuSelect(indexPath: string) {
  116. if (wholeMenus.value.length === 0 || isRemaining(indexPath)) return;
  117. emitter.emit("changLayoutRoute", indexPath);
  118. }
  119. /** 判断路径是否参与菜单 */
  120. function isRemaining(path: string) {
  121. return remainingPaths.includes(path);
  122. }
  123. /** 获取`logo` */
  124. function getLogo() {
  125. // return new URL("/logo.svg", import.meta.url).href;
  126. return window.vueGlobal.sysCfg.sysCfgView.logoBase64 || new URL("/logo.svg", import.meta.url).href;
  127. }
  128. function getTitle() {
  129. // return $config.Title;
  130. return window.vueGlobal.sysCfg.sysCfgView.webTitle;
  131. };
  132. return {
  133. title,
  134. device,
  135. layout,
  136. logout,
  137. routers,
  138. $storage,
  139. isFullscreen,
  140. Fullscreen,
  141. ExitFullscreen,
  142. toggle,
  143. backTopMenu,
  144. onPanel,
  145. getDivStyle,
  146. changeTitle,
  147. toggleSideBar,
  148. menuSelect,
  149. handleResize,
  150. resolvePath,
  151. getTitle,
  152. getLogo,
  153. isCollapse,
  154. pureApp,
  155. username,
  156. userAvatar,
  157. avatarsStyle,
  158. tooltipEffect,
  159. toAccountSettings,
  160. getDropdownItemStyle,
  161. getDropdownItemClass
  162. };
  163. }