Navbar.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <div class="navbar">
  3. <Hamburger
  4. :is-active="pureApp.sidebar.opened"
  5. class="hamburger-container"
  6. @toggleClick="toggleSideBar"
  7. />
  8. <Breadcrumb class="breadcrumb-container" />
  9. <div class="right-menu">
  10. <!-- 全屏 -->
  11. <screenfull v-show="!deviceDetection()" />
  12. <!-- 国际化 -->
  13. <div
  14. v-show="!deviceDetection()"
  15. class="inter"
  16. :title="currentLocale ? '中文' : 'English'"
  17. @click="toggleLang"
  18. >
  19. <img :src="currentLocale ? ch : en" />
  20. </div>
  21. <i
  22. class="el-icon-setting hsset"
  23. :title="$t('message.hssystemSet')"
  24. @click="onPanel"
  25. ></i>
  26. <!-- 退出登陆 -->
  27. <el-dropdown trigger="click">
  28. <span class="el-dropdown-link">
  29. <img :src="favicon" />
  30. <p>{{ usename }}</p>
  31. </span>
  32. <template #dropdown>
  33. <el-dropdown-menu>
  34. <el-dropdown-item icon="el-icon-switch-button" @click="logout">{{
  35. $t("message.hsLoginOut")
  36. }}</el-dropdown-item>
  37. </el-dropdown-menu>
  38. </template>
  39. </el-dropdown>
  40. </div>
  41. </div>
  42. </template>
  43. <script lang="ts">
  44. import {
  45. defineComponent,
  46. onMounted,
  47. unref,
  48. watch,
  49. getCurrentInstance
  50. } from "vue";
  51. import Breadcrumb from "/@/components/ReBreadCrumb";
  52. import Hamburger from "/@/components/ReHamBurger";
  53. import screenfull from "../components/screenfull/index.vue";
  54. import { useRouter, useRoute } from "vue-router";
  55. import { useAppStoreHook } from "/@/store/modules/app";
  56. import { storageSession } from "/@/utils/storage";
  57. import ch from "/@/assets/ch.png";
  58. import en from "/@/assets/en.png";
  59. import favicon from "/favicon.ico";
  60. import { emitter } from "/@/utils/mitt";
  61. import { deviceDetection } from "/@/utils/deviceDetection";
  62. import { useI18n } from "vue-i18n";
  63. let routerArrays: Array<object> = [
  64. {
  65. path: "/welcome",
  66. meta: {
  67. title: "message.hshome",
  68. icon: "el-icon-s-home",
  69. showLink: true,
  70. savedPosition: false
  71. }
  72. }
  73. ];
  74. export default defineComponent({
  75. name: "Navbar",
  76. components: {
  77. Breadcrumb,
  78. Hamburger,
  79. screenfull
  80. },
  81. // @ts-ignore
  82. computed: {
  83. // eslint-disable-next-line vue/return-in-computed-property
  84. currentLocale() {
  85. if (
  86. !this.$storage.routesInStorage ||
  87. this.$storage.routesInStorage.length === 0
  88. ) {
  89. // eslint-disable-next-line vue/no-side-effects-in-computed-properties
  90. this.$storage.routesInStorage = routerArrays;
  91. }
  92. if (!this.$storage.locale) {
  93. // eslint-disable-next-line
  94. this.$storage.locale = { locale: "zh" };
  95. useI18n().locale.value = "zh";
  96. }
  97. switch (this.$storage.locale?.locale) {
  98. case "zh":
  99. return true;
  100. case "en":
  101. return false;
  102. }
  103. }
  104. },
  105. setup() {
  106. const instance =
  107. getCurrentInstance().appContext.config.globalProperties.$storage;
  108. const pureApp = useAppStoreHook();
  109. const router = useRouter();
  110. const route = useRoute();
  111. let usename = storageSession.getItem("info")?.username;
  112. const { locale, t } = useI18n();
  113. // 国际化语言切换
  114. const toggleLang = (): void => {
  115. switch (instance.locale.locale) {
  116. case "zh":
  117. instance.locale = { locale: "en" };
  118. locale.value = "en";
  119. break;
  120. case "en":
  121. instance.locale = { locale: "zh" };
  122. locale.value = "zh";
  123. break;
  124. }
  125. };
  126. watch(
  127. () => locale.value,
  128. () => {
  129. //@ts-ignore
  130. document.title = t(unref(route.meta.title)); // 动态title
  131. }
  132. );
  133. // 退出登录
  134. const logout = (): void => {
  135. storageSession.removeItem("info");
  136. router.push("/login");
  137. };
  138. function onPanel() {
  139. emitter.emit("openPanel");
  140. }
  141. function toggleSideBar() {
  142. pureApp.toggleSideBar();
  143. }
  144. onMounted(() => {
  145. document
  146. .querySelector(".el-dropdown__popper")
  147. ?.setAttribute("class", "resetTop");
  148. document
  149. .querySelector(".el-popper__arrow")
  150. ?.setAttribute("class", "hidden");
  151. });
  152. return {
  153. pureApp,
  154. toggleSideBar,
  155. usename,
  156. toggleLang,
  157. logout,
  158. ch,
  159. en,
  160. favicon,
  161. onPanel,
  162. deviceDetection,
  163. locale,
  164. t
  165. };
  166. }
  167. });
  168. </script>
  169. <style lang="scss" scoped>
  170. .navbar {
  171. width: 100%;
  172. height: 50px;
  173. overflow: hidden;
  174. background: #fff;
  175. box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  176. .hamburger-container {
  177. line-height: 46px;
  178. height: 100%;
  179. float: left;
  180. cursor: pointer;
  181. transition: background 0.3s;
  182. -webkit-tap-highlight-color: transparent;
  183. &:hover {
  184. background: rgba(0, 0, 0, 0.025);
  185. }
  186. }
  187. .breadcrumb-container {
  188. float: left;
  189. }
  190. .right-menu {
  191. position: absolute;
  192. right: 0;
  193. display: flex;
  194. align-items: center;
  195. height: 48px;
  196. line-height: 48px;
  197. .inter {
  198. width: 40px;
  199. height: 48px;
  200. display: flex;
  201. align-items: center;
  202. justify-content: space-around;
  203. &:hover {
  204. cursor: pointer;
  205. background: #f0f0f0;
  206. }
  207. img {
  208. width: 25px;
  209. }
  210. }
  211. .hsset {
  212. width: 40px;
  213. height: 48px;
  214. display: flex;
  215. align-items: center;
  216. justify-content: space-around;
  217. margin-right: 5px;
  218. &:hover {
  219. cursor: pointer;
  220. background: #f0f0f0;
  221. }
  222. }
  223. .el-dropdown-link {
  224. width: 70px;
  225. display: flex;
  226. align-items: center;
  227. justify-content: space-around;
  228. margin-right: 10px;
  229. cursor: pointer;
  230. p {
  231. font-size: 14px;
  232. }
  233. img {
  234. width: 22px;
  235. height: 22px;
  236. }
  237. }
  238. }
  239. }
  240. // single element-plus reset
  241. .el-dropdown-menu__item {
  242. padding: 0 10px;
  243. }
  244. .el-dropdown-menu {
  245. padding: 6px 0;
  246. }
  247. .el-dropdown-menu__item:focus,
  248. .el-dropdown-menu__item:not(.is-disabled):hover {
  249. color: #606266;
  250. background: #f0f0f0;
  251. }
  252. </style>