fix(shop-cart): 修复购物车后端查询及前后端字段名对齐问题
- 修复后端ShopCartMapper SQL,关联查询商品和SKU信息,返回完整商品详情字段 - 新增ShopCart实体非持久化字段,映射商品名称、图片、价格、库存等 - 调整后端Controller列表方法,自动过滤当前登录用户购物车数据 - 前端ShopCart接口新增salePrice、dealerPrice字段,CartContext映射新增字段 - 修正购物车数量和选中状态字段名,前端与后端统一使用cartNum和selected - 统一更新购物车数量、选中状态的接口调用参数,避免因字段不匹配导致更新失败 - 购物车及结算页价格显示逻辑优先使用后端扁平dealerPrice字段,兼容VIP价格显示 - 各购物车相关页面调整添加购物车参数,确保字段名一致和正确传递
This commit is contained in:
@@ -63,7 +63,7 @@ export async function addToCart(data: AddToCartParam) {
|
||||
export async function updateCartNum(data: UpdateCartNumParam) {
|
||||
const res = await request.put<ApiResult<unknown>>(
|
||||
'/shop/shop-cart',
|
||||
{ id: data.id, num: data.num }
|
||||
{ id: data.id, cartNum: data.cartNum }
|
||||
);
|
||||
if (res.code === 0) {
|
||||
return res.message || '更新成功';
|
||||
@@ -77,7 +77,7 @@ export async function updateCartNum(data: UpdateCartNumParam) {
|
||||
export async function updateCartChecked(id: number, checked: boolean) {
|
||||
const res = await request.put<ApiResult<unknown>>(
|
||||
'/shop/shop-cart',
|
||||
{ id, checked }
|
||||
{ id, selected: checked }
|
||||
);
|
||||
if (res.code === 0) {
|
||||
return res.message || '更新成功';
|
||||
|
||||
@@ -12,8 +12,8 @@ export interface ShopCart {
|
||||
goodsId?: number;
|
||||
// SKU ID
|
||||
skuId?: number;
|
||||
// 数量
|
||||
num?: number;
|
||||
// 数量(后端字段名 cartNum)
|
||||
cartNum?: number;
|
||||
// 商品名称
|
||||
goodsName?: string;
|
||||
// 商品图片
|
||||
@@ -24,6 +24,12 @@ export interface ShopCart {
|
||||
skuSpec?: string;
|
||||
// 库存
|
||||
stock?: number;
|
||||
// 市场价(划线价)
|
||||
salePrice?: string | number;
|
||||
// 经销商价(VIP专享)
|
||||
dealerPrice?: string | number;
|
||||
// 是否选中(后端字段名 selected)
|
||||
selected?: boolean;
|
||||
// 租户id
|
||||
tenantId?: number;
|
||||
// 创建时间
|
||||
@@ -52,8 +58,8 @@ export interface AddToCartParam {
|
||||
goodsId: number;
|
||||
// SKU ID(单规格可不传)
|
||||
skuId?: number;
|
||||
// 数量
|
||||
num: number;
|
||||
// 数量(后端字段名 cartNum)
|
||||
cartNum: number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,8 +68,8 @@ export interface AddToCartParam {
|
||||
export interface UpdateCartNumParam {
|
||||
// 购物车ID
|
||||
id: number;
|
||||
// 数量
|
||||
num: number;
|
||||
// 数量(后端字段名 cartNum)
|
||||
cartNum: number;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,6 +18,8 @@ export interface CartItem {
|
||||
skuPrice?: string
|
||||
skuSpec?: string
|
||||
stock?: number
|
||||
salePrice?: string | number
|
||||
dealerPrice?: string | number
|
||||
}
|
||||
|
||||
interface CartContextType {
|
||||
@@ -53,21 +55,20 @@ export const CartProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
||||
const data = await listShopCart()
|
||||
// 防御性检查:确保返回的是数组
|
||||
const safeData = Array.isArray(data) ? data : []
|
||||
// 转换为前端格式,默认都选中
|
||||
// 转换为前端格式
|
||||
const mappedItems: CartItem[] = safeData.map(item => ({
|
||||
id: item.id,
|
||||
goodsId: item.goodsId!,
|
||||
skuId: item.skuId,
|
||||
quantity: item.num || 1,
|
||||
quantity: item.cartNum || 1,
|
||||
goodsName: item.goodsName,
|
||||
goodsImage: item.goodsImage,
|
||||
skuPrice: item.skuPrice,
|
||||
skuSpec: item.skuSpec,
|
||||
stock: item.stock,
|
||||
// 后端可能返回嵌套的 product/sku 对象(对应 ShopCartItem 结构)
|
||||
product: (item as any).product,
|
||||
sku: (item as any).sku,
|
||||
checked: true,
|
||||
salePrice: item.salePrice,
|
||||
dealerPrice: item.dealerPrice,
|
||||
checked: item.selected !== false,
|
||||
}))
|
||||
setItems(mappedItems)
|
||||
} catch (err) {
|
||||
@@ -83,7 +84,7 @@ export const CartProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
||||
try {
|
||||
const params = {
|
||||
goodsId: product.goodsId!,
|
||||
num: quantity,
|
||||
cartNum: quantity,
|
||||
} as Record<string, unknown>
|
||||
// 只有多规格商品才传 skuId,且不能为 0
|
||||
if (sku?.id && sku.id > 0) {
|
||||
@@ -125,7 +126,7 @@ export const CartProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
||||
if (!item?.id) return
|
||||
|
||||
try {
|
||||
await updateCartNum({ id: item.id, num: quantity })
|
||||
await updateCartNum({ id: item.id, cartNum: quantity })
|
||||
// 本地更新
|
||||
setItems(prev => prev.map(i =>
|
||||
(i.goodsId === goodsId && i.skuId === skuId) ? { ...i, quantity } : i
|
||||
@@ -201,8 +202,8 @@ export const CartProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
||||
const calcPrice = (list: CartItem[]) => {
|
||||
const vip = isVipMember()
|
||||
return list.reduce((sum, i) => {
|
||||
// VIP 会员优先使用 dealerPrice
|
||||
const dealerPrice = vip ? (i.product as any)?.dealerPrice : null
|
||||
// VIP 会员优先使用 dealerPrice(后端关联字段 > product 嵌套对象)
|
||||
const dealerPrice = vip ? (i.dealerPrice || (i.product as any)?.dealerPrice) : null
|
||||
const unitPrice = dealerPrice || i.skuPrice || i.sku?.price || i.product?.salePrice || i.product?.price || 0
|
||||
return sum + Number(unitPrice) * i.quantity
|
||||
}, 0).toFixed(2)
|
||||
|
||||
@@ -182,7 +182,7 @@ const CartPage: React.FC = () => {
|
||||
)}
|
||||
<View className="flex justify-between items-center">
|
||||
<Text className="text-sm font-bold text-red-500">
|
||||
¥{isVipMember() && (item.product as any)?.dealerPrice ? (item.product as any).dealerPrice : (item.skuPrice || item.sku?.price || item.product?.salePrice || item.product?.price || '0')}
|
||||
¥{isVipMember() && (item.dealerPrice || (item.product as any)?.dealerPrice) ? (item.dealerPrice || (item.product as any).dealerPrice) : (item.skuPrice || item.sku?.price || item.salePrice || item.product?.salePrice || item.product?.price || '0')}
|
||||
</Text>
|
||||
<View className="flex items-center gap-3">
|
||||
<View className="w-6 h-6 rounded bg-gray-100 flex items-center justify-center" onClick={() => updateQuantity(item.goodsId, item.skuId, item.quantity - 1)}>
|
||||
|
||||
@@ -135,7 +135,7 @@ const CategoryPage: React.FC = () => {
|
||||
if (!requireLogin({ action: 'addToCart' })) return
|
||||
addToCart({
|
||||
goodsId: product.goodsId!,
|
||||
num: product.step || 1,
|
||||
cartNum: product.step || 1,
|
||||
}).then(() => {
|
||||
Taro.showToast({ title: '已加入购物车', icon: 'success' })
|
||||
}).catch(err => {
|
||||
|
||||
@@ -149,9 +149,9 @@ const CheckoutPage: React.FC = () => {
|
||||
if (!items || items.length === 0) return 0
|
||||
const vip = isVipMember()
|
||||
return items.reduce((sum, item) => {
|
||||
// VIP 会员优先使用 dealerPrice
|
||||
const dealerPrice = vip ? (item.product as any)?.dealerPrice : null
|
||||
const unitPrice = dealerPrice || item.skuPrice || item.sku?.price || item.product?.salePrice || item.product?.price || 0
|
||||
// VIP 会员优先使用 dealerPrice(后端关联字段 > product 嵌套对象)
|
||||
const dealerPrice = vip ? (item.dealerPrice || (item.product as any)?.dealerPrice) : null
|
||||
const unitPrice = dealerPrice || item.skuPrice || item.sku?.price || item.salePrice || item.product?.salePrice || item.product?.price || 0
|
||||
return sum + Number(unitPrice) * (item.quantity || item.num || 1)
|
||||
}, 0)
|
||||
}, [buyNowItems, selectedItems])
|
||||
@@ -368,7 +368,7 @@ const CheckoutPage: React.FC = () => {
|
||||
<View className='flex justify-between items-center mt-1'>
|
||||
<Text className='text-xs text-gray-500'>x{item.quantity || item.num || 1}</Text>
|
||||
<Text className='text-sm font-medium text-gray-800'>
|
||||
¥{isVipMember() && (item.product as any)?.dealerPrice ? (item.product as any).dealerPrice : (item.skuPrice || item.sku?.salePrice || item.sku?.price || item.product?.salePrice || item.product?.price || '0')}
|
||||
¥{isVipMember() && (item.dealerPrice || (item.product as any)?.dealerPrice) ? (item.dealerPrice || (item.product as any).dealerPrice) : (item.skuPrice || item.sku?.salePrice || item.sku?.price || item.salePrice || item.product?.salePrice || item.product?.price || '0')}
|
||||
</Text>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
@@ -147,7 +147,7 @@ const ShopPage: React.FC = () => {
|
||||
if (!requireLogin({ action: 'addToCart' })) return
|
||||
addToCart({
|
||||
goodsId: product.goodsId!,
|
||||
num: product.step || 1,
|
||||
cartNum: product.step || 1,
|
||||
}).then(() => {
|
||||
Taro.showToast({ title: '已加入购物车', icon: 'success' })
|
||||
}).catch(err => {
|
||||
|
||||
Reference in New Issue
Block a user