feat: 优化搜索

This commit is contained in:
alger
2024-09-12 17:28:51 +08:00
parent 0c156e2708
commit 791121ae06
3 changed files with 31 additions and 29 deletions
+7 -4
View File
@@ -14,7 +14,7 @@
</template> </template>
<template #suffix> <template #suffix>
<div class="w-20 px-3 flex justify-between items-center"> <div class="w-20 px-3 flex justify-between items-center">
<div>{{ searchTypeOptions.find((item) => item.key === searchType)?.label }}</div> <div>{{ searchTypeOptions.find((item) => item.key === store.state.searchType)?.label }}</div>
<n-dropdown trigger="hover" :options="searchTypeOptions" @select="selectSearchType"> <n-dropdown trigger="hover" :options="searchTypeOptions" @select="selectSearchType">
<i class="iconfont icon-xiasanjiaoxing"></i> <i class="iconfont icon-xiasanjiaoxing"></i>
</n-dropdown> </n-dropdown>
@@ -90,7 +90,6 @@ onMounted(() => {
// 搜索词 // 搜索词
const searchValue = ref(''); const searchValue = ref('');
const searchType = ref(1);
const search = () => { const search = () => {
const { value } = searchValue; const { value } = searchValue;
if (value === '') { if (value === '') {
@@ -98,17 +97,21 @@ const search = () => {
return; return;
} }
if (router.currentRoute.value.path === '/search') {
store.state.searchValue = value;
return;
}
router.push({ router.push({
path: '/search', path: '/search',
query: { query: {
keyword: value, keyword: value,
type: searchType.value,
}, },
}); });
}; };
const selectSearchType = (key: number) => { const selectSearchType = (key: number) => {
searchType.value = key; store.state.searchType = key;
}; };
const searchTypeOptions = ref(SEARCH_TYPES); const searchTypeOptions = ref(SEARCH_TYPES);
+4
View File
@@ -18,6 +18,8 @@ interface State {
setData: any; setData: any;
lyric: any; lyric: any;
isMobile: boolean; isMobile: boolean;
searchValue: string;
searchType: number;
} }
const state: State = { const state: State = {
@@ -32,6 +34,8 @@ const state: State = {
setData: null, setData: null,
lyric: {}, lyric: {},
isMobile: false, isMobile: false,
searchValue: '',
searchType: 1,
}; };
const windowData = window as any; const windowData = window as any;
+20 -25
View File
@@ -13,7 +13,7 @@
:class="setAnimationClass('animate__bounceInLeft')" :class="setAnimationClass('animate__bounceInLeft')"
:style="setAnimationDelay(index, 10)" :style="setAnimationDelay(index, 10)"
class="hot-search-item" class="hot-search-item"
@click.stop="clickHotKeyword(item.searchWord)" @click.stop="loadSearch(item.searchWord, 1)"
> >
<span class="hot-search-item-count" :class="{ 'hot-search-item-count-3': index < 3 }">{{ index + 1 }}</span> <span class="hot-search-item-count" :class="{ 'hot-search-item-count-3': index < 3 }">{{ index + 1 }}</span>
{{ item.searchWord }} {{ item.searchWord }}
@@ -60,7 +60,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import { useDateFormat } from '@vueuse/core'; import { useDateFormat } from '@vueuse/core';
import { onMounted, ref, watch } from 'vue'; import { onMounted, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router'; import { useRoute } from 'vue-router';
import { useStore } from 'vuex'; import { useStore } from 'vuex';
import { getHotSearch } from '@/api/home'; import { getHotSearch } from '@/api/home';
@@ -74,9 +74,10 @@ defineOptions({
}); });
const route = useRoute(); const route = useRoute();
const router = useRouter(); const store = useStore();
const searchDetail = ref<any>(); const searchDetail = ref<any>();
const searchType = ref(Number(route.query.type) || 1); const searchType = computed(() => store.state.searchType as number);
const searchDetailLoading = ref(false); const searchDetailLoading = ref(false);
// 热搜列表 // 热搜列表
@@ -88,29 +89,26 @@ const loadHotSearch = async () => {
onMounted(() => { onMounted(() => {
loadHotSearch(); loadHotSearch();
loadSearch(route.query.keyword);
}); });
const hotKeyword = ref(route.query.keyword || '搜索列表'); const hotKeyword = ref(route.query.keyword || '搜索列表');
const clickHotKeyword = (keyword: string) => {
hotKeyword.value = keyword; watch(
router.push({ () => store.state.searchValue,
path: '/search', (value) => {
query: { loadSearch(value);
keyword, },
type: 1, );
},
});
// isHotSearchList.value = false;
};
const dateFormat = (time: any) => useDateFormat(time, 'YYYY.MM.DD').value; const dateFormat = (time: any) => useDateFormat(time, 'YYYY.MM.DD').value;
const loadSearch = async (keywords: any) => { const loadSearch = async (keywords: any, type: any = null) => {
hotKeyword.value = keywords; hotKeyword.value = keywords;
searchDetail.value = undefined; searchDetail.value = undefined;
if (!keywords) return; if (!keywords) return;
searchDetailLoading.value = true; searchDetailLoading.value = true;
const { data } = await getSearch({ keywords, type: searchType.value }); const { data } = await getSearch({ keywords, type: type || searchType.value });
const songs = data.result.songs || []; const songs = data.result.songs || [];
const albums = data.result.albums || []; const albums = data.result.albums || [];
@@ -148,18 +146,15 @@ const loadSearch = async (keywords: any) => {
searchDetailLoading.value = false; searchDetailLoading.value = false;
}; };
loadSearch(route.query.keyword);
watch( watch(
() => route.query, () => route.path,
async (newParams) => { async (path) => {
searchType.value = Number(newParams.type || 1); if (path === '/search') {
loadSearch(newParams.keyword); store.state.searchValue = route.query.keyword;
}
}, },
); );
const store = useStore();
const handlePlay = () => { const handlePlay = () => {
const tracks = searchDetail.value?.songs || []; const tracks = searchDetail.value?.songs || [];
store.commit('setPlayList', tracks); store.commit('setPlayList', tracks);