|
@@ -1,5 +1,5 @@
|
|
|
import React from 'react'
|
|
import React from 'react'
|
|
|
-import { View, Text, ScrollView, Swiper, SwiperItem, Image } from '@tarojs/components'
|
|
|
|
|
|
|
+import { View, Text, ScrollView } from '@tarojs/components'
|
|
|
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
|
|
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
|
|
|
import { TabBarLayout } from '@/layouts/tab-bar-layout'
|
|
import { TabBarLayout } from '@/layouts/tab-bar-layout'
|
|
|
import TDesignSearch from '@/components/tdesign/search'
|
|
import TDesignSearch from '@/components/tdesign/search'
|
|
@@ -12,17 +12,15 @@ import { useAuth } from '@/utils/auth'
|
|
|
import { useCart } from '@/contexts/CartContext'
|
|
import { useCart } from '@/contexts/CartContext'
|
|
|
import { Navbar } from '@/components/ui/navbar'
|
|
import { Navbar } from '@/components/ui/navbar'
|
|
|
import { Carousel } from '@/components/ui/carousel'
|
|
import { Carousel } from '@/components/ui/carousel'
|
|
|
-import Taro, { usePullDownRefresh, useReachBottom, useShareAppMessage } from '@tarojs/taro'
|
|
|
|
|
|
|
+import Taro, { usePullDownRefresh, useShareAppMessage } from '@tarojs/taro'
|
|
|
|
|
|
|
|
type GoodsResponse = InferResponseType<typeof goodsClient.$get, 200>
|
|
type GoodsResponse = InferResponseType<typeof goodsClient.$get, 200>
|
|
|
type Goods = GoodsResponse['data'][0]
|
|
type Goods = GoodsResponse['data'][0]
|
|
|
-type AdvertisementResponse = InferResponseType<typeof advertisementClient.$get, 200>
|
|
|
|
|
-type Advertisement = AdvertisementResponse['data'][0]
|
|
|
|
|
|
|
|
|
|
const HomePage: React.FC = () => {
|
|
const HomePage: React.FC = () => {
|
|
|
const { isLoggedIn } = useAuth();
|
|
const { isLoggedIn } = useAuth();
|
|
|
const { addToCart } = useCart();
|
|
const { addToCart } = useCart();
|
|
|
- if( !isLoggedIn ) return null;
|
|
|
|
|
|
|
+ const [refreshing, setRefreshing] = React.useState(false);
|
|
|
|
|
|
|
|
// 广告数据查询
|
|
// 广告数据查询
|
|
|
const {
|
|
const {
|
|
@@ -58,7 +56,7 @@ const HomePage: React.FC = () => {
|
|
|
} = useInfiniteQuery({
|
|
} = useInfiniteQuery({
|
|
|
queryKey: ['home-goods-infinite'],
|
|
queryKey: ['home-goods-infinite'],
|
|
|
queryFn: async ({ pageParam = 1 }) => {
|
|
queryFn: async ({ pageParam = 1 }) => {
|
|
|
- console.debug('请求商品数据,页码:', pageParam)
|
|
|
|
|
|
|
+ // console.debug('请求商品数据,页码:', pageParam)
|
|
|
const response = await goodsClient.$get({
|
|
const response = await goodsClient.$get({
|
|
|
query: {
|
|
query: {
|
|
|
page: pageParam,
|
|
page: pageParam,
|
|
@@ -72,14 +70,14 @@ const HomePage: React.FC = () => {
|
|
|
throw new Error('获取商品失败')
|
|
throw new Error('获取商品失败')
|
|
|
}
|
|
}
|
|
|
const result = await response.json()
|
|
const result = await response.json()
|
|
|
- console.debug('API响应数据:', {
|
|
|
|
|
- page: pageParam,
|
|
|
|
|
- dataCount: result.data?.length || 0,
|
|
|
|
|
- pagination: result.pagination
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ // console.debug('API响应数据:', {
|
|
|
|
|
+ // page: pageParam,
|
|
|
|
|
+ // dataCount: result.data?.length || 0,
|
|
|
|
|
+ // pagination: result.pagination
|
|
|
|
|
+ // })
|
|
|
return result
|
|
return result
|
|
|
},
|
|
},
|
|
|
- getNextPageParam: (lastPage, allPages) => {
|
|
|
|
|
|
|
+ getNextPageParam: (lastPage, _allPages) => {
|
|
|
const { pagination } = lastPage
|
|
const { pagination } = lastPage
|
|
|
const totalPages = Math.ceil(pagination.total / pagination.pageSize)
|
|
const totalPages = Math.ceil(pagination.total / pagination.pageSize)
|
|
|
|
|
|
|
@@ -98,6 +96,7 @@ const HomePage: React.FC = () => {
|
|
|
},
|
|
},
|
|
|
staleTime: 5 * 60 * 1000,
|
|
staleTime: 5 * 60 * 1000,
|
|
|
initialPageParam: 1,
|
|
initialPageParam: 1,
|
|
|
|
|
+ enabled: isLoggedIn,
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
// 合并所有分页数据
|
|
// 合并所有分页数据
|
|
@@ -118,7 +117,7 @@ const HomePage: React.FC = () => {
|
|
|
name: goods?.name || '',
|
|
name: goods?.name || '',
|
|
|
cover_image: imageUrl,
|
|
cover_image: imageUrl,
|
|
|
price: goods?.price || 0,
|
|
price: goods?.price || 0,
|
|
|
- originPrice: goods?.originPrice || 0,
|
|
|
|
|
|
|
+ originPrice: (goods as any)?.originPrice || 0,
|
|
|
tags: (goods?.salesNum || 0) > 100 ? ['热销'] : ['新品'],
|
|
tags: (goods?.salesNum || 0) > 100 ? ['热销'] : ['新品'],
|
|
|
hasSpecOptions,
|
|
hasSpecOptions,
|
|
|
parentGoodsId,
|
|
parentGoodsId,
|
|
@@ -167,12 +166,30 @@ const HomePage: React.FC = () => {
|
|
|
// }
|
|
// }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 使用Taro全局钩子 - 下拉刷新
|
|
|
|
|
- usePullDownRefresh(() => {
|
|
|
|
|
|
|
+ // ScrollView下拉刷新处理函数
|
|
|
|
|
+ const handleRefresh = () => {
|
|
|
|
|
+ //console.log('首页下拉刷新触发 - ScrollView刷新处理函数被调用')
|
|
|
|
|
+ //console.log('refetch函数:', refetch)
|
|
|
|
|
+ //console.log('用户登录状态:', isLoggedIn)
|
|
|
|
|
+ setRefreshing(true)
|
|
|
refetch().finally(() => {
|
|
refetch().finally(() => {
|
|
|
|
|
+ //console.log('首页下拉刷新完成,停止刷新状态')
|
|
|
|
|
+ setRefreshing(false)
|
|
|
|
|
+ // 同时停止页面下拉刷新动画(如果同时启用)
|
|
|
Taro.stopPullDownRefresh()
|
|
Taro.stopPullDownRefresh()
|
|
|
})
|
|
})
|
|
|
- })
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 暂时注释页面级下拉刷新钩子,使用ScrollView自带刷新
|
|
|
|
|
+ // usePullDownRefresh(() => {
|
|
|
|
|
+ // console.log('首页下拉刷新触发 - 钩子被调用')
|
|
|
|
|
+ // console.log('refetch函数:', refetch)
|
|
|
|
|
+ // console.log('用户登录状态:', isLoggedIn)
|
|
|
|
|
+ // refetch().finally(() => {
|
|
|
|
|
+ // console.log('首页下拉刷新完成,停止刷新动画')
|
|
|
|
|
+ // Taro.stopPullDownRefresh()
|
|
|
|
|
+ // })
|
|
|
|
|
+ // })
|
|
|
|
|
|
|
|
// 分享功能
|
|
// 分享功能
|
|
|
useShareAppMessage(() => {
|
|
useShareAppMessage(() => {
|
|
@@ -183,14 +200,30 @@ const HomePage: React.FC = () => {
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- // // 商品点击
|
|
|
|
|
- // const handleGoodsClick = (goods: GoodsData, index: number) => {
|
|
|
|
|
- // console.log('点击商品:', goods, index)
|
|
|
|
|
- // }
|
|
|
|
|
|
|
+ // 如果未登录,不渲染页面内容
|
|
|
|
|
+ if (!isLoggedIn) return null
|
|
|
|
|
+
|
|
|
// 跳转到商品详情
|
|
// 跳转到商品详情
|
|
|
- const handleGoodsClick = (goods: Goods) => {
|
|
|
|
|
|
|
+ const handleGoodsClick = (goods: GoodsData, index: number) => {
|
|
|
|
|
+ console.log('点击商品:', goods, index)
|
|
|
|
|
+ // 安全解析商品ID:GoodsData.id是string类型,需要转换为number
|
|
|
|
|
+ let goodsId: number
|
|
|
|
|
+ if (typeof goods.id === 'number') {
|
|
|
|
|
+ goodsId = goods.id
|
|
|
|
|
+ } else if (typeof goods.id === 'string') {
|
|
|
|
|
+ goodsId = parseInt(goods.id, 10)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error('商品ID类型无效:', goods.id, typeof goods.id)
|
|
|
|
|
+ Taro.showToast({ title: '商品ID错误', icon: 'none' })
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isNaN(goodsId)) {
|
|
|
|
|
+ console.error('商品ID解析失败:', goods.id)
|
|
|
|
|
+ Taro.showToast({ title: '商品ID错误', icon: 'none' })
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
Taro.navigateTo({
|
|
Taro.navigateTo({
|
|
|
- url: `/pages/goods-detail/index?id=${goods.id}`
|
|
|
|
|
|
|
+ url: `/pages/goods-detail/index?id=${goodsId}`
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -242,11 +275,6 @@ const HomePage: React.FC = () => {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 商品图片点击
|
|
|
|
|
- const handleThumbClick = (goods: GoodsData, index: number) => {
|
|
|
|
|
- console.log('点击商品图片:', goods, index)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
// 搜索框点击
|
|
// 搜索框点击
|
|
|
const handleSearchClick = () => {
|
|
const handleSearchClick = () => {
|
|
|
Taro.navigateTo({
|
|
Taro.navigateTo({
|
|
@@ -265,9 +293,11 @@ const HomePage: React.FC = () => {
|
|
|
/>
|
|
/>
|
|
|
<ScrollView
|
|
<ScrollView
|
|
|
className="home-scroll-view"
|
|
className="home-scroll-view"
|
|
|
- scrollY
|
|
|
|
|
- onScrollToLower={handleScrollToLower}
|
|
|
|
|
- >
|
|
|
|
|
|
|
+ scrollY
|
|
|
|
|
+ refresherEnabled={true}
|
|
|
|
|
+ refresherTriggered={refreshing}
|
|
|
|
|
+ onRefresherRefresh={handleRefresh}
|
|
|
|
|
+ onScrollToLower={handleScrollToLower} >
|
|
|
{/* 页面头部 - 搜索栏和轮播图 */}
|
|
{/* 页面头部 - 搜索栏和轮播图 */}
|
|
|
<View className="home-page-header">
|
|
<View className="home-page-header">
|
|
|
{/* 搜索栏 */}
|
|
{/* 搜索栏 */}
|
|
@@ -295,7 +325,7 @@ const HomePage: React.FC = () => {
|
|
|
items={finalImgSrcs.filter(item => item.imageFile?.fullUrl).map(item => ({
|
|
items={finalImgSrcs.filter(item => item.imageFile?.fullUrl).map(item => ({
|
|
|
src: item.imageFile!.fullUrl,
|
|
src: item.imageFile!.fullUrl,
|
|
|
title: item.title || '',
|
|
title: item.title || '',
|
|
|
- description: item.description || ''
|
|
|
|
|
|
|
+ description: (item as any).description || ''
|
|
|
}))}
|
|
}))}
|
|
|
height={800}
|
|
height={800}
|
|
|
autoplay={true}
|
|
autoplay={true}
|
|
@@ -333,7 +363,6 @@ const HomePage: React.FC = () => {
|
|
|
goodsList={goodsList}
|
|
goodsList={goodsList}
|
|
|
onClick={handleGoodsClick}
|
|
onClick={handleGoodsClick}
|
|
|
onAddCart={handleAddCart}
|
|
onAddCart={handleAddCart}
|
|
|
- onThumbClick={handleThumbClick}
|
|
|
|
|
/>
|
|
/>
|
|
|
|
|
|
|
|
{/* 加载更多状态 */}
|
|
{/* 加载更多状态 */}
|