Explorar o código

✅ fix: 修复首页商品卡片加入购物车无响应问题

- 导入并使用 useCart hook 实现购物车功能
- 修改 handleAddCart 函数,实际调用 addToCart 方法
- 添加成功提示,与商品列表页保持一致

🤖 Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname hai 1 mes
pai
achega
94671a8058
Modificáronse 1 ficheiros con 18 adicións e 1 borrados
  1. 18 1
      mini/src/pages/index/index.tsx

+ 18 - 1
mini/src/pages/index/index.tsx

@@ -9,6 +9,7 @@ import { goodsClient, advertisementClient } from '@/api'
 import { InferResponseType } from 'hono'
 import './index.css'
 import { useAuth } from '@/utils/auth'
+import { useCart } from '@/utils/cart'
 import { Navbar } from '@/components/ui/navbar'
 import Taro from '@tarojs/taro'
 
@@ -19,6 +20,7 @@ type Advertisement = AdvertisementResponse['data'][0]
 
 const HomePage: React.FC = () => {
   const { isLoggedIn } = useAuth();
+  const { addToCart } = useCart();
   if( !isLoggedIn ) return null;
   
   // 广告数据查询
@@ -121,7 +123,22 @@ const HomePage: React.FC = () => {
 
   // 添加购物车
   const handleAddCart = (goods: GoodsData, index: number) => {
-    console.log('添加到购物车:', goods, index)
+    // 找到对应的原始商品数据
+    const originalGoods = allGoods.find(g => g.id.toString() === goods.id)
+    if (originalGoods) {
+      addToCart({
+        id: originalGoods.id,
+        name: originalGoods.name,
+        price: originalGoods.price,
+        image: originalGoods.imageFile?.fullUrl || '',
+        stock: originalGoods.stock,
+        quantity: 1
+      })
+      Taro.showToast({
+        title: '已添加到购物车',
+        icon: 'success'
+      })
+    }
   }
 
   // 商品图片点击