// 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, select_allowance: false, allowance_data: null, allowance_price: 0, showAllowance: false, }, /** * 生命周期函数--监听页面加载 */ 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]; } console.log(this.data.showPrice) }, changeAllowance: function () { this.setData({ showAllowance: !this.data.showAllowance }) }, 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.allowance_data && this.data.select_allowance && this.data.allowance_price) { let spread_price = Number(this.data.allowance_data.discount_limit_price - this.data.allowance_data.user_used_price); price -= this.data.allowance_price * item.productNum < spread_price ? this.data.allowance_price * item.productNum : spread_price } }) 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 (this.data.select_allowance) data.is_allowance = 1 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; } if(!app.globalData.list){ 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({}); }) }else{ app.globalData.productPrice = (this.data.showPrice + this.data.postFee - (this.data.coupon?this.data.coupon.activity.money:0))/100; app.globalData.createDate = data console.log(app.globalData.createDate) wx.navigateTo({ url: '/pages/order/scene/index', }) } }, 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 }) }) }, /** * 选择补贴 */ selectAllowance: function () { this.setData({ select_allowance: !this.data.select_allowance }) this.changePrice() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.setData({ coupon: app.globalData.couponInfo }) // 获取补贴 if (this.data.product.length == 1) { commonApi.user_post("product/getProductAllowancePrice", { product_code: this.data.product[0].product.product_code }).then(resTwo => { console.log(resTwo) if (resTwo && resTwo.code == 1) { this.setData({ allowance_data: resTwo.data }) let spread_price = Number(this.data.allowance_data.discount_limit_price - this.data.allowance_data.user_used_price) let sInfo = this.data.product[0].sku console.log(9999, this.data.allowance_data.discount_rate / 100 * sInfo.price) console.log(9999, spread_price) if (spread_price > 0) { if (spread_price < this.data.allowance_data.discount_rate / 100 * sInfo.price) { this.setData({ allowance_price: spread_price }) } else { this.setData({ allowance_price: this.data.allowance_data.discount_rate / 100 * sInfo.price }) } this.changePrice() } else { this.setData({ allowance_price: 0 }) } } }) } }, /** * 生命周期函数--监听页面隐藏 */ 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 () { } })