This commit is contained in:
liu
2026-08-07 08:45:25 +08:00
parent 39b789154c
commit 0203c84357
19 changed files with 1652 additions and 1085 deletions
+18 -5
View File
@@ -9,8 +9,9 @@ import type { Category, Product } from '@/types/product'
import './index.less'
const PAGE_SIZE = 10
/** 存储 key:首页点击分类跳转时经本地存储传参(switchTab 无法带参) */
/** 存储 key:首页点击分类/搜索跳转时经本地存储传参(switchTab 无法带参) */
const PENDING_CATEGORY_KEY = 'product_category_id'
const PENDING_KEYWORD_KEY = 'product_keyword'
export default function ProductPage() {
const addItem = useCartStore(s => s.addItem)
@@ -51,16 +52,16 @@ export default function ProductPage() {
[activeChild, activeTop],
)
/** 拉取商品列表 */
/** 拉取商品列表keywordOverride 用于状态未更新时显式传本次搜索词) */
const fetchList = useCallback(
async (pageNum: number, reset: boolean) => {
async (pageNum: number, reset: boolean, keywordOverride?: string) => {
if (listLoadingRef.current) return
listLoadingRef.current = true
setLoading(true)
try {
const res = await getProductListApi({
category_id: effectiveCategoryId,
keyword: searchKey || undefined,
keyword: keywordOverride ?? (searchKey || undefined),
page: pageNum,
pageSize: PAGE_SIZE,
})
@@ -108,8 +109,20 @@ export default function ProductPage() {
}, [])
useDidShow(() => {
// 处理首页搜索跳转带入的关键词
let pendingKeyword: string | null = null
try {
pendingKeyword = Taro.getStorageSync(PENDING_KEYWORD_KEY) || null
Taro.removeStorageSync(PENDING_KEYWORD_KEY)
} catch {
// noop
}
if (pendingKeyword) {
setInputKey(pendingKeyword)
setSearchKey(pendingKeyword)
}
loadCategories()
fetchList(1, true)
fetchList(1, true, pendingKeyword ?? undefined)
})
useReachBottom(() => {