header.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <div class="header_nav">
  3. <nav class="w1000 df jcsb aic">
  4. <!-- 网站title -->
  5. <h1 class="game_logo df aic" @click="clearAct">
  6. <a class="logo" href="/p_index" alt="朱雀游戏官网" title="朱雀游戏官网">朱雀游戏中心</a>
  7. </h1>
  8. <!-- 首页、分类、排行 -->
  9. <router-link v-for="(item, index) in routes" :key="index" class="nav_title" :to="item.path"
  10. @click="changeIndex(item.title)" :class="{ active: currentIndex === item.title }">
  11. {{ item.title }}
  12. </router-link>
  13. <!-- 搜索框 -->
  14. <el-autocomplete v-model="game_name" :fetch-suggestions="searchChange" placeholder="请输入游戏名称" :prefix-icon="Search"
  15. clearable @select="handleSelect" @keyup.native.enter="handleKeyEnter" maxlength="30" />
  16. <!-- 登录 -->
  17. <router-link class="nav_title" :to="hasToken ? '' : 'p_login'" @click="clearActive">
  18. <img v-if="!hasToken" src="https://static.g.mi.com/game/websites/public/img/portrait_default.06318944.png" />
  19. <el-dropdown trigger="click" v-else>
  20. <span class="el-dropdown-link">
  21. <img src="https://static.g.mi.com/game/websites/public/img/portrait_default.06318944.png" />
  22. </span>
  23. <template #dropdown>
  24. <el-dropdown-menu>
  25. <el-dropdown-item class="clearfix" @click="logOut">
  26. 退出登录
  27. </el-dropdown-item>
  28. </el-dropdown-menu>
  29. </template>
  30. </el-dropdown>
  31. </router-link>
  32. </nav>
  33. </div>
  34. </template>
  35. <script setup lang="ts">
  36. import { reactive, ref, watch } from 'vue'
  37. import { Search } from '@element-plus/icons-vue'
  38. import { useRouter, useRoute } from 'vue-router'
  39. import { getGameList } from '@/api/index'
  40. import Message from '@/utils/Message'
  41. import local from '@/utils/local'
  42. import { useStore } from '@/store'
  43. const router = useRouter()
  44. const route = useRoute()
  45. const search = useStore('search')
  46. const header = useStore('header')
  47. // 搜索内容
  48. const game_name = ref(local.get('searchText') || '')
  49. // 绑定el-autocomplete组件
  50. const myAutocomplete = ref(null)
  51. const hasToken = ref(localStorage.getItem('token'))
  52. const currentIndex = ref(local.get('headerPath') || header.getHeaderTitle) //取保存的title
  53. console.log('currentIndex', currentIndex.value);
  54. if (route.query.account) {
  55. currentIndex.value = '我的'
  56. console.log('1234567', currentIndex.value);
  57. }
  58. // 顶部导航栏
  59. const routes = reactive([
  60. { id: 0, path: '/p_index', title: '首页' },
  61. // { id: 1, path: '/p_rank', title: '排行' },
  62. { id: 2, path: '/p_categroy', title: '分类' },
  63. { id: 3, path: '/p_mine', title: '我的' }
  64. ])
  65. // 改变字体颜色
  66. const changeIndex = (val: string) => {
  67. currentIndex.value = val
  68. local.set('headerPath', currentIndex.value) //保存当前title
  69. local.remove('searchText')
  70. game_name.value = ''
  71. }
  72. // 点击登录清除本地存储
  73. const clearActive = () => {
  74. currentIndex.value = ''
  75. local.remove('headerPath') //删除保存的title
  76. local.remove('searchText')
  77. game_name.value = ''
  78. }
  79. // 点击网页title清除本地存储
  80. const clearAct = () => {
  81. local.remove('headerPath')
  82. currentIndex.value = '首页'
  83. local.set('headerPath', currentIndex.value)
  84. local.remove('searchText')
  85. game_name.value = ''
  86. }
  87. // 退出登录
  88. const logOut = () => {
  89. // console.log(22);
  90. localStorage.removeItem('token')
  91. router.push({ path: 'p_login' })
  92. }
  93. interface LinkItem {
  94. id: string
  95. value: string
  96. }
  97. const links: any = ref<LinkItem[]>([])
  98. const searchChange = async (str: string, cb: (arg: any) => void) => {
  99. if (str && str.length > 0) {
  100. game_name.value = str
  101. currentIndex.value = ''
  102. local.remove('headerPath')
  103. try {
  104. var params = reactive({
  105. type: 0,
  106. tag_id: 0,
  107. page: 1,
  108. pagesize: 10,
  109. q: str
  110. })
  111. var { data } = await getGameList(params)
  112. links.value = data.data.lists
  113. links.value = links.value.map((item, index) => {
  114. return {
  115. id: `${index}`,
  116. value: `${item.screen_name}`
  117. }
  118. })
  119. links.value = links.value.filter(item => {
  120. return item.value.indexOf(str) > -1
  121. })
  122. cb(links.value)
  123. } catch (error: any) {
  124. // links.value.push({
  125. // id: '-1',
  126. // value: '暂无匹配数据'
  127. // })
  128. // cb(links.value)
  129. console.log(error)
  130. Message.error(error.data.msg)
  131. }
  132. } else {
  133. cb([])
  134. }
  135. }
  136. const handleSelect = (item: LinkItem) => {
  137. if (item) {
  138. let { value } = item
  139. router.push({ path: '/p_search', query: { screen_name: value } })
  140. search.setSearchText(game_name.value)
  141. local.set('searchText', game_name.value)
  142. search.setSearchLis(game_name.value)
  143. }
  144. }
  145. // 手动回车跳转搜索页
  146. const handleKeyEnter = () => {
  147. router.push({ path: '/p_search', query: { screen_name: game_name.value } })
  148. search.setSearchText(game_name.value)
  149. local.set('searchText', game_name.value)
  150. search.setSearchLis(game_name.value)
  151. }
  152. watch(currentIndex, (iold, inew) => { }, { deep: true, immediate: true })
  153. </script>
  154. <style scoped lang="scss">
  155. .header_nav{
  156. margin: auto;
  157. }
  158. nav {
  159. height: 100%;
  160. .game_logo {
  161. height: 100%;
  162. line-height: 80px;
  163. font-weight: normal;
  164. .logo {
  165. font-size: 24px;
  166. color: #000;
  167. background: url('http://gm.nkfzs.com/favicon.ico') no-repeat center left;
  168. background-size: 30px 30px;
  169. height: 100%;
  170. padding-left: 37px;
  171. }
  172. }
  173. .nav_title {
  174. font-size: 20px;
  175. text-align: center;
  176. &:hover {
  177. color: #ed8c0f
  178. }
  179. }
  180. .active {
  181. color: #ed8c0f
  182. }
  183. }
  184. :deep(.el-input) {
  185. width: 200px;
  186. }
  187. :deep(.el-input__wrapper) {
  188. border-radius: 20px;
  189. }
  190. // .el-dropdown {
  191. // margin-top: 1.1rem;
  192. // }
  193. </style>