feat 对接购物车

This commit is contained in:
lwh 2023-08-06 16:17:16 +08:00
parent fa015b5cd4
commit 7272b0a14b
16 changed files with 378 additions and 145 deletions

View File

@ -1,31 +1,19 @@
<view class="cart-bar__placeholder" wx:if="{{fixed}}" /> <view class="cart-bar__placeholder" wx:if="{{fixed}}" />
<view class="cart-bar {{fixed ? 'cart-bar--fixed' : ''}} flex flex-v-center" style="bottom: {{fixed ? 'calc(' + bottomHeight + 'rpx + env(safe-area-inset-bottom))' : ''}};"> <view class="cart-bar {{fixed ? 'cart-bar--fixed' : ''}} flex flex-v-center" style="bottom: {{fixed ? 'calc(' + bottomHeight + 'rpx + env(safe-area-inset-bottom))' : ''}};">
<t-icon <t-icon size="40rpx" color="{{isAllSelected ? '#FA4126' : '#BBBBBB'}}" name="{{isAllSelected ? 'check-circle-filled' : 'circle'}}" class="cart-bar__check" catchtap="handleSelectAll" />
size="40rpx" <text>全选</text>
color="{{isAllSelected ? '#FA4126' : '#BBBBBB'}}" <view class="cart-bar__total flex1">
name="{{isAllSelected ? 'check-circle-filled' : 'circle'}}" <view>
class="cart-bar__check" <text class="cart-bar__total--bold text-padding-right">总计</text>
catchtap="handleSelectAll" <price priceUnit="yuan" price="{{totalAmount || '0'}}" fill="{{false}}" decimalSmaller class="cart-bar__total--bold cart-bar__total--price" />
/> <text class="cart-bar__total--normal">(不含运费)</text>
<text>全选</text> </view>
<view class="cart-bar__total flex1"> <view wx:if="{{totalDiscountAmount}}">
<view> <text class="cart-bar__total--normal text-padding-right">已优惠</text>
<text class="cart-bar__total--bold text-padding-right">总计</text> <price class="cart-bar__total--normal" price="{{totalDiscountAmount || '0'}}" fill="{{false}}" />
<price </view>
price="{{totalAmount || '0'}}" </view>
fill="{{false}}" <view catchtap="handleToSettle" class="{{!isDisabled ? '' : 'disabled-btn'}} account-btn" hover-class="{{!isDisabled ? '' : 'hover-btn'}}">
decimalSmaller 去结算({{totalGoodsNum}})
class="cart-bar__total--bold cart-bar__total--price" </view>
/> </view>
<text class="cart-bar__total--normal">(不含运费)</text>
</view>
<view wx:if="{{totalDiscountAmount}}">
<text class="cart-bar__total--normal text-padding-right">已优惠</text>
<price class="cart-bar__total--normal" price="{{totalDiscountAmount || '0'}}" fill="{{false}}" />
</view>
</view>
<view catchtap="handleToSettle" class="{{!isDisabled ? '' : 'disabled-btn'}} account-btn" hover-class="{{!isDisabled ? '' : 'hover-btn'}}">
去结算({{totalGoodsNum}})
</view>
</view>

View File

@ -44,7 +44,7 @@
width: 192rpx; width: 192rpx;
height: 80rpx; height: 80rpx;
border-radius: 40rpx; border-radius: 40rpx;
background-color: #fa4126; background-color: #1989fa;
font-size: 28rpx; font-size: 28rpx;
font-weight: bold; font-weight: bold;
line-height: 80rpx; line-height: 80rpx;

View File

@ -13,11 +13,15 @@ Component({
for (const store of storeGoods) { for (const store of storeGoods) {
for (const activity of store.promotionGoodsList) { for (const activity of store.promotionGoodsList) {
for (const goods of activity.goodsPromotionList) { for (const goods of activity.goodsPromotionList) {
goods.specs = goods.specInfo.map((item) => item.specValue); // 目前仅展示商品已选规格的值 if(goods.specInfo){
goods.specs = goods.specInfo.map((item) => item.specValue); // 目前仅展示商品已选规格的值
}
} }
} }
for (const goods of store.shortageGoodsList) { for (const goods of store.shortageGoodsList) {
goods.specs = goods.specInfo.map((item) => item.specValue); // 目前仅展示商品已选规格的值 if(goods.specInfo){
goods.specs = goods.specInfo.map((item) => item.specValue); // 目前仅展示商品已选规格的值
}
} }
} }
@ -28,7 +32,9 @@ Component({
type: Array, type: Array,
observer(invalidGoodItems) { observer(invalidGoodItems) {
invalidGoodItems.forEach((goods) => { invalidGoodItems.forEach((goods) => {
goods.specs = goods.specInfo.map((item) => item.specValue); // 目前仅展示商品已选规格的值 if(goods.specInfo){
goods.specs = goods.specInfo.map((item) => item.specValue); // 目前仅展示商品已选规格的值
}
}); });
this.setData({ _invalidGoodItems: invalidGoodItems }); this.setData({ _invalidGoodItems: invalidGoodItems });
}, },
@ -127,6 +133,14 @@ Component({
}); });
}, },
gotoShop(e){
const { storeIndex } = e.currentTarget.dataset;
const store = this.data.storeGoods[storeIndex];
wx.navigateTo({
url: "/pages/shop/all-goods/index?shopGuid=" + store.storeId,
});
},
// 展开/收起切换 // 展开/收起切换
showToggle() { showToggle() {
this.setData({ this.setData({
@ -136,12 +150,12 @@ Component({
// 展示规格popup // 展示规格popup
specsTap(e) { specsTap(e) {
this.isSpecsTap = true; // this.isSpecsTap = true;
const { goods } = e.currentTarget.dataset; // const { goods } = e.currentTarget.dataset;
this.setData({ // this.setData({
isShowSpecs: true, // isShowSpecs: true,
currentGoods: goods, // currentGoods: goods,
}); // });
}, },
hideSpecsPopup() { hideSpecsPopup() {

View File

@ -2,8 +2,8 @@
<wxs src="./utils.wxs" module="utils" /> <wxs src="./utils.wxs" module="utils" />
<view class="cart-group"> <view class="cart-group">
<view class="goods-wrap" wx:for="{{_storeGoods}}" wx:for-item="store" wx:for-index="si" wx:key="storeId"> <view class="goods-wrap" wx:for="{{_storeGoods}}" wx:for-item="store" wx:for-index="si" wx:key="storeId" data-store-index="{{si}}" bindtap="gotoShop">
<view class="cart-store"> <view class="cart-store" >
<t-icon <t-icon
size="40rpx" size="40rpx"
color="{{store.isSelected ? '#FA4126' : '#BBBBBB'}}" color="{{store.isSelected ? '#FA4126' : '#BBBBBB'}}"
@ -17,7 +17,7 @@
<t-icon prefix="wr" size="40rpx" color="#333333" name="store" /> <t-icon prefix="wr" size="40rpx" color="#333333" name="store" />
<view class="store-name">{{store.storeName}}</view> <view class="store-name">{{store.storeName}}</view>
</view> </view>
<view class="get-coupon" catch:tap="gotoCoupons">优惠券</view> <!-- <view class="get-coupon" catch:tap="gotoCoupons">优惠券</view> -->
</view> </view>
</view> </view>
<block wx:for="{{store.promotionGoodsList}}" wx:for-item="promotion" wx:for-index="promoindex" wx:key="promoindex"> <block wx:for="{{store.promotionGoodsList}}" wx:for-item="promotion" wx:for-index="promoindex" wx:key="promoindex">
@ -145,7 +145,7 @@
price="{{currentGoods.price || ''}}" price="{{currentGoods.price || ''}}"
thumb="{{utils.imgCut(currentGoods.thumb, 180, 180)}}" thumb="{{utils.imgCut(currentGoods.thumb, 180, 180)}}"
specs="{{currentGoods.specs || []}}" specs="{{currentGoods.specs || []}}"
zIndex="{{999}}" zIndex="{{99}}"
bindclose="hideSpecsPopup" bindclose="hideSpecsPopup"
/> />

View File

@ -8,12 +8,18 @@
<view class="wr-goods-card__main"> <view class="wr-goods-card__main">
<view class="wr-goods-card__thumb thumb-class" bind:tap="clickThumbHandle"> <view class="wr-goods-card__thumb thumb-class" bind:tap="clickThumbHandle">
<!-- data-src 是方便加购动画读取图片用的 --> <!-- data-src 是方便加购动画读取图片用的 -->
<t-image <!-- <t-image
t-class="wr-goods-card__thumb-com" t-class="wr-goods-card__thumb-com"
wx:if="{{ !!goods.thumb && !goods.hideKey.thumb }}" wx:if="{{ !!goods.thumb && !goods.hideKey.thumb }}"
src="{{ goods.thumb }}" src="{{ goods.thumb }}"
mode="{{ thumbMode }}" mode="{{ thumbMode }}"
lazy-load="{{ lazyLoad }}" lazy-load="{{ lazyLoad }}"
/> -->
<image
class="wr-goods-card__thumb-com"
wx:if="{{ !!goods.thumb && !goods.hideKey.thumb }}"
src="{{ goods.thumb }}"
mode="{{ thumbMode }}"
/> />
<slot name="thumb-cover" /> <slot name="thumb-cover" />
</view> </view>
@ -38,6 +44,7 @@
<slot name="price-prefix" /> <slot name="price-prefix" />
<view wx:if="{{ goods.price && !goods.hideKey.price }}" class="wr-goods-card__price"> <view wx:if="{{ goods.price && !goods.hideKey.price }}" class="wr-goods-card__price">
<price <price
priceUnit="yuan"
wr-class="price-class" wr-class="price-class"
symbol="{{currency}}" symbol="{{currency}}"
price="{{goods.price}}" price="{{goods.price}}"
@ -48,6 +55,7 @@
<view wx:if="{{ goods.originPrice && !goods.hideKey.originPrice && isValidityLinePrice }}" class="wr-goods-card__origin-price"> <view wx:if="{{ goods.originPrice && !goods.hideKey.originPrice && isValidityLinePrice }}" class="wr-goods-card__origin-price">
<price <price
wr-class="origin-price-class" wr-class="origin-price-class"
priceUnit="yuan"
symbol="{{currency}}" symbol="{{currency}}"
price="{{goods.originPrice}}" price="{{goods.originPrice}}"
fill="{{priceFill}}" fill="{{priceFill}}"

View File

@ -1,6 +1,20 @@
import Dialog from 'tdesign-miniprogram/dialog/index'; import Dialog from 'tdesign-miniprogram/dialog/index';
import Toast from 'tdesign-miniprogram/toast/index'; import Toast from 'tdesign-miniprogram/toast/index';
import { fetchCartGroupData } from '../../services/cart/cart'; import {
fetchCartGroupData
} from '~/services/cart/cart';
import {
updateCartGoodsNum
} from '~/services/cart/updateCartGoodsNum';
import {
updateCartGoodsSelect
} from '~/services/cart/updateCartGoodsSelect';
import {
selectAllCartGoods
} from '~/services/cart/selectAllCartGoods';
import {
deleteCart
} from '~/services/cart/deleteCart';
Page({ Page({
data: { data: {
@ -9,6 +23,7 @@ Page({
// 调用自定义tabbar的init函数使页面与tabbar激活状态保持一致 // 调用自定义tabbar的init函数使页面与tabbar激活状态保持一致
onShow() { onShow() {
this.refreshData();
this.getTabBar().init(); this.getTabBar().init();
}, },
@ -20,7 +35,6 @@ Page({
this.getCartGroupData().then((res) => { this.getCartGroupData().then((res) => {
let isEmpty = true; let isEmpty = true;
const cartGroupData = res; const cartGroupData = res;
console.log(cartGroupData,'1231232');
// 一些组件中需要的字段可能接口并没有返回,或者返回的数据结构与预期不一致,需要在此先对数据做一些处理 // 一些组件中需要的字段可能接口并没有返回,或者返回的数据结构与预期不一致,需要在此先对数据做一些处理
// 统计门店下加购的商品是否全选、是否存在缺货/无货 // 统计门店下加购的商品是否全选、是否存在缺货/无货
for (const store of cartGroupData.storeGoods) { for (const store of cartGroupData.storeGoods) {
@ -62,7 +76,9 @@ Page({
// return goods; // return goods;
// }); // });
cartGroupData.isNotEmpty = !isEmpty; cartGroupData.isNotEmpty = !isEmpty;
this.setData({ cartGroupData }); this.setData({
cartGroupData
});
}); });
}, },
@ -70,7 +86,9 @@ Page({
let currentStore; let currentStore;
let currentActivity; let currentActivity;
let currentGoods; let currentGoods;
const { storeGoods } = this.data.cartGroupData; const {
storeGoods
} = this.data.cartGroupData;
for (const store of storeGoods) { for (const store of storeGoods) {
for (const activity of store.promotionGoodsList) { for (const activity of store.promotionGoodsList) {
for (const goods of activity.goodsPromotionList) { for (const goods of activity.goodsPromotionList) {
@ -96,68 +114,59 @@ Page({
// 注:实际场景时应该调用接口获取购物车数据 // 注:实际场景时应该调用接口获取购物车数据
getCartGroupData() { getCartGroupData() {
const { cartGroupData } = this.data; return fetchCartGroupData();
if (!cartGroupData) {
return fetchCartGroupData();
}
return Promise.resolve({ data: cartGroupData });
}, },
// 选择单个商品 // 选择单个商品
// 注:实际场景时应该调用接口更改选中状态 // 注:实际场景时应该调用接口更改选中状态
selectGoodsService({ spuId, skuId, isSelected }) { selectGoodsService(
this.findGoods(spuId, skuId).currentGoods.isSelected = isSelected; cartId,
return Promise.resolve(); isSelected
) {
let data = {
cartId: cartId,
isSelected: isSelected,
}
return updateCartGoodsSelect(data);
}, },
// 全选门店 // 全选门店
// 注:实际场景时应该调用接口更改选中状态 // 注:实际场景时应该调用接口更改选中状态
selectStoreService({ storeId, isSelected }) { selectStoreService({
storeId,
isSelected
}) {
const currentStore = this.data.cartGroupData.storeGoods.find((s) => s.storeId === storeId); const currentStore = this.data.cartGroupData.storeGoods.find((s) => s.storeId === storeId);
currentStore.isSelected = isSelected; currentStore.isSelected = isSelected;
currentStore.promotionGoodsList.forEach((activity) => { currentStore.promotionGoodsList.forEach((activity) => {
activity.goodsPromotionList.forEach((goods) => { activity.goodsPromotionList.forEach((goods) => {
goods.isSelected = isSelected; console.log(goods.cartId,'asdasd');
this.selectGoodsService(goods.cartId, isSelected)
}); });
}); });
return Promise.resolve(); return Promise.resolve();
}, },
// 加购数量变更 // 加购数量变更接口
// 注:实际场景时应该调用接口 changeQuantityService({
changeQuantityService({ spuId, skuId, quantity }) { cartId,
this.findGoods(spuId, skuId).currentGoods.quantity = quantity; quantity
return Promise.resolve(); }) {
// this.findGoods(spuId, skuId).currentGoods.quantity = quantity;
let data = {
cartId: cartId,
cartGoodsNum: quantity,
}
return updateCartGoodsNum(data).then((res) => {
this.refreshData();
})
}, },
// 删除加购商品 // 删除加购商品
// 注:实际场景时应该调用接口 // 注:实际场景时应该调用接口
deleteGoodsService({ spuId, skuId }) { deleteGoodsService(cartId) {
function deleteGoods(group) { // console.log(cartId,'删除的id');
for (const gindex in group) { return deleteCart(cartId)
const goods = group[gindex];
if (goods.spuId === spuId && goods.skuId === skuId) {
group.splice(gindex, 1);
return gindex;
}
}
return -1;
}
const { storeGoods, invalidGoodItems } = this.data.cartGroupData;
for (const store of storeGoods) {
for (const activity of store.promotionGoodsList) {
if (deleteGoods(activity.goodsPromotionList) > -1) {
return Promise.resolve();
}
}
if (deleteGoods(store.shortageGoodsList) > -1) {
return Promise.resolve();
}
}
if (deleteGoods(invalidGoodItems) > -1) {
return Promise.resolve();
}
return Promise.reject();
}, },
// 清空失效商品 // 清空失效商品
@ -169,35 +178,59 @@ Page({
onGoodsSelect(e) { onGoodsSelect(e) {
const { const {
goods: { spuId, skuId }, goods: {
cartId,
spuId,
skuId
},
isSelected, isSelected,
} = e.detail; } = e.detail;
const { currentGoods } = this.findGoods(spuId, skuId); // const {
Toast({ // currentGoods
context: this, // } = this.findGoods(spuId, skuId);
selector: '#t-toast', // Toast({
message: `${isSelected ? '选择' : '取消'}"${ // context: this,
currentGoods.title.length > 5 ? `${currentGoods.title.slice(0, 5)}...` : currentGoods.title // selector: '#t-toast',
}"`, // message: `${isSelected ? '选择' : '取消'}"${
icon: '', // currentGoods.title.length > 5 ? `${currentGoods.title.slice(0, 5)}...` : currentGoods.title
// }"`,
// icon: '',
// });
this.selectGoodsService(
cartId,
isSelected
).then(() => {
this.refreshData()
}); });
this.selectGoodsService({ spuId, skuId, isSelected }).then(() => this.refreshData());
}, },
onStoreSelect(e) { onStoreSelect(e) {
const { const {
store: { storeId }, store: {
storeId
},
isSelected, isSelected,
} = e.detail; } = e.detail;
this.selectStoreService({ storeId, isSelected }).then(() => this.refreshData()); this.selectStoreService({
storeId,
isSelected
}).then(() => this.refreshData());
}, },
// 修改购物车的商品数量
onQuantityChange(e) { onQuantityChange(e) {
console.log(e.detail, '加够的信息');
const { const {
goods: { spuId, skuId }, goods: {
spuId,
skuId,
cartId
},
quantity, quantity,
} = e.detail; } = e.detail;
const { currentGoods } = this.findGoods(spuId, skuId); const {
currentGoods
} = this.findGoods(spuId, skuId);
const stockQuantity = currentGoods.stockQuantity > 0 ? currentGoods.stockQuantity : 0; // 避免后端返回的是-1 const stockQuantity = currentGoods.stockQuantity > 0 ? currentGoods.stockQuantity : 0; // 避免后端返回的是-1
// 加购数量超过库存数量 // 加购数量超过库存数量
if (quantity > stockQuantity) { if (quantity > stockQuantity) {
@ -211,22 +244,24 @@ Page({
return; return;
} }
Dialog.confirm({ Dialog.confirm({
title: '商品库存不足', title: '商品库存不足',
content: `当前商品库存不足,最大可购买数量为${stockQuantity}`, content: `当前商品库存不足,最大可购买数量为${stockQuantity}`,
confirmBtn: '修改为最大可购买数量', confirmBtn: '修改为最大可购买数量',
cancelBtn: '取消', cancelBtn: '取消',
}) })
.then(() => { .then(() => {
this.changeQuantityService({ this.changeQuantityService({
spuId, cartId,
skuId, stockQuantity,
quantity: stockQuantity, });
}).then(() => this.refreshData());
}) })
.catch(() => {}); .catch(() => {});
return; return;
} }
this.changeQuantityService({ spuId, skuId, quantity }).then(() => this.refreshData()); this.changeQuantityService({
cartId,
quantity
});
}, },
goCollect() { goCollect() {
@ -238,7 +273,10 @@ Page({
}, },
goGoodsDetail(e) { goGoodsDetail(e) {
const { spuId, storeId } = e.detail.goods; const {
spuId,
storeId
} = e.detail.goods;
wx.navigateTo({ wx.navigateTo({
url: `/pages/goods/details/index?spuId=${spuId}&storeId=${storeId}`, url: `/pages/goods/details/index?spuId=${spuId}&storeId=${storeId}`,
}); });
@ -251,28 +289,44 @@ Page({
onGoodsDelete(e) { onGoodsDelete(e) {
const { const {
goods: { spuId, skuId }, goods: {
cartId
},
} = e.detail; } = e.detail;
Dialog.confirm({ Dialog.confirm({
content: '确认删除该商品吗?', content: '确认删除该商品吗?',
confirmBtn: '确定', confirmBtn: '确定',
cancelBtn: '取消', cancelBtn: '取消',
}).then(() => { }).then(() => {
this.deleteGoodsService({ spuId, skuId }).then(() => { this.deleteGoodsService(cartId).then((res) => {
Toast({ context: this, selector: '#t-toast', message: '商品删除成功' }); if (res.code === 200) {
this.refreshData(); Toast({
context: this,
selector: '#t-toast',
message: '商品删除成功'
});
this.refreshData();
}
}); });
}); });
}, },
onSelectAll(event) { onSelectAll(event) {
const { isAllSelected } = event?.detail ?? {}; const {
Toast({ isAllSelected
context: this, } = event?.detail ?? {};
selector: '#t-toast', // Toast({
message: `${isAllSelected ? '取消' : '点击'}了全选按钮`, // context: this,
}); // selector: '#t-toast',
// message: `${isAllSelected ? '取消' : '点击'}了全选按钮`,
// });
// console.log(isAllSelected);
// 调用接口改变全选 // 调用接口改变全选
selectAllCartGoods({isSelected: !isAllSelected}).then((res) => {
if(res.code === 200){
this.refreshData()
}
})
}, },
onToSettle() { onToSettle() {
@ -287,9 +341,13 @@ Page({
}); });
}); });
wx.setStorageSync('order.goodsRequestList', JSON.stringify(goodsRequestList)); wx.setStorageSync('order.goodsRequestList', JSON.stringify(goodsRequestList));
wx.navigateTo({ url: '/pages/order/order-confirm/index?type=cart' }); wx.navigateTo({
url: '/pages/order/order-confirm/index?type=cart'
});
}, },
onGotoHome() { onGotoHome() {
wx.switchTab({ url: '/pages/home/home' }); wx.switchTab({
url: '/pages/home/home'
});
}, },
}); });

View File

@ -1,5 +1,5 @@
<!-- 分层购物车 --> <!-- 分层购物车 -->
<block wx:if="{{cartGroupData.isNotEmpty}}"> <block wx:if="{{cartGroupData.isNotEmpty}}" class="cart-box">
<cart-group <cart-group
store-goods="{{ cartGroupData.storeGoods }}" store-goods="{{ cartGroupData.storeGoods }}"
invalid-good-items="{{ cartGroupData.invalidGoodItems }}" invalid-good-items="{{ cartGroupData.invalidGoodItems }}"
@ -20,7 +20,7 @@
total-goods-num="{{cartGroupData.selectedGoodsCount}}" total-goods-num="{{cartGroupData.selectedGoodsCount}}"
total-discount-amount="{{cartGroupData.totalDiscountAmount}}" total-discount-amount="{{cartGroupData.totalDiscountAmount}}"
fixed="{{true}}" fixed="{{true}}"
bottomHeight="{{112}}" bottomHeight="{{110}}"
bindhandleSelectAll="onSelectAll" bindhandleSelectAll="onSelectAll"
bindhandleToSettle="onToSettle" bindhandleToSettle="onToSettle"
/> />

View File

@ -2,12 +2,13 @@
padding-bottom: 100rpx; padding-bottom: 100rpx;
} }
.cart-box{
margin-bottom: 1000rpx;
}
.gap { .gap {
height: 100rpx; height: 100rpx;
width: 100%; width: 100%;
} }
.t-button {
--td-button-default-color: #000;
--td-button-primary-text-color: #fa4126;
}

View File

@ -338,6 +338,7 @@ Component({
this.setData({ this.setData({
buyNum: value, buyNum: value,
}); });
this.setBuyNum(value)
}, },
}, },
}); });

View File

@ -2,6 +2,9 @@ import Toast from 'tdesign-miniprogram/toast/index';
import { import {
fetchGood fetchGood
} from '~/services/good/fetchGood'; } from '~/services/good/fetchGood';
import {
addOrUpdateCart
} from '~/services/cart/addOrUpdateCart';
import { import {
fetchActivityList fetchActivityList
} from '~/services/activity/fetchActivityList'; } from '~/services/activity/fetchActivityList';
@ -77,6 +80,7 @@ Page({
specImg: '', specImg: '',
isSpuSelectPopupShow: false, isSpuSelectPopupShow: false,
isAllSelectedSku: false, isAllSelectedSku: false,
selectedSkuId: 0,
buyType: 0, buyType: 0,
outOperateStatus: false, // 是否外层加入购物车 outOperateStatus: false, // 是否外层加入购物车
operateType: 0, operateType: 0,
@ -193,7 +197,9 @@ Page({
}); });
this.selectSpecsName(selectedSkuValues.length > 0 ? selectedAttrStr : ''); this.selectSpecsName(selectedSkuValues.length > 0 ? selectedAttrStr : '');
if (skuItem) { if (skuItem) {
console.log(skuItem[0]?.skuId,'选中的item');
this.setData({ this.setData({
selectedSkuId: skuItem[0]?.skuId,
selectItem: skuItem, selectItem: skuItem,
selectSkuSellsPrice: skuItem[0]?.priceInfo[0].price || 0, selectSkuSellsPrice: skuItem[0]?.priceInfo[0].price || 0,
selectSkuLinePrice: skuItem[0]?.priceInfo[1].price || 0, selectSkuLinePrice: skuItem[0]?.priceInfo[1].price || 0,
@ -215,6 +221,7 @@ Page({
getSelectedSkuValues(skuTree, selectedSku) { getSelectedSkuValues(skuTree, selectedSku) {
const normalizedTree = this.normalizeSkuTree(skuTree); const normalizedTree = this.normalizeSkuTree(skuTree);
return Object.keys(selectedSku).reduce((selectedValues, skuKeyStr) => { return Object.keys(selectedSku).reduce((selectedValues, skuKeyStr) => {
console.log(selectedSku,'sku啊');
const skuValues = normalizedTree[skuKeyStr]; const skuValues = normalizedTree[skuKeyStr];
const skuValueId = selectedSku[skuKeyStr]; const skuValueId = selectedSku[skuKeyStr];
if (skuValueId !== '') { if (skuValueId !== '') {
@ -247,29 +254,75 @@ Page({
} }
}, },
/** 加入购物车 */
addCart() { addCart() {
const { const {
isAllSelectedSku isAllSelectedSku,
} = this.data; } = this.data;
const { const {
specList specList,
} = this.data.details; } = this.data.details;
if (specList.length != 0) { if (specList.length != 0) {
Toast({ if (isAllSelectedSku) {
context: this, this.addCartFun()
selector: '#t-toast', } else {
message: isAllSelectedSku ? '点击加入购物车' : '请选择规格', Toast({
icon: '', context: this,
duration: 1000, selector: '#t-toast',
}); message: '请选择规格',
icon: '',
duration: 1000,
});
}
} }
else{
this.addCartFun()
}
},
addCartFun(){
const {
isAllSelectedSku,
buyNum,
selectedSkuId
} = this.data;
const {
specList,
shopGuid,
spuId,
} = this.data.details;
let data = {
shopGuid: shopGuid,
goodsGuid: spuId,
goodsSkuId: selectedSkuId,
cartGoodsNum: buyNum
}
console.log(data, '加入购物车的数据');
// 添加购物车接口
addOrUpdateCart(data).then((res) => {
if (res.code == 200) {
Toast({
context: this,
selector: '#t-toast',
message: '加入购物车成功',
icon: '',
duration: 1000,
});
this.setData({
isSpuSelectPopupShow: false,
});
}
})
}, },
gotoBuy(type) { gotoBuy(type) {
console.log(type); console.log(type);
const { const {
isAllSelectedSku, isAllSelectedSku,
buyNum buyNum,
} = this.data; } = this.data;
const { const {
specList specList
@ -315,7 +368,7 @@ Page({
}); });
}, },
gotoShop(){ gotoShop() {
const { const {
shopGuid shopGuid
} = this.data.details; } = this.data.details;
@ -506,7 +559,7 @@ Page({
const { const {
spuId spuId
} = query; } = query;
// const spuId = "1680467515018448896"; // const spuId = "1672964367177617408";
this.setData({ this.setData({
spuId: spuId, spuId: spuId,

View File

@ -0,0 +1,21 @@
import {
request
} from '../_utils/request';
/** 添加购物车 */
export function addOrUpdateCart(data) {
return new Promise((resolve, reject) => {
request({
url: `CartApi/addOrUpdateCart`,
method: 'POST',
data: data,
success: function (res) {
resolve(res);
},
fail: function (error) {
reject(error);
}
});
});
}

View File

@ -0,0 +1,21 @@
import {
request
} from '../_utils/request';
/** 删除购物车记录 */
export function deleteCart(ids) {
return new Promise((resolve, reject) => {
request({
url: `CartApi/` + ids,
method: 'DELETE',
success: function (res) {
resolve(res);
},
fail: function (error) {
reject(error);
}
});
});
}

View File

@ -0,0 +1,21 @@
import {
request
} from '../_utils/request';
/** 全选购物车商品 */
export function selectAllCartGoods(data) {
return new Promise((resolve, reject) => {
request({
url: `CartApi/selectAllCartGoods`,
method: 'POST',
data: data,
success: function (res) {
resolve(res);
},
fail: function (error) {
reject(error);
}
});
});
}

View File

@ -0,0 +1,21 @@
import {
request
} from '../_utils/request';
/** 修改购物车商品数量 */
export function updateCartGoodsNum(data) {
return new Promise((resolve, reject) => {
request({
url: `CartApi/updateCartGoodsNum`,
method: 'POST',
data: data,
success: function (res) {
resolve(res);
},
fail: function (error) {
reject(error);
}
});
});
}

View File

@ -0,0 +1,21 @@
import {
request
} from '../_utils/request';
/** 修改购物车商品数量 */
export function updateCartGoodsSelect(data) {
return new Promise((resolve, reject) => {
request({
url: `CartApi/updateCartGoodsSelect`,
method: 'POST',
data: data,
success: function (res) {
resolve(res);
},
fail: function (error) {
reject(error);
}
});
});
}

View File

@ -47,6 +47,11 @@
--td-tab-track-color: #1989fa; --td-tab-track-color: #1989fa;
} }
.t-button {
--td-button-default-color: #000;
--td-button-primary-text-color: #1989fa;
}
.dialog__button-confirm { .dialog__button-confirm {
color: #1989fa !important; color: #1989fa !important;
} }