Files
cz-app/src/pages/case-library/index.vue
2026-11-05 13:50:25 +08:00

287 lines
7.4 KiB
Vue

<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { getCaseLibraryList } from '@/api/case-library'
import AppTabBar from '@/components/AppTabBar.vue'
import { useCaseFavorites } from '@/hooks/use-case-favorites'
import { useCaseIndustries } from '@/hooks/use-case-industries'
import {
CASE_CATEGORIES,
CASE_PAGE_SIZE,
DEFAULT_CASE_FILTER,
} from '@/constants/case-library'
import type { CaseCategory, CaseFilterValues, CaseItem } from '@/types/case-library'
import { ensureLoggedIn } from '@/utils/auth-guard'
import { buildCaseLibraryQuery } from '@/utils/build-case-library-query'
import { cloneCaseFilter } from '@/utils/case-filter'
import { ensureCaseBusinessTypeMap } from '@/utils/ensure-case-business-type-map'
import { fetchCaseLibraryByBusinessTypes } from '@/utils/fetch-case-library-by-types'
import { resolveBusinessTypeFilterValues } from '@/utils/format-business-type'
import { mapProjectCasesToCaseItems } from '@/utils/map-case-library'
import filterIcon from '@/static/icons/icon-filter.svg'
import CaseCard from './components/CaseCard.vue'
import CaseFilterPanel from './components/CaseFilterPanel.vue'
import LoadMoreFooter from './components/LoadMoreFooter.vue'
const { toggleFavorite } = useCaseFavorites()
const { industries, loadCaseIndustries } = useCaseIndustries()
const keyword = ref('')
const activeCategory = ref<CaseCategory>('全部')
const caseList = ref<CaseItem[]>([])
const totalCount = ref(0)
const pageNum = ref(1)
const loadingList = ref(false)
const loadingMore = ref(false)
const filterVisible = ref(false)
const appliedFilter = ref<CaseFilterValues>(cloneCaseFilter(DEFAULT_CASE_FILTER))
const hasMore = computed(() => caseList.value.length < totalCount.value)
const loadFinished = computed(() => !hasMore.value && caseList.value.length > 0)
let keywordTimer: ReturnType<typeof setTimeout> | null = null
async function fetchCaseList(reset = false) {
if (reset) {
pageNum.value = 1
caseList.value = []
}
if (loadingList.value || loadingMore.value)
return
if (reset)
loadingList.value = true
else
loadingMore.value = true
try {
await ensureCaseBusinessTypeMap()
const params = buildCaseLibraryQuery({
keyword: keyword.value,
activeCategory: activeCategory.value,
filter: appliedFilter.value,
pageNum: pageNum.value,
pageSize: CASE_PAGE_SIZE,
})
const typeValues = activeCategory.value === '全部'
? []
: resolveBusinessTypeFilterValues(activeCategory.value)
const result = typeValues.length
? await fetchCaseLibraryByBusinessTypes(params, typeValues)
: await getCaseLibraryList(params)
const rows = result?.rows ?? []
const mapped = await mapProjectCasesToCaseItems(rows)
totalCount.value = Number(result?.total ?? 0)
caseList.value = reset
? mapped
: [...caseList.value, ...mapped]
}
catch {
if (reset) {
caseList.value = []
totalCount.value = 0
}
}
finally {
loadingList.value = false
loadingMore.value = false
}
}
watch(keyword, () => {
if (keywordTimer)
clearTimeout(keywordTimer)
keywordTimer = setTimeout(() => {
fetchCaseList(true)
}, 400)
})
watch(activeCategory, () => {
fetchCaseList(true)
})
onShow(() => {
loadCaseIndustries()
fetchCaseList(true)
})
function handleSelectCategory(category: CaseCategory) {
activeCategory.value = category
}
async function handleToggleFavorite(id: string) {
if (!ensureLoggedIn())
return
const target = caseList.value.find(item => item.id === id)
if (!target)
return
const nextFavorite = !target.favorite
caseList.value = caseList.value.map(item => (
item.id === id ? { ...item, favorite: nextFavorite } : item
))
const success = await toggleFavorite(id, target.favorite ?? false)
if (!success) {
caseList.value = caseList.value.map(item => (
item.id === id ? { ...item, favorite: target.favorite } : item
))
}
}
function handleOpenFavorites() {
if (!ensureLoggedIn())
return
uni.navigateTo({
url: '/pages/case-library/favorites/index',
})
}
function handleLoadMore() {
if (loadingMore.value || !hasMore.value)
return
pageNum.value += 1
fetchCaseList(false)
}
function handleOpenFilter() {
filterVisible.value = true
}
function handleCloseFilter() {
filterVisible.value = false
}
function handleConfirmFilter(value: CaseFilterValues) {
appliedFilter.value = cloneCaseFilter(value)
fetchCaseList(true)
}
function handleCaseTap(id: string) {
uni.navigateTo({
url: `/pages/case-library/detail/index?id=${id}`,
})
}
</script>
<template>
<view class="case-library">
<view class="case-library__header">
<text class="case-library__title">
案例库
</text>
<view class="case-library__search-row">
<view class="case-library__search">
<image
class="case-library__search-icon"
src="/static/icons/icon-search.svg"
mode="aspectFit"
/>
<input
v-model="keyword"
class="case-library__search-input"
placeholder="搜索案例标题、描述、标签..."
placeholder-class="case-library__search-placeholder"
>
</view>
<view
class="case-library__favorite-btn"
@tap="handleOpenFavorites"
>
<image
class="case-library__favorite-btn-icon"
src="/static/icons/icon-heart.svg"
mode="aspectFit"
/>
</view>
</view>
<scroll-view
class="case-library__tabs"
scroll-x
:show-scrollbar="false"
>
<view class="case-library__tabs-inner">
<text
v-for="category in CASE_CATEGORIES"
:key="category"
class="case-library__tab"
:class="{ 'case-library__tab--active': activeCategory === category }"
@tap="handleSelectCategory(category)"
>
{{ category }}
</text>
</view>
</scroll-view>
<view class="case-library__toolbar">
<text class="case-library__count">
共 {{ totalCount }}
</text>
<view
class="case-library__filter"
@tap="handleOpenFilter"
>
<image
class="case-library__filter-icon"
:src="filterIcon"
mode="aspectFit"
/>
<text class="case-library__filter-text">
筛选
</text>
</view>
</view>
</view>
<scroll-view
class="case-library__scroll"
scroll-y
:show-scrollbar="false"
:lower-threshold="120"
@scrolltolower="handleLoadMore"
>
<view class="case-library__list">
<CaseCard
v-for="item in caseList"
:key="item.id"
:item="item"
@toggle-favorite="handleToggleFavorite"
@tap="handleCaseTap(item.id)"
/>
<LoadMoreFooter
:loading="loadingList || loadingMore"
:has-more="hasMore"
:finished="loadFinished"
@load-more="handleLoadMore"
/>
</view>
</scroll-view>
<AppTabBar active="case" />
<CaseFilterPanel
:visible="filterVisible"
:cases="caseList"
:industries="industries"
:value="appliedFilter"
@close="handleCloseFilter"
@confirm="handleConfirmFilter"
/>
</view>
</template>
<style scoped lang="scss">
@import './index.scss';
</style>