const app = getApp(); const myOrder = require("../../utils/myOrder.js"); Page({ /** * 页面的初始数据 */ data: { nvabarData: { showCapsule: 1, white: true, address: '' }, product: {}, tempSuit: null, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.data.product = wx.getStorageSync("suitProduct"); console.log(this.data.product); wx.setNavigationBarTitle({ title: this.data.product.productName, }) console.log(this.data.product); this.setData({ product: this.data.product, color: app.globalData.color }); this.handleMake(); }, /** * 搭配套餐 */ cy_tapSuitDetail: function (e) { var position = e.currentTarget.dataset.position; var index = e.currentTarget.dataset.index; var suitList = this.data.product.suitList[position]; for (var item of suitList.list) { item.select = false; } var item = suitList.list[index]; item.select = true; var product = this.data.product; var salePrice = this.cy_getTotalPrice(); this.setData({ product: product, salePrice: salePrice }); }, handleMake: function () { var product = this.data.product; //查看必选做法 for (var i = 0; i < product.makeList.length; i++) { var item = product.makeList[i]; if (item.list.length >= 1) { for (var j = 0; j < item.list.length; j++) { var itemitem = item.list[j]; if (itemitem.isMustMake == 1) { item.list[0].checked = true; } } } } var suitList=product.suitList; if(suitList.length>=1){ for(var i=0;i= 1) { for (var i = 0; i < suitList.length; i++) { var suitItem = suitList[i]; if (suitItem.list.length >= 1) { for (var j = 0; j < suitItem.list.length; j++) { var suitItemList = suitItem.list[j]; var count=suitItemList.quantity; if(suitItemList.select){ addPrice+=suitItemList.addPrice; suitItem.selectName=suitItemList.product.productName; suitItem.selectSpecName=suitItemList.product.specName; suitItem.quantity=suitItemList.quantity; var product=suitItemList.product; for(var h=0;h 0) { salePrice = salePrice + selectMakePrice; } this.setData({addPrice:addPrice}) return parseFloat(parseFloat(salePrice).toFixed(2)); }, // 选择做法 cy_selectMake: function (e) { console.log(this.data.product); var index = e.currentTarget.dataset.index; var tempSuitList = this.data.product.suitList[index]; var tempSuit; for (var suit of tempSuitList.list) { if (suit.select) { tempSuit = suit; this.data.tempSuit = suit; break; } } if (tempSuit.product.makeList.length == 0) { wx.showToast({ title: tempSuit.product.productName + "~~没有做法", icon: "none" }) return; } //查看必选做法 for (var i = 0; i < tempSuit.product.makeList.length; i++) { var item = tempSuit.product.makeList[i]; if (item.list.length >= 1) { for (var j = 0; j < item.list.length; j++) { var itemitem = item.list[j]; if (itemitem.isMustMake == 1) { item.list[0].checked = true; } } } } var idsName = myOrder.cy_getIdsName(tempSuit.product); var salePrice = this.cy_getTotalPrice(); this.setData({ isMake: true, newSpecPrice: salePrice, shopName: tempSuit.product.productName, showNum: tempSuit.quantity, makeList: tempSuit.product.makeList, }) }, /** * 修改做法 */ cy_changeMake: function (e) { var x = e.currentTarget.dataset.x; var y = e.currentTarget.dataset.y; var tempSuit = this.data.tempSuit; var makeList = tempSuit.product.makeList[x]; var selectMake = makeList.list[y]; //当前做法多选选择做法的数量 var currentMultiMark=0; if(selectMake.isRadio !=1){ for (var i = 0; i < makeList.list.length; i++) { var make = makeList.list[i]; if(make.checked){ currentMultiMark++; } } } if (selectMake.isRadio == 1) { // 做法互斥 for (var i = 0; i < makeList.list.length; i++) { var makeTemp = makeList.list[i]; if (makeTemp.makeId == selectMake.makeId) { if(! makeTemp.checked){ makeTemp.checked = !makeTemp.checked; } } else { makeTemp.checked = false; } } } else { // 做法多选 for (var i = 0; i < makeList.list.length; i++) { var makeTemp = makeList.list[i]; if (makeTemp.makeId == selectMake.makeId) { if(currentMultiMark>1){ makeTemp.checked = !makeTemp.checked; }else{ if(! makeTemp.checked){ makeTemp.checked = !makeTemp.checked; } } } } } var salePrice = this.cy_getTotalPrice(); this.setData({ newSpecPrice: salePrice, makeList: tempSuit.product.makeList, }) }, /** * 计算合计金额 */ cy_getTotalPrice: function (e) { var salePrice = this.cy_sumSalePrice(); return parseFloat(salePrice.toFixed(2));; }, /** * 确认做法 */ cy_confirm: function (e) { console.log(e); var salePrice = this.cy_getTotalPrice(); this.cy_hide(); this.setData({ salePrice: salePrice, product: this.data.product, }) }, /** * 加入购物车 */ cy_addCart: function () { var product = this.data.product; var salePrice = this.cy_sumSalePrice(); var saleTotalPrice = this.cy_getTotalPrice(); var tempProduct = JSON.parse(JSON.stringify(product)); /**修改 套餐做法加价 */ var addPriceTotal = (saleTotalPrice - salePrice).toFixed(2); tempProduct.addPriceTotal = parseFloat(addPriceTotal); // 做法加价 //加价之前的原价 if(this.data.addPrice){ tempProduct.addPriceTotalConsumer=this.data.addPrice; tempProduct.addPriceOldPrice = parseFloat(parseFloat(product.price+this.data.addPrice).toFixed(2)); }else{ tempProduct.addPriceOldPrice = product.price; } tempProduct.specList[0].price = salePrice; tempProduct.specList[0].wprice = salePrice; tempProduct.count = 1; tempProduct.isSuit = 2; tempProduct.makeList = []; tempProduct.itemId = myOrder.getDaySeo(99); app.orderTicket.orderList.push(tempProduct); var totalAmount = 0; // 商品的总金额 for (var i = 0; i < product.suitList.length; i++) { var suitList = product.suitList[i]; for (var detail of suitList.list) { detail.isLast = false; if (i == product.suitList.length - 1) { detail.isLast = true; } if (detail.select) { var tempSpec = null; for (var spec of detail.product.specList) { if (detail.specId == spec.specId) { tempSpec = spec; } } totalAmount += (tempSpec.price + detail.addPrice) * detail.quantity; } } } totalAmount = parseFloat(totalAmount.toFixed(2)); // 保留两位小数 var discountTotal = parseFloat((totalAmount - (salePrice * tempProduct.count)).toFixed(2)); // 优惠的总金额 var sumMoney = 0; // 优惠券合计 使用减法计算最后一个商品的优惠 // 套餐包含数量 for (var suit of product.suitList) { for (var detail of suit.list) { if (detail.select) { var tempSpecList = []; // 商品的规格 var tempMakeList = []; // 记录商品选中的做法 var detailProduct = JSON.parse(JSON.stringify(detail.product)); var tempSpec = null; for (var spec of detailProduct.specList) { // 遍历商品规格 if (spec.specId == detailProduct.specId) { tempSpec = spec; tempSpecList.push(spec); } } var showName = ""; for (var makeMap of detailProduct.makeList) { // 遍历商品的做法 var list = makeMap.list; for (var make of list) { if (make.checked) { console.log(make); if (showName == "") { showName = showName + make.makeName; } else { showName = showName + "、" + make.makeName; } make.count = detail.quantity; tempMakeList.push(make); } } } detailProduct.showName = showName; detailProduct.specList = tempSpecList; detailProduct.makeList = tempMakeList; detailProduct.product_info = []; detailProduct.count = detail.quantity; detailProduct.isSuit = 3; var scale = (detail.addPrice + tempSpec.price) * detail.quantity / totalAmount; var discountMoney = (discountTotal * scale).toFixed(2); if (discountMoney > 0) { var product_info = {}; product_info.id = ""; product_info.orderItemId = ""; product_info.type = 50; product_info.info = "套餐优惠"; product_info.discountMoney = discountMoney; if (detail.isLast) { product_info.discountMoney = (discountTotal - sumMoney).toFixed(2); } detailProduct.product_info.push(product_info); // 套餐优惠分摊 } sumMoney += parseFloat((discountTotal * scale).toFixed(2)); detailProduct.parentItemId = tempProduct.itemId; console.log(detailProduct); app.orderTicket.orderList.push(detailProduct); } } } console.error(" app.orderTicket ", app.orderTicket); wx.navigateBack({ }) }, /** * 关闭做法 */ cy_hide: function () { this.setData({ isMake: false, }); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })