var app = getApp(); const myOrder = require("../../utils/myOrder.js"); Page({ /** * 页面的初始数据 */ data: { // 导航头组件所需的参数 nvabarData: { showCapsule: 1, white: true, address: '' }, indicatorDots: true, autoplay: true, interval: 3000, circular: true, busMode: 0, cartFlag: true, isSubmit: true, promotionPrice: 0, promotionMoney: 0 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ color: app.globalData.color }) if (options.detail) { var detail = JSON.parse(options.detail); this.setData({ detail: detail, busMode: app.globalData.busMode, count: app.orderTicket.sumCount, money: app.orderTicket.receivable }) } this.cy_getProgramEvaluateDownload(); }, cy_getProgramEvaluateDownload: function () { var that = this; var params = { method: "program.evaluate.download", programId: app.globalData.programId, openId: app.openId, storeId: app.orderTicket.storeId, pageNumber: 1, pageSize: 15, }; var ignores = ["storeId", "pageNumber", "pageSize"]; // 不签名参数 app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function (json) { console.log("评价", json); var data = json.data; if (data.status == 1) { that.setData({ evaluateList: data.list, }); } else { wx.showToast({ title: data.message, icon: "none" }) } }, function (err) { }); }, /** * 选择规格 */ cy_selectProductSpec: function (e) { if (this.data.isBusiness == '0') { return; } var productMap = this.data.detail; this.data.selectProduct = productMap; if (productMap.suitFlag == 0) { // 单品选择规格 if (productMap.specFlag) { this.cy_showSpecAndMake(); } else { this.cy_addItem(); } } }, /** * 展示默认规格和做法 */ cy_showSpecAndMake: function () { var productMap = this.data.selectProduct; // var mustMakePrice=0; var makeStr=""; for (var i = 0; i < productMap.makeList.length; i++) { var makeListTemp = productMap.makeList[i]; var mustMakePriceItem=0; var mustMakeName=""; for (var j = 0; j < makeListTemp.list.length; j++) { var make = makeListTemp.list[j]; make.checked = false; if(make.isMustMake==1){ makeListTemp.list[0].checked = true; if(mustMakeName==""){ mustMakeName=makeListTemp.list[0].makeName } mustMakePriceItem=makeListTemp.list[0].addPrice; } } if(mustMakeName!=""){ if(makeStr==""){ makeStr=mustMakeName }else{ makeStr=makeStr+"、"+mustMakeName } } mustMakePrice=mustMakePrice+mustMakePriceItem; } productMap.ids = myOrder.cy_getIds(productMap); productMap.idsName = myOrder.cy_getIdsName(productMap); var productCount = myOrder.cy_getProductCount(productMap); var specPrice = 0; var promotionPrice = 0; var defaultSpesFlag = true; for (var specItem of productMap.specList) { if (specItem.stock > 0) { if (defaultSpesFlag) { specItem.checked = true; defaultSpesFlag = false; specPrice = specItem.price; if (specItem.promotionPrice) { promotionPrice = specItem.promotionPrice; } } else specItem.checked = false; // break; } } var memberPrice =parseFloat( parseFloat(productMap.specList[0].memberPrice+mustMakePrice).toFixed(2)); var wmemberPrice = parseFloat(parseFloat(productMap.specList[0].wmemberPrice+mustMakePrice).toFixed(2)); specPrice=parseFloat(parseFloat(specPrice+mustMakePrice).toFixed(2)); if(promotionPrice>0){ promotionPrice=parseFloat(parseFloat(promotionPrice+mustMakePrice).toFixed(2)); } if(makeStr!=""){ productMap.idsName=productMap.idsName+"、"+makeStr; } this.setData({ isSpec: productMap.specFlag, shopName: productMap.productName, // 商品名称 showSpec: productMap.idsName, // 规格做法名称 spec: productMap.specList.length > 1, // 是否是多规格 specPrice: specPrice, // 单价 promotionPrice: parseFloat(promotionPrice), specList: productMap.specList, // 规格列表 makeList: productMap.makeList, // 做法列表 productCount: productCount, // memberPrice: memberPrice, //会员价 wmemberPrice: wmemberPrice //外卖会员价 }) }, /** * 修改规格 */ cy_changeSpec: function (e) { var index = e.detail.currentTarget.dataset.index; var productMap = this.data.detail; var specList = this.data.detail.specList; var selectSpec = specList[index]; var specPrice = 0; var promotionPrice = 0; var ids = ""; var idsName = ""; if (selectSpec.stock <= 0) { wx.showToast({ title: '该规格已售罄,请选择其他规格', icon: "none" }) return; } // 遍历规格 for (var i = 0; i < specList.length; i++) { var spec = specList[i]; spec.checked = false; if (spec.specId == selectSpec.specId) { ids = ids + selectSpec.specId; idsName = idsName + selectSpec.specName; spec.checked = true; specPrice = spec.price; promotionPrice = spec.promotionPrice; } } // 遍历做法 for (var i = 0; i < productMap.makeList.length; i++) { var makeListTemp = productMap.makeList[i]; for (var j = 0; j < makeListTemp.list.length; j++) { var make = makeListTemp.list[j]; if (make.checked) { ids = ids + "|" + make.makeId; idsName = idsName + "、" + make.showName; specPrice += make.addPrice; promotionPrice += specPrice; } } } var productCount = myOrder.cy_getProductCount(this.data.selectProduct); productMap.specList = specList; this.setData({ specPrice: specPrice, detail: productMap, productCount: productCount, showSpec: idsName, promotionPrice: promotionPrice }) }, /** * 修改做法 */ cy_changeMake: function (e) { var x = e.detail.currentTarget.dataset.x; var y = e.detail.currentTarget.dataset.y; var productMap = this.data.selectProduct; var makeList = productMap.makeList; // 做法分类 var list = makeList[x]; // 做法明细 var selectMake = list.list[y]; var specPrice = 0; var memberPrice = 0; var wmemberPrice = 0; var sprcPromotionPrice = 0; var ids = ""; var idsName = ""; // 遍历规格 for (var spec of productMap.specList) { if (spec.checked) { specPrice = parseFloat(spec.price); sprcPromotionPrice = parseFloat(spec.promotionPrice); ids = spec.specId; idsName = spec.specName; break; } } var currentMultiMark=0; //当前做法多选选择做法的数量 if(selectMake.isRadio !=1){ for (var i = 0; i < list.list.length; i++) { var make = list.list[i]; if(make.checked){ currentMultiMark++; } } } if (selectMake.isRadio == 1) { // 做法互斥 for (var i = 0; i < list.list.length; i++) { var makeTemp = list.list[i]; if (makeTemp.makeId == selectMake.makeId) { if(! makeTemp.checked){ makeTemp.checked = !makeTemp.checked; } } else { makeTemp.checked = false; } } } else { // 做法多选 for (var i = 0; i < list.list.length; i++) { var make = list.list[i]; if (make.makeId == selectMake.makeId) { if(currentMultiMark>=1){ make.checked = !make.checked; }else{ if(! make.checked){ make.checked = !make.checked; } } } } } for (var i = 0; i < productMap.makeList.length; i++) { var tempMakeList = productMap.makeList[i]; for (var j = 0; j < tempMakeList.list.length; j++) { var makeTemp = tempMakeList.list[j]; if (makeTemp.checked) { console.log(makeTemp); idsName = idsName + "、" + makeTemp.showName; ids = ids + "|" + makeTemp.makeId; specPrice += makeTemp.addPrice; sprcPromotionPrice += makeTemp.addPrice; } } } var productCount = myOrder.cy_getProductCount(this.data.selectProduct); console.log("修改做法", productCount); specPrice = parseFloat(specPrice.toFixed(2)); var shopName = this.data.shopName this.setData({ specPrice: specPrice, promotionPrice: sprcPromotionPrice, makeList: makeList, productCount: productCount, showSpec: idsName, shopName: shopName }) for (var i = 0; i < app.orderTicket.orderList.length; i++) { var item = app.orderTicket.orderList[i]; if(item.productId == productMap.productId) { item.showName = idsName; app.orderTicket.orderList[i].makeList.push(selectMake); } } }, cy_changeSpecSellOut: function () { wx.showToast({ title: '该规格已售罄,请选择其他规格', icon: "none" }) return; }, cy_hide: function () { this.setData({ isSpec: false, cartFlag: true, isFlagImage: true }); }, /** * 多规格加入购物车 */ cy_addCart: function () { /*console.log("多规格商品加入购物车"); myOrder.cy_addItem(this.data.selectProduct); var productCount = myOrder.cy_getProductCount(this.data.selectProduct); this.setData({ productCount: productCount, shopName: this.data.shopName, specPrice: this.data.specPrice, showSpec: this.data.showSpec });*/ this.setData({ showSpec: this.data.showSpec }); console.error("-------------", app.orderTicket); this.cy_hide(); this.cy_reloadData(); }, /** * 多规格商品减 */ cy_subSpecItem: function () { myOrder.cy_subItem(this.data.detail); var productCount = myOrder.cy_getProductCount(this.data.detail); this.setData({ productCount: productCount, shopName: this.data.shopName, specPrice: this.data.specPrice, showSpec: this.data.showSpec }); myOrder.checkPromotionProduct(); this.cy_changeGuQing(this.data.detail, 'add'); this.cy_reloadData(); }, /** * 多规格商品 */ cy_addSpecItem: function () { if(this.data.detail.stock < 1 || this.data.productCount == this.data.detail.stock) { wx.showToast({ title: '菜品库存不足', icon: 'none', duration: 1500 }) }else { myOrder.cy_addItem(this.data.detail); var productCount = myOrder.cy_getProductCount(this.data.detail); this.setData({ productCount: productCount, shopName: this.data.shopName, specPrice: this.data.specPrice, showSpec: this.data.showSpec }) this.cy_changeGuQing(this.data.detail, 'reduce'); } this.cy_reloadData(); }, /** * 点击购物车 */ cy_cart: function () { console.log("购物车", app.orderTicket); if (app.orderTicket.orderList.length > 0) { this.setData({ cartFlag: false, list: app.orderTicket.orderList }) } }, /** * */ cy_clearCart: function () { var that = this; wx.showModal({ title: '提示', content: '清空购物车商品', success: function (res) { if (res.confirm) { that.setData({ cartFlag: true, }) app.orderTicket.orderList = []; that.cy_reloadData(); app.orderTicket.promotionFlag = 0; } } }) }, /** * */ cy_subCartItem: function (e) { var index = e.detail.dataset.index; var map = app.orderTicket.orderList[index]; map.count = map.count - 1; if (map.isSuit == 2 && map.count == 0) { // 套餐主菜 for (var suit of map.suitList) { for (var i = 0; i < app.orderTicket.orderList.length; i++) { var item = app.orderTicket.orderList[i]; if (item.isSuit == 3 && map.itemId == item.parentItemId) { app.orderTicket.orderList.remove(i); } } } } if (map.count == 0) { app.orderTicket.orderList.remove(index); } if (app.orderTicket.orderList.length == 0) { this.setData({ cartFlag: true, }) } myOrder.checkPromotionProduct(); this.cy_changeGuQing(map, 'add'); this.cy_reloadData(); }, /** * 购物车数量加一 */ cy_addCartItem: function (e) { console.error("eee", e) var index = e.detail.dataset.index; var index = e.detail.dataset.index; var map = app.orderTicket.orderList[index]; if(map.stock < 1 || map.count == map.stock) { wx.showToast({ title: '菜品库存不足', icon: 'none', duration: 1500 }) }else { map.count = map.count + 1; this.cy_changeGuQing(map, 'reduce'); } this.cy_reloadData(); }, /** * 刷新数据 */ cy_reloadData: function () { this.setData({ hidden: false, msg: "处理中..." }); var store = wx.getStorageSync("store"); var dateTime = app.utils.getFormatTime(new Date(), 5); var openTime = store.openTime; var isOpen = app.utils.isOpen(dateTime, openTime); var isOpen = false; var count = 0; // 数量 var amount = 0.0; // 消费金额 var discountTotal = 0.0; // 优惠金额 var receivable = 0.0; // 应收金额 var promotionReceivable = 0.0; //促销应收 var maling = 0.0; // 抹零金额 var paid = 0.0; // 实收金额 var boxFee = 0.0; // 餐盒费 var deliverFee = 0.0; // 配送费 if (app.orderTicket.orderList) { for (var product of app.orderTicket.orderList) { // 遍历订单列表计算商品金额 if (product.isSuit != 2) { // 套餐主菜不计算餐盒费 product.boxFee = product.boxPrice * product.count; // 单品餐盒费 boxFee = boxFee + product.boxFee // 总餐盒费 } if (product.isSuit == 3) { // 套餐明细不参与商品金额计算 /**修改套餐做法信息 */ // var sumAddMoney = 0; // for (var make of product.makeList) { // if (make.checked) { // sumAddMoney += make.count * make.addPrice; // } // } // receivable += sumAddMoney; continue; } var addReceivable = 0; // 加价应收金额 // 计算做法的价格 if (product.isSuit == 1) { if (product.makeList) { for (var make of product.makeList) { make.count = product.count; addReceivable += make.addPrice * make.count; } } } /**修改套餐的做法加价 */ if (product.isSuit == 2) { addReceivable = product.addPriceTotal; } var spec = product.specList[0]; // 商品规格 product.amount = parseFloat((spec.price * product.count + addReceivable).toFixed(2)); // 总金额 product.receivable = product.amount; // 应收金额 count += product.count; amount = amount + product.amount; var oldReceivable = receivable; receivable += product.receivable; //判断是否促销商品 是的话给订单一个标识 if (spec.discountRule) { app.orderTicket.promotionFlag = 1; product.promotionTotalPrice = parseFloat((spec.promotionPrice * product.count + addReceivable).toFixed(2)); // 促销总金额 if (promotionReceivable > 0) { promotionReceivable = parseFloat((parseFloat(product.promotionTotalPrice) + parseFloat(promotionReceivable)).toFixed(2)); } else { promotionReceivable = parseFloat((parseFloat(product.promotionTotalPrice) + parseFloat(oldReceivable)).toFixed(2)); } } else { if (promotionReceivable > 0) { promotionReceivable = parseFloat(parseFloat(promotionReceivable) + parseFloat(product.amount)).toFixed(2); } } } } paid = receivable; app.orderTicket.count = count; app.orderTicket.amount = parseFloat(amount).toFixed(2); app.orderTicket.discountTotal = parseFloat(discountTotal).toFixed(2); app.orderTicket.receivable = parseFloat(receivable).toFixed(2); app.orderTicket.maling = parseFloat(maling).toFixed(2); app.orderTicket.paid = parseFloat(paid).toFixed(2); app.orderTicket.boxFee = boxFee; app.orderTicket.promotionReceivable = parseFloat(promotionReceivable).toFixed(2); var productList = []; productList.push(this.data.detail); // 统计数量的合计 myOrder.cy_getProductList(productList); this.setData({ hidden: true, isOpen: isOpen, detail: this.data.detail, count: app.orderTicket.count, money: app.orderTicket.receivable, list: app.orderTicket.orderList, promotionMoney: app.orderTicket.promotionReceivable, }); console.log(app.orderTicket); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { if (app.orderTicket) { this.setData({ count: app.orderTicket.count }) } this.cy_reloadData(); }, /** * * 查询沽清数据 */ cy_changeGuQing: function(param, type) { var that = this; var params = { method: "saleclear.business.list", storeId: app.orderTicket.storeId, pageNumber: 1, pageSize: 500 }; var ignores = ["pageNumber", "pageSize"]; // 不签名参数 app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function (json) { var data = json.data; if (data.status == 1) { for(var i = 0; i < data.list.length; i++) { if(param.productId == data.list[i].productId) { data.list[i].typeId = type data.list[i].quantity = 1 //that.cy_updateGuQing(data.list[i]); } } } else { wx.showToast({ title: data.message, icon: "none" }) } }, function (err) { wx.showToast({ title: '网络连接失败,请检查', icon: "none" }) }) }, /** * * 修改沽清数据 */ cy_updateGuQing: function(data) { var params = { method: "saleclear.business.updatekc", storeId: app.orderTicket.storeId, jsonString: JSON.stringify(data) }; var ignores = ["jsonString"]; // 不签名参数 app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function (json) { var data = json.data; if (data.status == 1) { } else { wx.showToast({ title: data.message, icon: "none" }) } }, function (err) { wx.showToast({ title: '网络连接失败,请检查', icon: "none" }) }) }, /** * 单规格商品加一 */ cy_addItem: function (e) { if (this.data.isBusiness == '0') { return; } // var index = e.currentTarget.dataset.index; // var productMap = this.data.productList[index]; var productMap = this.data.detail; productMap.ids = myOrder.cy_getIds(productMap); productMap.idsName = myOrder.cy_getIdsName(productMap); this.data.selectProduct = productMap; if(productMap.stock < 1 || productMap.sumCount == productMap.stock) { wx.showToast({ title: '菜品库存不足', icon: 'none', duration: 1500 }) }else { myOrder.cy_addItem(productMap); this.cy_changeGuQing(productMap, 'reduce'); } this.cy_reloadData(); }, /** * 单规格商品减一 */ cy_subItem: function (e) { // var index = e.currentTarget.dataset.index; // var productMap = this.data.productList[index]; var productMap = this.data.detail; for (var i = 0; i < app.orderTicket.orderList.length; i++) { var map = app.orderTicket.orderList[i]; if (map.productId == productMap.productId) { map.count = map.count - 1; if (map.count == 0) { app.orderTicket.orderList.remove(i); } break; } } this.cy_reloadData(); }, /** * 确定下单 */ cy_placeOrder: function (e) { if (this.data.isSubmit) { this.data.isSubmit = false; var store = wx.getStorageSync("store"); var dateTime = app.utils.getFormatTime(new Date(), 5); var openTime = store.openTime; var isOpen = app.utils.isOpen(dateTime, openTime); var that = this; var int = setTimeout(function () { that.data.isSubmit = true; }, 1500); if (!isOpen) { wx.showToast({ title: '门店休息中,暂时无法下单', icon: "none" }) return; } if (app.orderTicket.orderList.length == 0) { wx.showToast({ title: '请您先点单', icon: "none" }) return; } that.setData({ cartFlag: true, }); var url = "../order/balance/submitOrder?storeBusModes=" + store.busModes; wx.redirectTo({ url: url, }) } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })