refactor: 重构faq与document页

- 将筛选器提取为单独的组件ProductFilters
This commit is contained in:
2025-10-29 16:42:48 +08:00
parent c4e797500f
commit 5f78c888a2
3 changed files with 137 additions and 140 deletions

View File

@ -0,0 +1,85 @@
<template>
<div class="question-category">
<el-row :gutter="12">
<el-col :span="8">
<span class="select-label">产品分类</span>
<el-select
v-model="model.selectedType"
placeholder="选择产品类型"
clearable
>
<el-option
v-for="type in productTypeOptions"
:key="type.id"
:label="type.name"
:value="type.id"
/>
</el-select>
</el-col>
<el-col :span="8">
<span class="select-label">产品系列</span>
<el-select
v-model="model.selectedProduct"
placeholder="选择系列产品"
clearable
>
<el-option
v-for="product in productOptions"
:key="product.id"
:label="product.name"
:value="product.id"
/>
</el-select>
</el-col>
<el-col :span="8">
<span class="select-label">关键词</span>
<el-input
v-model="model.keyword"
placeholder="输入关键词..."
clearable
:prefix-icon="Search"
/>
</el-col>
</el-row>
</div>
</template>
<script setup lang="ts">
import { Search } from '@element-plus/icons-vue';
defineProps({
productTypeOptions: {
type: Array as () => Array<{ id: number; name: string }>,
default: () => [],
},
productOptions: {
type: Array as () => Array<{ id: number; name: string }>,
default: () => [],
},
});
const model = defineModel<{
keyword: string;
selectedType: number | null;
selectedProduct: number | null;
}>();
</script>
<style scoped>
.question-category {
margin-bottom: 1rem;
}
.select-label {
color: var(--el-text-color-secondary);
font-size: 0.9rem;
}
:deep(.el-select__wrapper),
:deep(.el-input__wrapper) {
height: 40px;
font-size: 0.9rem;
}
</style>

View File

