// pages/goods/goods.js
Page({
data: {
goods: [
{
“name”: “热销榜”,
“type”: -1,
“foods”: [
{
“name”: “皮蛋瘦肉粥”,
“price”: 10,
“oldPrice”: “”,
“description”: “咸粥”,
“sellCount”: 229,
“Count”: 0,
“rating”: 100,
“info”: “一碗皮蛋瘦肉粥,总是我到粥店心满意足”,
“ratings”: [
{
“username”: “3******c”,
“rateTime”: 1469281964000,
“rateType”: 0,
“text”: “很喜欢的粥”,
“avatar”: “http://static.galileo.xiaojukeji.com/static/tms/default_header.png”
}
],
“icon”: “http://fuss10.elemecdn.com/c/cd/c12745ed8a5171e13b427dbc39401jpeg.jpeg?imageView2/1/w/114/h/114”,
“image”: “http://fuss10.elemecdn.com/c/cd/c12745ed8a5171e13b427dbc39401jpeg.jpeg?imageView2/1/w/750/h/750”
}
],
“icon”: “http://fuss10.elemecdn.com/f/28/a51e7b18751bcdf871648a23fd3b4jpeg.jpeg?imageView2/1/w/114/h/114”,
“image”: “http://fuss10.elemecdn.com/f/28/a51e7b18751bcdf871648a23fd3b4jpeg.jpeg?imageView2/1/w/750/h/750”
}
]
}
],
toView: ‘0’,
scrollTop: 100,
foodCounts: 0,
totalPrice: 0,// 总价格
totalCount: 0, // 总商品数
carArray: [],
minPrice: 20,//起送價格
payDesc: ”,
deliveryPrice: 4,//配送費
fold: true,
selectFoods: [{ price: 20, count: 2 }],
cartShow: ‘none’
},
// selectFoods: function (event) {
// var food = event.currentTarget.dataset.food;
// console.log(food);
// console.log(this.data.goods)
// let foods = [];
// this.data.goods.forEach((good) => {
// good.foods.forEach((food) => {
// // console.log(food)
// if (food.sellCount) {
// foods.push(food);
// console.log(foods);
// }
// })
// })
// },
selectMenu: function (e) {
var index = e.currentTarget.dataset.itemIndex;
this.setData({
toView: ‘order’ + index.toString()
})
console.log(this.data.toView);
},
//移除商品
decreaseCart: function (e) {
var index = e.currentTarget.dataset.itemIndex;
var parentIndex = e.currentTarget.dataset.parentindex;
this.data.goods.forEach((good) => {
good.foods.forEach((food) => {
var num = this.data.goods[parentIndex].foods[index].Count;
var mark = ‘a’ + index + ‘b’ + parentIndex
if (food.Count > 0) {
this.data.goods[parentIndex].foods[index].Count–
var price = this.data.goods[parentIndex].foods[index].price;
var obj = { price: price, num: num, mark: mark, name: name, index: index, parentIndex: parentIndex };
var carArray1 = this.data.carArray.filter(item => item.mark != mark);
carArray1.push(obj);
console.log(carArray1);
this.setData({
carArray: carArray1,
goods: this.data.goods
})
this.calTotalPrice()
this.setData({
payDesc: this.payDesc()
})
}
if (num > 0) {
var carArray1 = this.data.carArray.filter(item => item.num > 0)
console.log(carArray1)
this.setData({
carArray: carArray1,
})
}
})
})
},
decreaseShopCart: function (e) {
this.decreaseCart(e);
},
//添加到购物车
addCart(e) {
var index = e.currentTarget.dataset.itemIndex;
var parentIndex = e.currentTarget.dataset.parentindex;
this.data.goods[parentIndex].foods[index].Count++;
var mark = ‘a’ + index + ‘b’ + parentIndex
var price = this.data.goods[parentIndex].foods[index].price;
var num = this.data.goods[parentIndex].foods[index].Count;
var name = this.data.goods[parentIndex].foods[index].name;
var obj = { price: price, num: num, mark: mark, name: name, index: index, parentIndex: parentIndex };
var carArray1 = this.data.carArray.filter(item => item.mark != mark)
carArray1.push(obj)
console.log(carArray1);
this.setData({
carArray: carArray1,
goods: this.data.goods
})
this.calTotalPrice();
this.setData({
payDesc: this.payDesc()
})
},
addShopCart: function (e) {
this.addCart(e);
},
//计算总价
calTotalPrice: function () {
var carArray = this.data.carArray;
var totalPrice = 0;
var totalCount = 0;
for (var i = 0; i < carArray.length; i++) {
totalPrice += carArray[i].price * carArray[i].num;
totalCount += carArray[i].num
}
this.setData({
totalPrice: totalPrice,
totalCount: totalCount,
//payDesc: this.payDesc()
});
},
//差几元起送
payDesc() {
if (this.data.totalPrice === 0) {
return `¥${this.data.minPrice}元起送`;
} else if (this.data.totalPrice < this.data.minPrice) {
let diff = this.data.minPrice – this.data.totalPrice;
return ‘还差’ + diff + ‘元起送’;
} else {
return ‘去结算’;
}
},
//結算
pay() {
if (this.data.totalPrice < this.data.minPrice) {
return;
}
// window.alert(‘支付’ + this.totalPrice + ‘元’);
//确认支付逻辑
var resultType = “success”;
wx.redirectTo({
url: ‘../goods/pay/pay?resultType=’ + resultType
})
},
//彈起購物車
toggleList: function () {
if (!this.data.totalCount) {
return;
}
this.setData({
fold: !this.data.fold,
})
var fold = this.data.fold
//console.log(this.data.fold);
this.cartShow(fold)
},
cartShow: function (fold) {
console.log(fold);
if (fold == false) {
this.setData({
cartShow: ‘block’,
})
} else {
this.setData({
cartShow: ‘none’,
})
}
console.log(this.data.cartShow);
},
listShow() {
if (!this.data.totalCount) {
this.data.fold = true;
return false;
}
let show = !this.fold;
// if (show) {
// this.$nextTick(() => {
// if (!this.scroll) {
// this.scroll = new BScroll(this.$refs.listContent, {
// click: true
// });
// } else {
// this.scroll.refresh();
// }
// });
// }
return show;
},
onLoad: function (options) {
// 页面初始化 options为页面跳转所带来的参数
this.setData({
payDesc: this.payDesc()
});
},
onReady: function () {
// 页面渲染完成
},
onShow: function () {
// 页面显示
},
onHide: function () {
// 页面隐藏
},
onUnload: function () {
// 页面关闭
}
})今天的文章微信小程序购物车代码页面_如何获取小程序码「建议收藏」分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/58903.html