|
@@ -16,7 +16,7 @@
|
|
|
</view>
|
|
|
</view>
|
|
|
<u-col span="3">
|
|
|
- <view class="icon-container-r">
|
|
|
+ <view class="icon-container-r" @click="onMyInfo">
|
|
|
<u-icon label="我的" labelPos="bottom" size="32" :name="iconPeople"></u-icon>
|
|
|
</view>
|
|
|
</u-col>
|
|
@@ -64,14 +64,15 @@
|
|
|
</specPopup>
|
|
|
<view class="cart-bottom padding-sm dflex-b">
|
|
|
<view class="cart padding-lr">
|
|
|
- <uni-badge size="small" :text="100" absolute="rightTop">
|
|
|
+ <uni-badge size="small" :text="buyCount" absolute="rightTop">
|
|
|
<u-icon name="shopping-cart-fill" size="28" color="#FF873D" @click="showCart"></u-icon>
|
|
|
</uni-badge>
|
|
|
<text class="cart-total">总计:¥{{total}}</text>
|
|
|
</view>
|
|
|
- <view class="balance dflex-c background-gradient">去结算</view>
|
|
|
+ <view class="balance dflex-c background-gradient" @click="toBuy">去结算</view>
|
|
|
</view>
|
|
|
- <cartPopupVue ref="cartPopup" :carList="carList" @numChanged="numChanged" />
|
|
|
+ <cartPopupVue ref="cartPopup" :carList="carList" @selected-changed="handleSelectedChanged"
|
|
|
+ @numChanged="numChanged" />
|
|
|
|
|
|
</view>
|
|
|
</template>
|
|
@@ -100,55 +101,63 @@
|
|
|
{
|
|
|
id: 1,
|
|
|
goods_name: "小乳酸菌牛奶酸奶",
|
|
|
- goods_price: "5.20",
|
|
|
+ goods_price: 5.20,
|
|
|
goods_type: '大份',
|
|
|
goods_num: 1,
|
|
|
src: '../../static/x0.jpg',
|
|
|
+
|
|
|
},
|
|
|
{
|
|
|
id: 2,
|
|
|
goods_name: "饮料整箱饮品",
|
|
|
- goods_price: "5.20",
|
|
|
+ goods_price: 5.20,
|
|
|
goods_type: '大份',
|
|
|
goods_num: 1,
|
|
|
src: '../../static/x1.jpg',
|
|
|
+
|
|
|
},
|
|
|
{
|
|
|
id: 3,
|
|
|
goods_name: "饮品早餐酸乳益生菌",
|
|
|
- goods_price: "5.20",
|
|
|
+ goods_price: 5.20,
|
|
|
goods_type: '小份',
|
|
|
goods_num: 1,
|
|
|
src: '../../static/x0.jpg',
|
|
|
+
|
|
|
},
|
|
|
{
|
|
|
id: 4,
|
|
|
goods_name: "酸奶饮料",
|
|
|
- goods_price: "5.20",
|
|
|
+ goods_price: 5.20,
|
|
|
goods_type: '小份',
|
|
|
goods_num: 1,
|
|
|
src: '../../static/x1.jpg',
|
|
|
+
|
|
|
},
|
|
|
{
|
|
|
id: 5,
|
|
|
goods_name: "小乳酸菌",
|
|
|
- goods_price: "5.20",
|
|
|
+ goods_price: 5.20,
|
|
|
goods_type: '小份',
|
|
|
goods_num: 1,
|
|
|
src: '../../static/x0.jpg',
|
|
|
+
|
|
|
},
|
|
|
{
|
|
|
id: 6,
|
|
|
goods_name: "酸乳益生菌",
|
|
|
- goods_price: "5.20",
|
|
|
+ goods_price: 5.20,
|
|
|
goods_type: '大份',
|
|
|
goods_num: 1,
|
|
|
src: '../../static/x1.jpg',
|
|
|
+
|
|
|
}
|
|
|
],
|
|
|
total: 0,
|
|
|
carList: [],
|
|
|
- cardMsg: {}
|
|
|
+ cardMsg: {},
|
|
|
+ selectedList: [],
|
|
|
+ buyCount: 0
|
|
|
}
|
|
|
},
|
|
|
created() {
|
|
@@ -162,32 +171,42 @@
|
|
|
this.$refs.specPopup.open();
|
|
|
//this.$refs.specPopup.change(true);
|
|
|
},
|
|
|
+ handleSelectedChanged(selectedItems) {
|
|
|
+ console.log('选中的数据:', selectedItems)
|
|
|
+ this.carList = selectedItems
|
|
|
+ // this.buyCount = this.carList.length
|
|
|
+ this.totalPrice();
|
|
|
+ },
|
|
|
handleUpdateShopMsg(shopMsg) {
|
|
|
- console.log('接收到的购物信息:', shopMsg);
|
|
|
- const specString = shopMsg.spec + ',' + shopMsg.specs;
|
|
|
- const item = this.goods.find(item => item.id === shopMsg.id);
|
|
|
-
|
|
|
- if (item) {
|
|
|
- // 检查购物车列表中是否已存在相同id和规格的商品
|
|
|
- const existingCartItem = this.carList.find(cartItem => cartItem.id === item.id && cartItem.goods_type === specString);
|
|
|
-
|
|
|
- if (existingCartItem) {
|
|
|
- // 如果已存在,则增加商品数量
|
|
|
- existingCartItem.goods_num += shopMsg.num;
|
|
|
- } else {
|
|
|
- // 如果不存在,则复制item并添加到购物车列表,同时设置商品数量和规格
|
|
|
- const newItem = { ...item };
|
|
|
- newItem.goods_num = shopMsg.num;
|
|
|
- newItem.goods_type = specString;
|
|
|
- this.carList.push(newItem);
|
|
|
- }
|
|
|
-
|
|
|
- // 您可以在这里执行其他逻辑,比如保存到本地存储或发送到服务器
|
|
|
- }
|
|
|
-
|
|
|
- console.log('购物车列表更新:', this.carList);
|
|
|
- this.totalPrice();
|
|
|
+ console.log('接收到的购物信息:', shopMsg);
|
|
|
+ const specString = shopMsg.spec + ',' + shopMsg.specs;
|
|
|
+ const item = this.goods.find(item => item.id === shopMsg.id);
|
|
|
+
|
|
|
+ if (item) {
|
|
|
+ // 检查购物车列表中是否已存在相同id和规格的商品
|
|
|
+ const existingCartItem = this.carList.find(cartItem => cartItem.id === item.id && cartItem
|
|
|
+ .goods_type === specString);
|
|
|
+
|
|
|
+ if (existingCartItem) {
|
|
|
+ // 如果已存在,则增加商品数量
|
|
|
+ existingCartItem.goods_num += shopMsg.num;
|
|
|
+ } else {
|
|
|
+ // 如果不存在,则复制item并添加到购物车列表,同时设置商品数量和规格
|
|
|
+ const newItem = {
|
|
|
+ ...item
|
|
|
+ };
|
|
|
+ newItem.goods_num = shopMsg.num;
|
|
|
+ newItem.goods_type = specString;
|
|
|
+ this.carList.push(newItem);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 您可以在这里执行其他逻辑,比如保存到本地存储或发送到服务器
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log('购物车列表更新:', this.carList);
|
|
|
+ this.totalPrice();
|
|
|
},
|
|
|
+ //数量的加减
|
|
|
numChanged(value, index) {
|
|
|
console.log('更新后的数量????????????:', value, '索引:', index);
|
|
|
// 假设 carList 是一个 Map,其中 id 是键
|
|
@@ -200,13 +219,29 @@
|
|
|
}
|
|
|
this.totalPrice()
|
|
|
},
|
|
|
+ //计算selected为true的
|
|
|
totalPrice() {
|
|
|
let total = 0;
|
|
|
for (let i = 0; i < this.carList.length; i++) {
|
|
|
- total += this.carList[i].goods_price * this.carList[i].goods_num;
|
|
|
- //goods_num += this.carList[i].goods_num
|
|
|
+ // 检查商品是否有 selected 属性
|
|
|
+ if (this.carList[i].hasOwnProperty('selected')) {
|
|
|
+ // 如果 selected 属性存在,只有当它为 true 时才计算
|
|
|
+ if (this.carList[i].selected) {
|
|
|
+ let priceInCents = Math.round(this.carList[i].goods_price * 100);
|
|
|
+ let quantity = this.carList[i].goods_num;
|
|
|
+ total += priceInCents * quantity;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果 selected 属性不存在,直接计算所有商品
|
|
|
+ let priceInCents = Math.round(this.carList[i].goods_price * 100);
|
|
|
+ let quantity = this.carList[i].goods_num;
|
|
|
+ total += priceInCents * quantity;
|
|
|
+ }
|
|
|
}
|
|
|
- this.total = total
|
|
|
+
|
|
|
+ // 将总价格转换回浮点数(以元为单位)
|
|
|
+ this.total = (total / 100).toFixed(2);
|
|
|
+
|
|
|
console.log('total', this.total);
|
|
|
},
|
|
|
showCart() {
|
|
@@ -221,6 +256,16 @@
|
|
|
uni.navigateTo({
|
|
|
url: `/pages/goodDetail/goodDetail`
|
|
|
})
|
|
|
+ },
|
|
|
+ toBuy() {
|
|
|
+ uni.navigateTo({
|
|
|
+ url: `/pages/order/submitOrder/submitOrder`
|
|
|
+ })
|
|
|
+ },
|
|
|
+ onMyInfo() {
|
|
|
+ uni.navigateTo({
|
|
|
+ url: `/pages/order/myOrder/myOrder`
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
}
|