@ -9,50 +9,12 @@
<h1 class="page-title">{{ $t('navigation.documents') }}</h1> <h1 class="page-title">{{ $t('navigation.documents') }}</h1>
<app-breadcrumb class="breadcrumb" :items="breadcrumbItems" /> <app-breadcrumb class="breadcrumb" :items="breadcrumbItems" />
</div> </div>
<div class="document-category">
<el-row :gutter="12">
<el-col :span="8">
<span class="select-label">产品分类</span>
<el-select
v-model="selectedType"
placeholder="选择产品类型"
clearable
>
<el-option
v-for="type in productTypeOptions"
:key="type.id"
:label="type.name"
:value="type.id"
/>
</el-select>
</el-col>
<el-col :span="8">
<span class="select-label">产品系列</span>
<el-select
v-model="selectedProduct"
placeholder="选择系列产品"
clearable
>
<el-option
v-for="product in productOptions"
:key="product.id"
:label="product.name"
:value="product.id"
/>
</el-select>
</el-col>
<el-col :span="8">
<span class="select-label">关键词</span>
<el-input
v-model="keyword"
placeholder="输入关键词..."
clearable
:prefix-icon="Search"
/>
</el-col>
</el-row>
</div>
<div class="page-content"> <div class="page-content">
<product-filter
v-model="filters"
:product-type-options="productTypeOptions"
:product-options="productOptions"
/>
<document-list :documents="filteredDocuments" /> <document-list :documents="filteredDocuments" />
</div> </div>
</div> </div>
@ -60,8 +22,6 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { Search } from '@element-plus/icons-vue';
const localePath = useLocalePath(); const localePath = useLocalePath();
const breadcrumbItems = [ const breadcrumbItems = [
{ label: $t('navigation.home'), to: localePath('/') }, { label: $t('navigation.home'), to: localePath('/') },
@ -69,17 +29,18 @@
{ label: $t('navigation.documents') }, { label: $t('navigation.documents') },
]; ];
const filters = reactive({
selectedType: null as number | null,
selectedProduct: null as number | null,
keyword: '',
});
const { data, pending, error } = await useDocumentList(); const { data, pending, error } = await useDocumentList();
const documents = computed( const documents = computed(
() => data?.value.map((item) => toDocumentListView(item)) ?? [] () => data?.value.map((item) => toDocumentListView(item)) ?? []
); );
const keyword = ref('');
const selectedType = ref<number | null>(null);
const selectedProduct = ref<number | null>(null);
const productTypeOptions = computed(() => { const productTypeOptions = computed(() => {
const types: DocumentListProductType[] = []; const types: DocumentListProductType[] = [];
documents.value.forEach((doc: DocumentListView) => { documents.value.forEach((doc: DocumentListView) => {
@ -95,13 +56,13 @@
}); });
const productOptions = computed(() => { const productOptions = computed(() => {
if (!selectedType.value) return []; if (!filters.selectedType) return [];
const products: DocumentListProduct[] = []; const products: DocumentListProduct[] = [];
documents.value.forEach((doc: DocumentListView) => { documents.value.forEach((doc: DocumentListView) => {
doc.products?.forEach((product: DocumentListProduct) => { doc.products?.forEach((product: DocumentListProduct) => {
if ( if (
product.type.id === selectedType.value && product.type.id === filters.selectedType &&
!products.some((item) => item.id === product.id) !products.some((item) => item.id === product.id)
) { ) {
products.push(product); products.push(product);
@ -114,29 +75,32 @@
const filteredDocuments = computed(() => const filteredDocuments = computed(() =>
documents.value.filter((doc: DocumentListView) => { documents.value.filter((doc: DocumentListView) => {
const matchProduct = selectedProduct.value const matchProduct = filters.selectedProduct
? doc.products?.some( ? doc.products?.some(
(product: DocumentListProduct) => (product: DocumentListProduct) =>
product.id === selectedProduct.value product.id === filters.selectedProduct
) )
: selectedType.value : filters.selectedType
? doc.products?.some( ? doc.products?.some(
(product: DocumentListProduct) => (product: DocumentListProduct) =>
product.type?.id === selectedType.value product.type?.id === filters.selectedType
) )
: true; : true;
const matchKeyword = keyword.value const matchKeyword = filters.keyword
? doc.title && doc.title.includes(keyword.value) ? doc.title && doc.title.includes(filters.keyword)
: true; : true;
return matchProduct && matchKeyword; return matchProduct && matchKeyword;
}) })
); );
watch(selectedType, () => { watch(
selectedProduct.value = null; () => filters.selectedType,
}); () => {
filters.selectedProduct = null;
}
);
watch(documents, (value) => { watch(documents, (value) => {
console.log(value); console.log(value);

View File

@ -10,50 +10,13 @@
<app-breadcrumb class="breadcrumb" :items="breadcrumbItems" /> <app-breadcrumb class="breadcrumb" :items="breadcrumbItems" />
</div> </div>
<div class="question-category">
<el-row :gutter="12">
<el-col :span="8">
<span class="select-label">产品分类</span>
<el-select
v-model="selectedType"
placeholder="选择产品类型"
clearable
>
<el-option
v-for="type in productTypeOptions"
:key="type.id"
:label="type.name"
:value="type.id"
/>
</el-select>
</el-col>
<el-col :span="8">
<span class="select-label">产品系列</span>
<el-select
v-model="selectedProduct"
placeholder="选择系列产品"
clearable
>
<el-option
v-for="product in productOptions"
:key="product.id"
:label="product.name"
:value="product.id"
/>
</el-select>
</el-col>
<el-col :span="8">
<span class="select-label">关键词</span>
<el-input
v-model="keyword"
placeholder="输入关键词..."
clearable
:prefix-icon="Search"
/>
</el-col>
</el-row>
</div>
<div class="page-content"> <div class="page-content">
<product-filter
v-model="filters"
:product-type-options="productTypeOptions"
:product-options="productOptions"
/>
<question-list :questions="filteredQuestions" /> <question-list :questions="filteredQuestions" />
</div> </div>
</div> </div>
@ -61,10 +24,14 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { Search } from '@element-plus/icons-vue';
const localePath = useLocalePath(); const localePath = useLocalePath();
const filters = reactive({
selectedType: null as number | null,
selectedProduct: null as number | null,
keyword: '',
});
const breadcrumbItems = [ const breadcrumbItems = [
{ label: $t('navigation.home'), to: localePath('/') }, { label: $t('navigation.home'), to: localePath('/') },
{ label: $t('navigation.support'), to: localePath('/support') }, { label: $t('navigation.support'), to: localePath('/support') },
@ -77,11 +44,6 @@
() => data.value.map((item) => toQuestionListView(item)) ?? null () => data.value.map((item) => toQuestionListView(item)) ?? null
); );
const keyword = ref('');
const selectedType = ref<number | null>(null);
const selectedProduct = ref<number | null>(null);
const productTypeOptions = computed(() => { const productTypeOptions = computed(() => {
const types: QuestionListProductType[] = []; const types: QuestionListProductType[] = [];
questions.value.forEach((q: QuestionListView) => { questions.value.forEach((q: QuestionListView) => {
@ -96,12 +58,12 @@
}); });
const productOptions = computed(() => { const productOptions = computed(() => {
if (!selectedType.value) return []; if (!filters.selectedType) return [];
const products: QuestionListProduct[] = []; const products: QuestionListProduct[] = [];
questions.value.forEach((q: QuestionListView) => { questions.value.forEach((q: QuestionListView) => {
q.products.forEach((product: QuestionListProduct) => { q.products.forEach((product: QuestionListProduct) => {
if ( if (
product.type.id === selectedType.value && product.type.id === filters.selectedType &&
!products.some((p) => p.id === product.id) !products.some((p) => p.id === product.id)
) { ) {
products.push(product); products.push(product);
@ -113,30 +75,33 @@
const filteredQuestions = computed(() => { const filteredQuestions = computed(() => {
return questions.value.filter((question: QuestionListView) => { return questions.value.filter((question: QuestionListView) => {
const matchProduct = selectedProduct.value const matchProduct = filters.selectedProduct
? question.products?.some( ? question.products?.some(
(product: QuestionListProduct) => (product: QuestionListProduct) =>
product.id === selectedProduct.value product.id === filters.selectedProduct
) )
: selectedType.value : filters.selectedType
? question.products?.some( ? question.products?.some(
(product: QuestionListProduct) => (product: QuestionListProduct) =>
product.type.id === selectedType.value product.type.id === filters.selectedType
) )
: true; : true;
const matchKeyword = keyword.value const matchKeyword = filters.keyword
? (question.title && question.title.includes(keyword.value)) || ? (question.title && question.title.includes(filters.keyword)) ||
(question.content && question.content.includes(keyword.value)) (question.content && question.content.includes(filters.keyword))
: true; : true;
return matchProduct && matchKeyword; return matchProduct && matchKeyword;
}); });
}); });
watch(selectedType, () => { watch(
selectedProduct.value = null; () => filters.selectedType,
}); () => {
filters.selectedProduct = null;
}
);
watch(data, (newVal) => { watch(data, (newVal) => {
console.log('useAsyncData updated:', newVal); console.log('useAsyncData updated:', newVal);
@ -171,24 +136,7 @@
margin-left: auto; margin-left: auto;
} }
.question-category {
padding: 0rem 2rem;
gap: 4px;
margin-bottom: 0.5rem;
}
.page-content { .page-content {
padding: 1rem 2rem 2rem; padding: 1rem 2rem 2rem;
} }
.select-label {
color: var(--el-text-color-secondary);
font-size: 0.9rem;
}
:deep(.el-select__wrapper),
:deep(.el-input__wrapper) {
height: 40px;
font-size: 0.9rem;
}
</style> </style>