header.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <a-layout-header class="header-layout">
  3. <div class="content-layout">
  4. <div class="content-layout-left">
  5. <h2>
  6. <a :href="mainURL"><img :src="logoPng" /></a>
  7. </h2>
  8. <div class="main_menu">
  9. <router-link to="/" v-slot="{ isActive }">
  10. <span :class="{ active: isActive }">{{ t('room.myRoom') }}</span>
  11. </router-link>
  12. <router-link to="/statistic" v-slot="{ isActive }">
  13. <span :class="{ active: isActive }">{{ t('room.dashboard') }}</span>
  14. </router-link>
  15. </div>
  16. </div>
  17. <div class="right_menu_container">
  18. <a-dropdown placement="bottomRight">
  19. <template #overlay>
  20. <a-menu style="width: 100px" @click="handlerLangClick">
  21. <a-menu-item
  22. v-for="menu in langList"
  23. :key="menu.key"
  24. class="menu-item lang-item"
  25. :class="{ [menu.key]: true }"
  26. >
  27. <img style="height: 10px" :src="menu.icon" />
  28. {{ menu.label }}
  29. </a-menu-item>
  30. </a-menu>
  31. </template>
  32. <div class="avatar" style="margin-right: 30px">
  33. <img style="height: 14px" :src="getImgSrc(currentLang.key)" />
  34. <span>
  35. {{ currentLang.label }}
  36. </span>
  37. </div>
  38. </a-dropdown>
  39. <a-dropdown placement="bottomRight">
  40. <template #overlay>
  41. <a-menu
  42. :style="{
  43. width: isEn ? '140px' : '100px'
  44. }"
  45. @click="handlerMenuClick"
  46. >
  47. <a-menu-item
  48. v-for="menu in menus"
  49. :key="menu.key"
  50. class="menu-item"
  51. :class="{ [menu.key]: true }"
  52. >
  53. {{ t(menu.label) }}
  54. </a-menu-item>
  55. </a-menu>
  56. </template>
  57. <div class="avatar">
  58. <a-avatar :size="32">
  59. <template #icon>
  60. <img :src="userStore.current.avatar" />
  61. </template>
  62. </a-avatar>
  63. <span>
  64. {{ userStore.current.nickname }}
  65. <DownOutlined />
  66. </span>
  67. </div>
  68. </a-dropdown>
  69. </div>
  70. </div>
  71. </a-layout-header>
  72. </template>
  73. <script lang="ts" setup>
  74. import { MenuProps } from 'ant-design-vue'
  75. import { useUserStore } from '@/store/modules/user'
  76. import { postLogout } from '@/api'
  77. import { mainURL } from '@/env'
  78. // import logoPng from '@/assets/images/logo.png'
  79. import { useLocaleStore } from '@/store/modules/locale'
  80. import { useI18n } from '@/hook/useI18n'
  81. import { useLocale } from '@/locales/useLocale'
  82. import { computed, onMounted, ref, unref, watch } from 'vue'
  83. import { LocaleType } from '#/config'
  84. import { showLoading } from '@/components/loading'
  85. import { getImgSrc } from '@/utils/getImgSrc'
  86. import browser from '@/utils/browser'
  87. defineOptions({ name: 'LayoutHeader' })
  88. const userStore = useUserStore()
  89. userStore.fetch()
  90. const { localInfo, getLocale } = useLocaleStore()
  91. const { t } = useI18n()
  92. const currentLang = ref({
  93. key: getLocale,
  94. label: t(`base.lang.${unref(getLocale)}`)
  95. })
  96. const logoPng = computed(() => getImgSrc(`logo_${unref(currentLang).key}`))
  97. // const currentLang = computed(() => {
  98. // return {
  99. // key: getLocale,
  100. // label: t(`base.lang.${unref(getLocale)}`)
  101. // }
  102. // })
  103. const isEn = computed(() => unref(currentLang).key === 'en')
  104. // console.log('getImgSrc', getImgSrc)
  105. const langList = localInfo.availableLocales.map((item: string) => {
  106. return {
  107. key: item,
  108. label: t(`base.lang.${item}`),
  109. icon: getImgSrc(item)
  110. }
  111. })
  112. console.log('langList', langList)
  113. const menus = [
  114. { label: 'base.personalCenter', key: 'user' },
  115. { label: 'base.userGuideline', key: 'guideLine' },
  116. { label: 'base.logout', key: 'logout' }
  117. ]
  118. const handlerMenuClick: MenuProps['onClick'] = async e => {
  119. switch (true) {
  120. case e.key === 'logout':
  121. await postLogout()
  122. location.replace(`${mainURL}/?lang=${currentLang.value.key}`)
  123. break
  124. case e.key === 'user':
  125. location.href = `${mainURL}/#/information?lang=${currentLang.value.key}`
  126. break
  127. case e.key === 'guideLine':
  128. window.open('https://docs.4dkankan.com/#/product/livestream/zh-cn/README', '_blank');
  129. break
  130. }
  131. }
  132. const handlerLangClick: MenuProps['onClick'] = async e => {
  133. console.log(e.key)
  134. showLoading()
  135. const searchParam = new URLSearchParams(location.search)
  136. searchParam.set('lang', `${e.key}`)
  137. const { changeLocale } = useLocale()
  138. changeLocale(e.key as LocaleType)
  139. console.log('searchParam', searchParam.toString())
  140. const replaceUrl =
  141. location.origin + location.pathname + '?' + searchParam.toString()
  142. history.replaceState(null, '', replaceUrl)
  143. setTimeout(() => {
  144. location.reload()
  145. }, 300)
  146. }
  147. onMounted(() => {
  148. const handleChangeLocale = (lang: LocaleType) => {
  149. const { changeLocale } = useLocale()
  150. changeLocale(lang)
  151. }
  152. const setCurrentLang = (lang: LocaleType) => {
  153. currentLang.value.key = lang
  154. currentLang.value.label = t(`base.lang.${unref(lang)}`)
  155. }
  156. const lang = browser.getURLParam('lang')
  157. let defaultLang: LocaleType = 'zh'
  158. console.log('lang', lang)
  159. if (lang) {
  160. defaultLang = lang as LocaleType
  161. handleChangeLocale(lang as LocaleType)
  162. } else {
  163. defaultLang = 'zh'
  164. handleChangeLocale('zh')
  165. }
  166. setCurrentLang(defaultLang)
  167. })
  168. </script>
  169. <style lang="scss">
  170. .menu-item {
  171. text-align: center;
  172. &.logout {
  173. color: #fa7c7c;
  174. }
  175. }
  176. .lang-item {
  177. text-align: left;
  178. vertical-align: middle;
  179. }
  180. </style>
  181. <style lang="scss" scoped>
  182. .header-layout {
  183. background-color: #fff;
  184. display: flex;
  185. padding: 0;
  186. h2 {
  187. padding: 0;
  188. margin: 0;
  189. display: flex;
  190. justify-content: center;
  191. align-items: center;
  192. a {
  193. display: flex;
  194. justify-content: center;
  195. align-items: center;
  196. }
  197. img {
  198. width: 130px;
  199. }
  200. }
  201. }
  202. .right_menu_container {
  203. display: flex;
  204. flex-direction: row;
  205. }
  206. .content-layout {
  207. display: flex;
  208. justify-content: space-between;
  209. align-items: center;
  210. }
  211. .avatar {
  212. display: flex;
  213. align-items: center;
  214. cursor: pointer;
  215. > span {
  216. margin-left: 8px;
  217. }
  218. }
  219. .content-layout-left {
  220. display: flex;
  221. flex-direction: row;
  222. // justify-content: center;
  223. align-items: center;
  224. }
  225. .main_menu {
  226. margin-left: 20px;
  227. a {
  228. text-decoration: none;
  229. color: #323233;
  230. font-size: 16px;
  231. margin: 0 25px;
  232. }
  233. span.active {
  234. color: #0076f6;
  235. position: relative;
  236. &::before {
  237. content: '';
  238. position: absolute;
  239. width: 20px;
  240. height: 4px;
  241. background: #0076f6;
  242. top: 120%;
  243. left: 50%;
  244. transform: translateX(-50%);
  245. }
  246. }
  247. }
  248. </style>