|
@@ -103,6 +103,30 @@ export default function GoodsDetailPage() {
|
|
|
staleTime: 5 * 60 * 1000,
|
|
staleTime: 5 * 60 * 1000,
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+ // 获取子商品列表,用于判断是否有规格选项
|
|
|
|
|
+ const { data: childGoodsData } = useQuery({
|
|
|
|
|
+ queryKey: ['goods', goodsId, 'children'],
|
|
|
|
|
+ queryFn: async () => {
|
|
|
|
|
+ const response = await goodsClient[':id'].children.$get({
|
|
|
|
|
+ param: { id: goodsId },
|
|
|
|
|
+ query: {
|
|
|
|
|
+ page: 1,
|
|
|
|
|
+ pageSize: 100, // 获取所有子商品,假设不会超过100个
|
|
|
|
|
+ sortBy: 'createdAt',
|
|
|
|
|
+ sortOrder: 'ASC'
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ if (response.status !== 200) {
|
|
|
|
|
+ throw new Error('获取子商品列表失败')
|
|
|
|
|
+ }
|
|
|
|
|
+ return response.json()
|
|
|
|
|
+ },
|
|
|
|
|
+ enabled: goodsId > 0,
|
|
|
|
|
+ staleTime: 5 * 60 * 1000,
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ const hasSpecOptions = childGoodsData && childGoodsData.data && childGoodsData.data.length > 0
|
|
|
|
|
+
|
|
|
// 商品轮播图
|
|
// 商品轮播图
|
|
|
const carouselItems = goods?.slideImages?.map((file: any) => ({
|
|
const carouselItems = goods?.slideImages?.map((file: any) => ({
|
|
|
src: file.fullUrl || '',
|
|
src: file.fullUrl || '',
|
|
@@ -478,14 +502,26 @@ export default function GoodsDetailPage() {
|
|
|
<Button
|
|
<Button
|
|
|
className="add-cart-btn"
|
|
className="add-cart-btn"
|
|
|
onClick={handleAddToCart}
|
|
onClick={handleAddToCart}
|
|
|
- disabled={goods.stock <= 0}
|
|
|
|
|
|
|
+ disabled={
|
|
|
|
|
+ !goods
|
|
|
|
|
+ ? true
|
|
|
|
|
+ : hasSpecOptions
|
|
|
|
|
+ ? !selectedSpec || selectedSpec.stock <= 0
|
|
|
|
|
+ : goods.stock <= 0
|
|
|
|
|
+ }
|
|
|
>
|
|
>
|
|
|
加入购物车
|
|
加入购物车
|
|
|
</Button>
|
|
</Button>
|
|
|
<Button
|
|
<Button
|
|
|
className="buy-now-btn"
|
|
className="buy-now-btn"
|
|
|
onClick={handleBuyNow}
|
|
onClick={handleBuyNow}
|
|
|
- disabled={goods.stock <= 0}
|
|
|
|
|
|
|
+ disabled={
|
|
|
|
|
+ !goods
|
|
|
|
|
+ ? true
|
|
|
|
|
+ : hasSpecOptions
|
|
|
|
|
+ ? !selectedSpec || selectedSpec.stock <= 0
|
|
|
|
|
+ : goods.stock <= 0
|
|
|
|
|
+ }
|
|
|
>
|
|
>
|
|
|
立即购买
|
|
立即购买
|
|
|
</Button>
|
|
</Button>
|