horizontal.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <script setup lang="ts">
  2. import {
  3. computed,
  4. unref,
  5. watch,
  6. nextTick,
  7. onMounted,
  8. getCurrentInstance
  9. } from "vue";
  10. import { useI18n } from "vue-i18n";
  11. import { emitter } from "/@/utils/mitt";
  12. import { templateRef } from "@vueuse/core";
  13. import SidebarItem from "./sidebarItem.vue";
  14. import { algorithm } from "/@/utils/algorithm";
  15. import screenfull from "../screenfull/index.vue";
  16. import { useRoute, useRouter } from "vue-router";
  17. import { storageSession } from "/@/utils/storage";
  18. import { deviceDetection } from "/@/utils/deviceDetection";
  19. import globalization from "/@/assets/svg/globalization.svg";
  20. import { usePermissionStoreHook } from "/@/store/modules/permission";
  21. const instance =
  22. getCurrentInstance().appContext.config.globalProperties.$storage;
  23. const title =
  24. getCurrentInstance().appContext.config.globalProperties.$config?.Title;
  25. const menuRef = templateRef<ElRef | null>("menu", null);
  26. const routeStore = usePermissionStoreHook();
  27. const route = useRoute();
  28. const router = useRouter();
  29. const routers = useRouter().options.routes;
  30. let usename = storageSession.getItem("info")?.username;
  31. const { locale, t } = useI18n();
  32. watch(
  33. () => locale.value,
  34. () => {
  35. //@ts-ignore
  36. // 动态title
  37. document.title = t(unref(route.meta.title));
  38. }
  39. );
  40. // 退出登录
  41. const logout = (): void => {
  42. storageSession.removeItem("info");
  43. router.push("/login");
  44. };
  45. function onPanel() {
  46. emitter.emit("openPanel");
  47. }
  48. const activeMenu = computed((): string => {
  49. const { meta, path } = route;
  50. if (meta.activeMenu) {
  51. // @ts-ignore
  52. return meta.activeMenu;
  53. }
  54. return path;
  55. });
  56. const menuSelect = (indexPath: string): void => {
  57. let parentPath = "";
  58. let parentPathIndex = indexPath.lastIndexOf("/");
  59. if (parentPathIndex > 0) {
  60. parentPath = indexPath.slice(0, parentPathIndex);
  61. }
  62. // 找到当前路由的信息
  63. function findCurrentRoute(routes) {
  64. return routes.map(item => {
  65. if (item.path === indexPath) {
  66. // 切换左侧菜单 通知标签页
  67. emitter.emit("changLayoutRoute", {
  68. indexPath,
  69. parentPath
  70. });
  71. } else {
  72. if (item.children) findCurrentRoute(item.children);
  73. }
  74. });
  75. }
  76. findCurrentRoute(algorithm.increaseIndexes(routers));
  77. };
  78. function backHome() {
  79. router.push("/welcome");
  80. }
  81. function handleResize() {
  82. // @ts-ignore
  83. menuRef.value.handleResize();
  84. }
  85. // 简体中文
  86. function translationCh() {
  87. instance.locale = { locale: "zh" };
  88. locale.value = "zh";
  89. handleResize();
  90. }
  91. // English
  92. function translationEn() {
  93. instance.locale = { locale: "en" };
  94. locale.value = "en";
  95. handleResize();
  96. }
  97. onMounted(() => {
  98. nextTick(() => {
  99. handleResize();
  100. });
  101. });
  102. </script>
  103. <template>
  104. <div class="horizontal-header">
  105. <div class="horizontal-header-left" @click="backHome">
  106. <i class="fa fa-optin-monster"></i>
  107. <h4>{{ title }}</h4>
  108. </div>
  109. <el-menu
  110. ref="menu"
  111. :default-active="activeMenu"
  112. unique-opened
  113. router
  114. class="horizontal-header-menu"
  115. mode="horizontal"
  116. @select="menuSelect"
  117. >
  118. <sidebar-item
  119. v-for="route in routeStore.wholeRoutes"
  120. :key="route.path"
  121. :item="route"
  122. :base-path="route.path"
  123. />
  124. </el-menu>
  125. <div class="horizontal-header-right">
  126. <!-- 全屏 -->
  127. <screenfull v-show="!deviceDetection()" />
  128. <!-- 国际化 -->
  129. <el-dropdown trigger="click">
  130. <globalization />
  131. <template #dropdown>
  132. <el-dropdown-menu class="translation">
  133. <el-dropdown-item
  134. :style="{
  135. background: locale === 'zh' ? '#1b2a47' : '',
  136. color: locale === 'zh' ? '#f4f4f5' : '#000'
  137. }"
  138. @click="translationCh"
  139. >简体中文</el-dropdown-item
  140. >
  141. <el-dropdown-item
  142. :style="{
  143. background: locale === 'en' ? '#1b2a47' : '',
  144. color: locale === 'en' ? '#f4f4f5' : '#000'
  145. }"
  146. @click="translationEn"
  147. >English</el-dropdown-item
  148. >
  149. </el-dropdown-menu>
  150. </template>
  151. </el-dropdown>
  152. <!-- 退出登陆 -->
  153. <el-dropdown trigger="click">
  154. <span class="el-dropdown-link">
  155. <img
  156. src="https://avatars.githubusercontent.com/u/44761321?s=400&u=30907819abd29bb3779bc247910873e7c7f7c12f&v=4"
  157. />
  158. <p>{{ usename }}</p>
  159. </span>
  160. <template #dropdown>
  161. <el-dropdown-menu class="logout">
  162. <el-dropdown-item icon="el-icon-switch-button" @click="logout">{{
  163. $t("message.hsLoginOut")
  164. }}</el-dropdown-item>
  165. </el-dropdown-menu>
  166. </template>
  167. </el-dropdown>
  168. <i
  169. class="el-icon-setting"
  170. :title="$t('message.hssystemSet')"
  171. @click="onPanel"
  172. ></i>
  173. </div>
  174. </div>
  175. </template>
  176. <style lang="scss" scoped>
  177. .translation {
  178. .el-dropdown-menu__item {
  179. padding: 0 40px !important;
  180. }
  181. .el-dropdown-menu__item:focus,
  182. .el-dropdown-menu__item:not(.is-disabled):hover {
  183. color: #606266;
  184. background: #f0f0f0;
  185. }
  186. }
  187. .logout {
  188. .el-dropdown-menu__item {
  189. padding: 0 18px !important;
  190. }
  191. .el-dropdown-menu__item:focus,
  192. .el-dropdown-menu__item:not(.is-disabled):hover {
  193. color: #606266;
  194. background: #f0f0f0;
  195. }
  196. }
  197. </style>