feat:首页和案例库功能

This commit is contained in:
李伊帆
2026-09-21 15:41:52 +08:00
parent 24d001e0f5
commit b0a711f70a
35 changed files with 1201 additions and 354 deletions

View File

@@ -1,69 +1,133 @@
<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_LIBRARY_LIST,
CASE_PAGE_SIZE,
DEFAULT_CASE_FILTER,
} from '@/constants/case-library'
import type { CaseCategory, CaseFilterValues } from '@/types/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 { mapProjectCasesToCaseItems } from '@/utils/map-case-library'
import filterIcon from '@/static/icons/icon-filter.svg'
import { cloneCaseFilter, matchCaseFilter } from '@/utils/case-filter'
import CaseCard from './components/CaseCard.vue'
import CaseFilterPanel from './components/CaseFilterPanel.vue'
import LoadMoreFooter from './components/LoadMoreFooter.vue'
const LOAD_MORE_DELAY = 600
const { applyFavoritesToList, syncFavoriteIds, toggleFavorite } = useCaseFavorites()
const { toggleFavorite } = useCaseFavorites()
const { industries, loadCaseIndustries } = useCaseIndustries()
const keyword = ref('')
const activeCategory = ref<CaseCategory>('全部')
const caseList = ref(applyFavoritesToList(CASE_LIBRARY_LIST.map(item => ({ ...item }))))
const pageSize = ref(CASE_PAGE_SIZE)
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 filteredCases = computed(() => {
const text = keyword.value.trim()
const hasMore = computed(() => caseList.value.length < totalCount.value)
const loadFinished = computed(() => !hasMore.value && caseList.value.length > 0)
return caseList.value.filter((item) => {
const matchCategory = activeCategory.value === '全部' || item.category === activeCategory.value
const matchKeyword = !text || item.title.includes(text) || item.industry.includes(text) || item.category.includes(text)
const matchFilter = matchCaseFilter(item, appliedFilter.value)
return matchCategory && matchKeyword && matchFilter
})
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 {
const params = buildCaseLibraryQuery({
keyword: keyword.value,
activeCategory: activeCategory.value,
filter: appliedFilter.value,
pageNum: pageNum.value,
pageSize: CASE_PAGE_SIZE,
})
const result = 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)
})
const visibleCases = computed(() => filteredCases.value.slice(0, pageSize.value))
const hasMore = computed(() => pageSize.value < filteredCases.value.length)
const loadFinished = computed(() => !hasMore.value && visibleCases.value.length > 0)
watch([keyword, activeCategory, appliedFilter], () => {
pageSize.value = CASE_PAGE_SIZE
watch(activeCategory, () => {
fetchCaseList(true)
})
onShow(() => {
syncFavoriteIds()
caseList.value = applyFavoritesToList(CASE_LIBRARY_LIST.map(item => ({ ...item })))
loadCaseIndustries()
fetchCaseList(true)
})
function handleSelectCategory(category: CaseCategory) {
activeCategory.value = category
}
function handleToggleFavorite(id: string) {
toggleFavorite(id)
caseList.value = applyFavoritesToList(CASE_LIBRARY_LIST.map(item => ({ ...item })))
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',
})
@@ -73,12 +137,8 @@ function handleLoadMore() {
if (loadingMore.value || !hasMore.value)
return
loadingMore.value = true
setTimeout(() => {
pageSize.value += CASE_PAGE_SIZE
loadingMore.value = false
}, LOAD_MORE_DELAY)
pageNum.value += 1
fetchCaseList(false)
}
function handleOpenFilter() {
@@ -91,6 +151,7 @@ function handleCloseFilter() {
function handleConfirmFilter(value: CaseFilterValues) {
appliedFilter.value = cloneCaseFilter(value)
fetchCaseList(true)
}
function handleCaseTap(id: string) {
@@ -153,7 +214,7 @@ function handleCaseTap(id: string) {
<view class="case-library__toolbar">
<text class="case-library__count">
共 {{ filteredCases.length }}
共 {{ totalCount }}
</text>
<view
class="case-library__filter"
@@ -180,7 +241,7 @@ function handleCaseTap(id: string) {
>
<view class="case-library__list">
<CaseCard
v-for="item in visibleCases"
v-for="item in caseList"
:key="item.id"
:item="item"
@toggle-favorite="handleToggleFavorite"
@@ -188,7 +249,7 @@ function handleCaseTap(id: string) {
/>
<LoadMoreFooter
:loading="loadingMore"
:loading="loadingList || loadingMore"
:has-more="hasMore"
:finished="loadFinished"
@load-more="handleLoadMore"
@@ -201,6 +262,7 @@ function handleCaseTap(id: string) {
<CaseFilterPanel
:visible="filterVisible"
:cases="caseList"
:industries="industries"
:value="appliedFilter"
@close="handleCloseFilter"
@confirm="handleConfirmFilter"