fix:优化搜索逻辑

This commit is contained in:
jiangrui
2025-03-07 23:21:24 +08:00
parent a04f16bfa4
commit eedd68d137
6 changed files with 62 additions and 17 deletions

View File

@@ -142,7 +142,7 @@ import ResourceSelect from "@/components/Home/ResourceSelect.vue";
import ResourceTable from "@/components/Home/ResourceTable.vue";
import { formattedFileSize } from "@/utils/index";
import type { ResourceItem, TagColor } from "@/types";
import { onMounted, onBeforeUnmount } from "vue";
import ResourceCard from "@/components/Home/ResourceCard.vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
@@ -212,8 +212,20 @@ const handleLoadMore = (channelId: string) => {
};
const searchMovieforTag = (tag: string) => {
router.push({ path: "/", query: { keyword: tag } });
router.push({ path: "/resource", query: { keyword: tag } });
};
// 页面进入 设置缓存的数据源
onMounted(() => {
const lastResourceList = localStorage.getItem("last_resource_list");
if (lastResourceList) {
resourceStore.resources = JSON.parse(lastResourceList).list;
}
});
// 页面销毁 清除搜索词
onBeforeUnmount(() => {
resourceStore.keyword = "";
});
</script>
<style lang="scss" scoped>

View File

@@ -81,10 +81,16 @@ watch(
searchForm.value.keyword = keyword;
handleSearch();
} else {
searchForm.value.keyword = "";
searchForm.value.keyword = resourceStore.keyword;
}
}
);
watch(
() => resourceStore.keyword,
(newKeyword) => {
searchForm.value.keyword = newKeyword;
}
);
// 方法定义
const handleSearch = async () => {

View File

@@ -121,7 +121,7 @@
</template>
<script setup lang="ts">
import { ref, watch, onMounted, onUnmounted, computed } from "vue";
import { ref, watch, onMounted, onUnmounted, computed, onBeforeUnmount } from "vue";
import { useRouter } from "vue-router";
import { showToast } from "vant";
import { useResourceStore } from "@/stores/resource";
@@ -223,14 +223,14 @@ const searchMovieforTag = (tag: string) => {
// 使用节流包装加载更多函数
const throttledLoadMore = throttle((channelId: string) => {
resourceStore.searchResources("", true, channelId);
}, 200);
}, 2000);
// 滚动加载
const doScroll = () => {
const appElement = document.querySelector("#app") as HTMLElement;
if (appElement) {
const { scrollHeight, scrollTop, clientHeight } = appElement;
if (scrollHeight - (clientHeight + scrollTop) <= 200) {
if (scrollHeight - (clientHeight + scrollTop) <= 10) {
throttledLoadMore(currentTab.value);
}
}
@@ -258,6 +258,18 @@ watch(currentTab, () => {
appElement.scrollTo(0, 0);
}
});
// 页面进入 设置缓存的数据源
onMounted(() => {
const lastResourceList = localStorage.getItem("last_resource_list");
if (lastResourceList) {
resourceStore.resources = JSON.parse(lastResourceList).list;
}
});
// 页面销毁 清除搜索词
onBeforeUnmount(() => {
resourceStore.keyword = "";
});
</script>
<style lang="scss" scoped>