|
|
@@ -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'
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 商品图片点击
|