|
@@ -71,10 +71,11 @@ import logoPng from '@/assets/images/logo.png'
|
|
|
import { useLocaleStore } from '@/store/modules/locale'
|
|
|
import { useI18n } from '@/hook/useI18n'
|
|
|
import { useLocale } from '@/locales/useLocale'
|
|
|
-import { computed, unref } from 'vue'
|
|
|
+import { computed, onMounted, ref, unref, watch } from 'vue'
|
|
|
import { LocaleType } from '#/config'
|
|
|
import { showLoading } from '@/components/loading'
|
|
|
import { getImgSrc } from '@/utils/getImgSrc'
|
|
|
+import browser from '@/utils/browser'
|
|
|
// const getImgSrc = inject('getImgSrc')
|
|
|
|
|
|
defineOptions({ name: 'LayoutHeader' })
|
|
@@ -83,15 +84,18 @@ const userStore = useUserStore()
|
|
|
|
|
|
userStore.fetch()
|
|
|
const { localInfo, getLocale } = useLocaleStore()
|
|
|
-
|
|
|
-console.log('localInfo', localInfo)
|
|
|
const { t } = useI18n()
|
|
|
-const currentLang = computed(() => {
|
|
|
- return {
|
|
|
- key: unref(getLocale),
|
|
|
- label: t(`base.lang.${unref(getLocale)}`)
|
|
|
- }
|
|
|
+const currentLang = ref({
|
|
|
+ key: getLocale,
|
|
|
+ label: t(`base.lang.${unref(getLocale)}`)
|
|
|
})
|
|
|
+
|
|
|
+// const currentLang = computed(() => {
|
|
|
+// return {
|
|
|
+// key: getLocale,
|
|
|
+// label: t(`base.lang.${unref(getLocale)}`)
|
|
|
+// }
|
|
|
+// })
|
|
|
const isEn = computed(() => unref(getLocale) === 'en')
|
|
|
// console.log('getImgSrc', getImgSrc)
|
|
|
const langList = localInfo.availableLocales.map((item: string) => {
|
|
@@ -119,14 +123,49 @@ const handlerMenuClick: MenuProps['onClick'] = async e => {
|
|
|
|
|
|
const handlerLangClick: MenuProps['onClick'] = async e => {
|
|
|
console.log(e.key)
|
|
|
- showLoading()
|
|
|
- const { changeLocale } = useLocale()
|
|
|
- changeLocale(e.key as LocaleType)
|
|
|
- // location.reload()
|
|
|
+ // showLoading()
|
|
|
+ const searchParam = new URLSearchParams(location.search)
|
|
|
+ searchParam.set('lang', `${e.key}`)
|
|
|
+ console.log('searchParam', searchParam.toString())
|
|
|
+ const replaceUrl = location.origin + location.pathname + '?' + searchParam.toString();
|
|
|
+ history.replaceState(null, '', replaceUrl);
|
|
|
setTimeout(() => {
|
|
|
location.reload()
|
|
|
}, 100)
|
|
|
}
|
|
|
+
|
|
|
+onMounted(() => {
|
|
|
+ const handleChangeLocale = (lang: LocaleType) => {
|
|
|
+ const { changeLocale } = useLocale()
|
|
|
+ changeLocale(lang)
|
|
|
+ }
|
|
|
+ const setCurrentLang = (lang: LocaleType) => {
|
|
|
+ currentLang.value.key = lang
|
|
|
+ currentLang.value.label = t(`base.lang.${unref(lang)}`)
|
|
|
+ }
|
|
|
+
|
|
|
+ watch(
|
|
|
+ () => getLocale,
|
|
|
+ () => {
|
|
|
+ setCurrentLang(getLocale)
|
|
|
+ },
|
|
|
+ {
|
|
|
+ immediate: true
|
|
|
+ }
|
|
|
+ )
|
|
|
+ const lang = browser.getURLParam('lang')
|
|
|
+ let defaultLang: LocaleType = 'zh'
|
|
|
+ console.log('lang', lang)
|
|
|
+
|
|
|
+ if (lang) {
|
|
|
+ defaultLang = lang as LocaleType
|
|
|
+ handleChangeLocale(lang as LocaleType)
|
|
|
+ } else {
|
|
|
+ defaultLang = 'zh'
|
|
|
+ handleChangeLocale('zh')
|
|
|
+ }
|
|
|
+ setCurrentLang(defaultLang)
|
|
|
+})
|
|
|
</script>
|
|
|
<style lang="scss">
|
|
|
.menu-item {
|