This commit is contained in:
algerkong
2021-07-27 16:01:33 +08:00
parent be721628d0
commit 7f5de1a6a9
3 changed files with 95 additions and 66 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ import { ISearchDetail } from "@/type/search";
// 搜索内容 // 搜索内容
export const getSearch = (keywords: any) => { export const getSearch = (keywords: any) => {
return request.get<ISearchDetail>("/cloudsearch", { return request.get<ISearchDetail>("/search", {
params: { keywords: keywords, type: 1018 }, params: { keywords: keywords, type: 1018 },
}); });
}; };
+12 -56
View File
@@ -5,32 +5,14 @@
size="large" size="large"
round round
v-model:value="searchValue" v-model:value="searchValue"
:placeholder="searchKeyword" :placeholder="hotSearchKeyword"
class="border border-gray-600" class="border border-gray-600"
@focus="searchFocus"
@blur="isSearch = false"
@keydown.enter="search" @keydown.enter="search"
> >
<template #prefix> <template #prefix>
<i class="iconfont icon-search"></i> <i class="iconfont icon-search"></i>
</template> </template>
</n-input> </n-input>
<div
class="hot-search"
v-if="isSearch"
:class="setAnimationClass('animate__fadeInDown')"
>
<template v-for="(item,index) in hotSearchData?.data">
<div class="hot-search-item">
<span
class="hot-search-item-count"
:class="{ 'hot-search-item-count-3': index < 3 }"
>{{ index + 1 }}</span>
{{ item.searchWord }}
</div>
</template>
</div>
</div> </div>
<div class="user-box"> <div class="user-box">
<n-popselect v-model:value="value" :options="options" trigger="click" size="small"> <n-popselect v-model:value="value" :options="options" trigger="click" size="small">
@@ -48,43 +30,34 @@
<script lang="ts" setup> <script lang="ts" setup>
import { getSearchKeyword, getHotSearch } from '@/api/home'; import { getSearchKeyword, getHotSearch } from '@/api/home';
import type { IHotSearch } from "@/type/search";
import { onMounted, ref } from 'vue'; import { onMounted, ref } from 'vue';
import { setAnimationClass } from "@/utils";
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
const router = useRouter() const router = useRouter()
// 推荐热搜词
const searchKeyword = ref("搜索点什么吧...") const hotSearchKeyword = ref("搜索点什么吧...")
const searchValue = ref("") const loadHotSearchKeyword = async () => {
const loadSearchKeyword = async () => {
const { data } = await getSearchKeyword(); const { data } = await getSearchKeyword();
searchKeyword.value = data.data.showKeyword hotSearchKeyword.value = data.data.showKeyword
} }
const hotSearchData = ref<IHotSearch>()
const loadHotSearch = async () => {
const { data } = await getHotSearch();
hotSearchData.value = data;
}
const searchFocus = () => {
isSearch.value = true;
loadHotSearch()
}
// 页面初始化
onMounted(() => { onMounted(() => {
loadSearchKeyword() loadHotSearchKeyword()
}) })
const isSearch = ref(false)
// 搜索词
const searchValue = ref("")
const search = () => { const search = () => {
let value = searchValue.value let value = searchValue.value
if (value == "") { if (value == "") {
searchValue.value = searchKeyword.value searchValue.value = hotSearchKeyword.value
} else { } else {
router.push({ router.push({
path: "/search", path: "/search",
@@ -93,8 +66,6 @@ const search = () => {
} }
}) })
} }
isSearch.value = false;
} }
const value = 'Drive My Car' const value = 'Drive My Car'
@@ -123,20 +94,5 @@ const options = [
.search-box-input { .search-box-input {
@apply relative; @apply relative;
.hot-search {
@apply absolute mt-3 left-0 w-full z-10 shadow-lg border rounded-xl border-2 overflow-hidden grid grid-cols-3;
background: #1a1a1a;
border-color: #63e2b7;
animation-duration: 0.2s;
&-item {
@apply px-4 py-3 text-lg hover:bg-gray-700 rounded-xl cursor-pointer;
&-count {
@apply text-green-500 inline-block ml-3 w-8;
&-3 {
@apply text-red-600 font-bold inline-block ml-3 w-8;
}
}
}
}
} }
</style> </style>
+82 -9
View File
@@ -1,23 +1,96 @@
<template> <template>
<div>{{ searchDetail }}</div> <div class="search-page">
<div
class="hot-search"
style="height:750px"
:class="setAnimationClass('animate__fadeInDown')"
>
<n-layout style="height:750px" :native-scrollbar="false">
<template v-for="(item,index) in hotSearchData?.data">
<div class="hot-search-item" @click.stop="clickHotKeyword(item.searchWord)">
<span
class="hot-search-item-count"
:class="{ 'hot-search-item-count-3': index < 3 }"
>{{ index + 1 }}</span>
{{ item.searchWord }}
</div>
</template>
</n-layout>
</div>
<div>{{ searchDetail }}</div>
</div>
</template> </template>
<script lang="ts" setup>import { getSearch } from "@/api/search"; <script lang="ts" setup>
import { onMounted } from "@vue/runtime-core"; import { getSearch } from "@/api/search";
import { useRoute } from "vue-router"; import type { IHotSearch } from "@/type/search";
import { getHotSearch } from '@/api/home';
import { onBeforeRouteUpdate, useRoute, useRouter } from "vue-router";
import { setAnimationClass } from "@/utils";
import type { ISearchDetail } from "@/type/search" import type { ISearchDetail } from "@/type/search"
import { ref } from "vue"; import { onBeforeMount, onMounted, ref } from "vue";
const route = useRoute(); const route = useRoute();
const keyword = route.query.keyword; const router = useRouter()
const searchDetail = ref<ISearchDetail>() const searchDetail = ref<ISearchDetail>()
const loadSearch = async () => { const loadSearch = async (keyword: any) => {
if (!keyword) return;
const { data } = await getSearch(keyword); const { data } = await getSearch(keyword);
searchDetail.value = data; searchDetail.value = data;
} }
onMounted(() => {
loadSearch() // 热搜列表
const hotSearchData = ref<IHotSearch>()
const loadHotSearch = async () => {
const { data } = await getHotSearch();
hotSearchData.value = data;
}
onBeforeMount(() => {
loadSearch(route.params.keyword)
loadHotSearch();
}) })
const clickHotKeyword = (keyword: string) => {
router.push({
path: "/search",
query: {
keyword: keyword
}
})
// isHotSearchList.value = false;
}
// 监听路由参数变化
onBeforeRouteUpdate(to => {
let value = to.query.keyword?.toString()
loadSearch(value);
})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.search-page {
@apply flex;
}
.hot-search {
@apply mt-3 mr-4 border rounded-xl border-2 flex-1 overflow-hidden;
background: #1a1a1a;
border-color: #63e2b7;
animation-duration: 0.2s;
min-width: 400px;
&-item {
@apply px-4 py-3 text-lg hover:bg-gray-700 rounded-xl cursor-pointer;
&-count {
@apply text-green-500 inline-block ml-3 w-8;
&-3 {
@apply text-red-600 font-bold inline-block ml-3 w-8;
}
}
}
}
</style> </style>