header.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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="/">
  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 v-if="!browser.isMobile" type="primary" ghost>{{
  13. locale === 'zh' ? $t('zh') : $t('en')
  14. }}</n-button>
  15. <n-text v-else type="primary" ghost>{{ locale === 'zh' ? $t('zh') : $t('en') }}</n-text>
  16. </n-dropdown>
  17. </div>
  18. </div>
  19. <div class="search-box w-full flex justify-center items-center flex-col">
  20. <n-h1 class="help-text font-size-[48px] font-500">{{ $t('helperTip') }}</n-h1>
  21. <n-auto-complete
  22. v-model:value="keySearch"
  23. :options="searchOptions"
  24. class="search-box max-w-[640px]"
  25. size="large"
  26. round
  27. :placeholder="$t('enter_key')"
  28. blur-after-select
  29. @select="(index: number) => handleAutoSelect(index, searchOptions)"
  30. @keydown.enter="handleSearch"
  31. >
  32. <template #prefix>
  33. <n-icon :component="SearchOutline" />
  34. </template>
  35. </n-auto-complete>
  36. </div>
  37. </div>
  38. </template>
  39. <script setup lang="ts">
  40. import { NH1, NIcon, NDropdown, NAutoComplete, NButton } from 'naive-ui'
  41. import { SearchOutline } from '@vicons/ionicons5'
  42. import { computedAsync } from '@vueuse/core'
  43. import { getArticleSearch } from '@/api'
  44. import browser from '@/utils/browser'
  45. const { t, locale } = useI18n()
  46. const keySearch = ref('')
  47. const router = useRouter()
  48. const route = useRoute()
  49. const options = ref([
  50. {
  51. label: t('zh'),
  52. key: 'zh',
  53. },
  54. {
  55. label: t('en'),
  56. key: 'en',
  57. },
  58. ])
  59. const searchOptions = computedAsync(
  60. async () => {
  61. if (keySearch.value) {
  62. const list = await getArticleSearch(keySearch.value)
  63. console.log('list', list.data)
  64. return Array.from(list.data || []).map((suffix) => {
  65. return {
  66. label: suffix.title,
  67. value: suffix.id,
  68. }
  69. })
  70. }
  71. },
  72. null, // initial state
  73. )
  74. const handleSelect = (key: string) => {
  75. locale.value = key
  76. localStorage.setItem('locale', key)
  77. router.replace({
  78. name: route.name,
  79. query: {
  80. lang: key,
  81. },
  82. })
  83. setTimeout(() => {
  84. location.reload()
  85. }, 50)
  86. }
  87. onMounted(() => {
  88. let localeValue = localStorage.getItem('locale') || 'zh'
  89. if (route.query.lang) {
  90. localeValue = String(route.query.lang)
  91. locale.value = localeValue
  92. localStorage.setItem('locale', localeValue)
  93. }
  94. locale.value = String(localeValue)
  95. document.title = t('web_title')
  96. console.log('route', route)
  97. // route.query.lang = localeValue
  98. router.replace({
  99. name: route.name,
  100. query: {
  101. lang: localeValue,
  102. },
  103. })
  104. })
  105. const handleAutoSelect = (index: number, list: any[]) => {
  106. const item = list.find((it) => it.value === index)
  107. // debugger;
  108. console.log('handleAutoUpdate', item)
  109. router.push({ path: `/showdoc/${item.value}` })
  110. }
  111. const handleSearch = () => {
  112. console.log('keySearch', keySearch.value)
  113. router.push({ path: '/search', query: { key: keySearch.value } })
  114. }
  115. </script>
  116. <style lang="scss" scoped>
  117. .header {
  118. background-image: url('@/assets/img/banner_bg1.png');
  119. background-repeat: no-repeat;
  120. background-size: cover;
  121. background-position: top center;
  122. }
  123. .search-box .n-input {
  124. --n-border-radius: 10px !important;
  125. }
  126. [is-mobile] {
  127. .header {
  128. padding: 0 0.4267rem;
  129. box-sizing: border-box;
  130. min-height: 1.3333rem;
  131. width: 100%;
  132. height: 7.7333rem;
  133. background-image: url('@/assets/img/mobile/banner_bg1.png');
  134. .logo {
  135. img {
  136. width: 2.2rem;
  137. height: 0.5867rem;
  138. }
  139. }
  140. .menu {
  141. n-text {
  142. font-size: 0.3733rem;
  143. }
  144. }
  145. }
  146. .search-box {
  147. height: 1.0667rem;
  148. border-radius: 0.5333rem;
  149. .help-text {
  150. font-weight: bold;
  151. font-size: 0.5333rem;
  152. color: #202020;
  153. line-height: 0.6267rem;
  154. margin-bottom: 0.76rem;
  155. }
  156. }
  157. }
  158. </style>