header.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <div class="header grid min-h-sm w-full flex flex-col">
  3. <div class="w-full h-[100px] flex max-w-screen-xl my-0 mx-auto justify-between items-center">
  4. <div class="logo">
  5. <a href="https://www.4dkankan.com/#/" target="_blank">
  6. <img v-if="locale==='zh'" src="@/assets/img/logo_4dge_cn.png" alt="logo" />
  7. <img v-if="locale==='en'" src="@/assets/img/logo_4dge_en.png" alt="logo" />
  8. </a>
  9. </div>
  10. <div class="menu">
  11. <n-dropdown :options="options" @select="handleSelect">
  12. <n-button type="primary" ghost>{{ locale === 'zh' ? $t('zh') : $t('en') }}</n-button>
  13. </n-dropdown>
  14. </div>
  15. </div>
  16. <div class="search-box w-full flex justify-center items-center flex-col">
  17. <n-h1 class="font-size-[48px] font-500">{{ $t('helperTip') }}</n-h1>
  18. <n-auto-complete v-model:value="keySearch" :options="searchOptions" class="search-box max-w-[640px]" size="large"
  19. round :placeholder="$t('enter_key')" blur-after-select
  20. @select="(index: number) => handleAutoSelect(index, searchOptions)" @keydown.enter="handleSearch">
  21. <template #prefix>
  22. <n-icon :component="SearchOutline" />
  23. </template>
  24. </n-auto-complete>
  25. </div>
  26. </div>
  27. </template>
  28. <script setup lang="ts">
  29. import { NH1, NIcon, NDropdown, NAutoComplete, NButton } from 'naive-ui'
  30. import { SearchOutline } from '@vicons/ionicons5'
  31. import { computedAsync } from '@vueuse/core'
  32. import { getArticleSearch } from '@/api'
  33. const { t, locale } = useI18n()
  34. const keySearch = ref('')
  35. const router = useRouter()
  36. const route = useRoute()
  37. const options = ref([
  38. {
  39. label: t('zh'),
  40. key: 'zh',
  41. },
  42. {
  43. label: t('en'),
  44. key: 'en',
  45. },
  46. ])
  47. const searchOptions = computedAsync(
  48. async () => {
  49. if (keySearch.value) {
  50. const list = await getArticleSearch(keySearch.value)
  51. console.log('list', list.data)
  52. return Array.from(list.data || []).map((suffix) => {
  53. return {
  54. label: suffix.title,
  55. value: suffix.id
  56. }
  57. })
  58. }
  59. },
  60. null, // initial state
  61. )
  62. const handleSelect = (key: string) => {
  63. locale.value = key
  64. localStorage.setItem('locale', key)
  65. router.replace({
  66. name: route.name,
  67. query: {
  68. lang: key
  69. }
  70. })
  71. setTimeout(() => {
  72. location.reload();
  73. }, 50)
  74. }
  75. onMounted(() => {
  76. let localeValue = localStorage.getItem('locale') || 'zh'
  77. if (route.query.lang) {
  78. localeValue = String(route.query.lang)
  79. locale.value = localeValue
  80. localStorage.setItem('locale', localeValue)
  81. }
  82. locale.value = String(localeValue)
  83. document.title = t('web_title')
  84. console.log('route', route)
  85. // route.query.lang = localeValue
  86. router.replace({
  87. name: route.name,
  88. query: {
  89. lang: localeValue
  90. }
  91. })
  92. })
  93. const handleAutoSelect = (index: number, list: any[]) => {
  94. const item = list.find((it) => it.value === index)
  95. // debugger;
  96. console.log('handleAutoUpdate', item)
  97. router.push({ path: `/showdoc/${item.value}` })
  98. }
  99. const handleSearch = () => {
  100. console.log('keySearch', keySearch.value)
  101. router.push({ path: '/search', query: { key: keySearch.value } })
  102. }
  103. </script>
  104. <style>
  105. .header {
  106. background-image: url('@/assets/img/banner_bg1.png');
  107. background-repeat: no-repeat;
  108. background-size: cover;
  109. background-position: top center;
  110. }
  111. .search-box .n-input {
  112. --n-border-radius: 10px !important;
  113. }
  114. </style>