feat: 为搜索界面添加分页功能

- 限制搜索页面的显示条目数量为5条
- 为搜索页面添加分页功能
- 调整nuxt.config.ts,禁用ts类型检查

resolve #17
This commit is contained in:
2025-09-20 14:31:23 +08:00
2 changed files with 107 additions and 39 deletions

View File

@ -29,39 +29,75 @@
<el-skeleton :rows="4" animated /> <el-skeleton :rows="4" animated />
</div> </div>
<div v-else-if="hasResults" class="search-results"> <div v-else-if="hasResults" class="search-results">
<section <!-- <section -->
v-for="section in filteredSections" <!-- v-for="section in filteredSections" -->
:key="section.indexUid" <!-- :key="section.indexUid" -->
class="search-section" <!-- class="search-section" -->
> <!-- > -->
<header class="section-header"> <!-- <header class="section-header"> -->
<h2 class="section-title"> <!-- <h2 class="section-title"> -->
{{ getIndexLabel(section.indexUid) }} <!-- {{ getIndexLabel(section.indexUid) }} -->
<span class="section-count">{{ <!-- <span class="section-count">{{ -->
$t('search.result-count', { count: section.estimatedTotalHits }) <!-- $t('search.result-count', { count: section.estimatedTotalHits }) -->
}}</span> <!-- }}</span> -->
</h2> <!-- </h2> -->
</header> <!-- </header> -->
<!-- <div class="section-results"> -->
<!-- <el-card -->
<!-- v-for="(hit, hitIndex) in section.hits" -->
<!-- :key="`${section.indexUid}-${getHitIdentifier(hit, hitIndex)}`" -->
<!-- class="result-card" -->
<!-- > -->
<!-- <NuxtLink -->
<!-- v-if="resolveHitLink(hit)" -->
<!-- :to="resolveHitLink(hit) || ''" -->
<!-- class="result-title" -->
<!-- > -->
<!-- {{ getHitTitle(hit) }} -->
<!-- </NuxtLink> -->
<!-- <h3 v-else class="result-title">{{ getHitTitle(hit) }}</h3> -->
<!-- <p v-if="getHitSummary(hit)" class="result-summary"> -->
<!-- {{ getHitSummary(hit) }} -->
<!-- </p> -->
<!-- </el-card> -->
<!-- </div> -->
<!-- </section> -->
<div class="section-results"> <div class="section-results">
<el-card <el-card
v-for="(hit, hitIndex) in section.hits" v-for="(hit, hitIndex) in paginatedHits"
:key="`${section.indexUid}-${getHitIdentifier(hit, hitIndex)}`" :key="`${getHitIdentifier(hit.content, hitIndex)}`"
class="result-card" class="result-card"
> >
<NuxtLink <NuxtLink
v-if="resolveHitLink(hit)" v-if="resolveHitLink(hit.content)"
:to="resolveHitLink(hit) || ''" :to="resolveHitLink(hit.content) || ''"
class="result-title" class="result-title"
> >
{{ getHitTitle(hit) }} {{ getHitTitle(hit.content) }}
</NuxtLink> </NuxtLink>
<h3 v-else class="result-title">{{ getHitTitle(hit) }}</h3> <h3 v-else class="result-title">{{ getHitTitle(hit.content) }}</h3>
<p v-if="getHitSummary(hit)" class="result-summary"> <p v-if="getHitSummary(hit.content)" class="result-summary">
{{ getHitSummary(hit) }} {{ getHitSummary(hit.content) }}
</p>
<p v-if="hit.type" class="result-type">
<span>内容类型: </span>
<span class="result-type-name">{{ getIndexLabel(hit.type) }}</span>
</p> </p>
</el-card> </el-card>
</div> </div>
</section>
<!-- 分页组件 -->
<div class="pagination-container text-align-center mt-12">
<el-pagination
class="justify-center"
layout="prev, pager, next"
:current-page="currentPage"
:page-size="pageSize"
:total="hits.length"
@current-change="handleCurrentChange"
/>
</div>
</div> </div>
<div v-else class="search-state"> <div v-else class="search-state">
<el-empty <el-empty
@ -91,6 +127,8 @@
const loading = ref(true); const loading = ref(true);
const keyword = ref(''); const keyword = ref('');
const currentPage = ref(1);
const pageSize = ref(5);
const sections = ref<SearchSection[]>([]); const sections = ref<SearchSection[]>([]);
const localeSections = computed(() => const localeSections = computed(() =>
sections.value.map((section) => ({ sections.value.map((section) => ({
@ -105,6 +143,16 @@
const filteredSections = computed(() => const filteredSections = computed(() =>
localeSections.value.filter((section) => section.hits.length > 0) localeSections.value.filter((section) => section.hits.length > 0)
); );
const hits = computed(() =>
filteredSections.value.flatMap((item) =>
item.hits.map((content) => ({ content, type: item.indexUid }))
)
);
const paginatedHits = computed(() => {
const start = (currentPage.value - 1) * pageSize.value;
const end = currentPage.value * pageSize.value;
return hits.value.slice(start, end);
});
const activeRequestId = ref(0); const activeRequestId = ref(0);
const indexLabels = computed<Record<string, string>>(() => ({ const indexLabels = computed<Record<string, string>>(() => ({
@ -204,8 +252,8 @@
if (requestId === activeRequestId.value) { if (requestId === activeRequestId.value) {
sections.value = results; sections.value = results;
} }
console.log(results); console.log('hits:', hits.value);
console.log(localeSections.value); console.log('paginatedHits:', paginatedHits.value);
} catch (error) { } catch (error) {
console.error('Failed to perform search', error); console.error('Failed to perform search', error);
if (requestId === activeRequestId.value) { if (requestId === activeRequestId.value) {
@ -234,16 +282,16 @@
}; };
} }
// const handleInput = debounce((value: string) => {
// performSearch(value);
// }, 300);
const handleClear = () => { const handleClear = () => {
keyword.value = ''; keyword.value = '';
sections.value = []; sections.value = [];
router.replace(localePath({ path: '/search' })); router.replace(localePath({ path: '/search' }));
}; };
const handleCurrentChange = (page: number) => {
currentPage.value = page;
};
watch( watch(
() => route.query.query, () => route.query.query,
(newQuery) => { (newQuery) => {
@ -367,16 +415,26 @@
font-size: 1.2rem; font-size: 1.2rem;
font-weight: 600; font-weight: 600;
color: var(--el-color-primary); color: var(--el-color-primary);
margin-bottom: 0.5rem;
display: inline-block; display: inline-block;
} }
.result-summary { .result-summary {
font-size: 0.95rem; font-size: 0.95rem;
color: var(--el-text-color-regular); color: var(--el-text-color-regular);
margin-bottom: 0.5rem;
line-height: 1.6; line-height: 1.6;
} }
.result-type {
font-size: 0.8rem;
color: var(--el-text-color-secondary);
}
.result-type-name {
margin-left: 4px;
color: var(--el-color-primary);
}
@media (max-width: 640px) { @media (max-width: 640px) {
.search-page { .search-page {
padding: 2rem 1rem; padding: 2rem 1rem;

View File

@ -36,6 +36,16 @@ export default defineNuxtConfig({
provider: 'local', provider: 'local',
}, },
typescript: {
tsConfig: {
compilerOptions: {
noUnUsedLocals: false,
noUnUsedParameters: false,
strict: false,
},
},
},
// css // css
css: [ css: [
'@unocss/reset/tailwind.css', '@unocss/reset/tailwind.css',