// pages/order/post/index.js let app = getApp() import commonApi from "../../../utils/https/common" Page({ /** * 页面的初始数据 */ data: { product:[], showPrice:0, from:null, address:null, postFee:0, coupon:null, sku_id:'', kjId:null, gp_id:null, prizeId:null }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ from:options.from, kjId:app.globalData.kjId, gp_id:app.globalData.gp_id || app.globalData.team_id, prizeId:wx.getStorageSync('prizeId') }) let price = 0,sku_id=[]; app.globalData.postProduct.map(item=>{ if(!app.globalData.kjId && !this.data.gp_id){ price = price + item.sku.price * item.productNum; } else if(this.data.gp_id){ price = price + item.sku.event_price * item.productNum; } sku_id.push(item.sku.id) }) this.setData({ product:app.globalData.postProduct, showPrice:app.globalData.kjId?0:price, sku_id:sku_id.join(",") }) if(!this.data.kjId && !this.data.gp_id){ this.couponCom = this.selectAllComponents("#coupon")[0]; } }, inputRemark:function(e){ let index = e.currentTarget.dataset.index,product = this.data.product; product[index].remark = e.detail.value; this.setData({ product:product }) }, setAddress:function(e){ this.setData({ address:e.detail }) this.getPostFee() }, minus:function(e){ let index = e.currentTarget.dataset.index,product = this.data.product; let productNum = product[index].productNum; if(productNum<=1) return; product[index].productNum--; this.setData({ product:product, coupon:null }) this.getPostFee() this.changePrice() }, add:function(e){ let index = e.currentTarget.dataset.index,product = this.data.product; product[index].productNum = product[index].productNum+1; this.setData({ product:product, coupon:null }) this.getPostFee() this.changePrice() }, changePrice:function(){ let product = this.data.product,price=0; product.map(item=>{ price = price + item.sku.price * item.productNum; }) if(this.data.coupon && this.data.showPrice!=price){ wx.showToast({ title: '订单价格发生变化,请重新选择优惠券', icon: 'none' }) app.globalData.couponInfo = null; if(!this.data.kjId && !this.data.gp_id){ this.couponCom.setNullCoupon() } this.setData({ coupon:null }) } this.setData({ showPrice:app.globalData.kjId?0:price }) }, order:function(){ if(!this.data.address){ wx.showToast({ title: '请选择收货地址!', icon:'none' }) return; } let product = this.data.product,product_list=[]; product.map(item=>{ let productItem = { type:item.product.type, product_id:item.product.id, sku_id:item.sku.id, post:this.data.address.id, product_num:item.productNum, remark:item.remark } product_list.push(productItem) }) let data = { source:"WECHATXCX", product_list:product_list, coupon:this.data.coupon?this.data.coupon.id:null, originate_order_id:this.data.kjId, gp_id:app.globalData.gp_id, team_id:app.globalData.team_id }; if(app.globalData.from){ data.system_name = app.globalData.from; } if(app.globalData.retailId){ data.commission_code = app.globalData.retailId; app.globalData.retailId = null; } if(this.data.prizeId){ data.lottery_id = this.data.prizeId } if(app.globalData.category_id){ data.category_id = app.globalData.category_id; } commonApi.user_post("order/create",{ data:JSON.stringify(data) }).then(res=>{ if(app.globalData.kjId){ app.globalData.kjId = null; this.setData({ kjId:null }) this.onLoad({}) } app.globalData.gp_id = null; app.globalData.team_id = null; this.setData({ gp_id:null }) this.onLoad({}); }) }, getPostFee:function(){ if(!this.data.address) return; let product = this.data.product,list=[],that = this; product.map(item=>{ list.push(commonApi.user_post("order/get_post_price",{ sku_id:item.sku.id, num:item.productNum, consignee_id:that.data.address.id })) }) // 获取邮费 Promise.all(list).then(res=>{ let postFee = 0,flag = true; res.map((item,index)=>{ if(item.data.price<0) flag = false; postFee = postFee+Number(item.data.price); product[index].postFee = item.data.price; }) if(!flag){ wx.showToast({ title: '该地区不发货', icon:'none' }) product.map(item=>{ item.postFee = 0; }) postFee = 0; } if(this.data.coupon && this.data.postFee!=postFee){ wx.showToast({ title: '订单价格发生变化,请重新选择优惠券', icon: 'none' }) app.globalData.couponInfo = null; if(!this.data.kjId && !this.data.gp_id){ this.couponCom.setNullCoupon() } this.setData({ coupon:null }) } this.setData({ postFee:postFee, product:product }) }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.setData({ coupon:app.globalData.couponInfo }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { this.setData({ kjId:null, gp_id:null }); app.globalData.kjId = null; app.globalData.gp_id = null; app.globalData.team_id = null; }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { } })