const app = getApp() const api = require('../../../../../request/api'); const http = require('../../../../../request/http'); const utils = require('../../../../../utils/util.js'); const storeKeys = require('../../../../../utils/storageKeys.js'); Page({ data: { isIphoneX: app.globalData.isIphoneX, isLoading: true, //是否处于加载 mode: 'buyNow', //页面来源 dataParams: {}, //参数对象 model_addr: false, goodsObj: {}, //商品返回数据 addressId: '', //地址id isDisabled: false, //是否禁用按钮 remark: '', //备注 orderId: '', buyNumber: 1, //购买数量 buySelfNumber: 1, //自提购买数量 cardModel: false, riceList: [], riceCardId: 0, //确认选中的礼品卡 riceCardCheckItem: {}, //选择使用的礼品卡 isCommit: false, orderType: 10, //配送方式 默认10 10-快递配送 20-门店自提 join_id: 0, //拼团id formeType: 1, //1:拼团 2:裂变拼团 couponModel: false, //是否显示优惠卷弹窗 couponList: [], //优惠卷列表 couponId: 0, //确认选中的优惠卷id couponCheckItem: {}, //确认选中的优惠卷 couponName: '', //确认选中的优惠卷名字 discountModel: false, //是否显示优惠明细弹窗 kimmyModel: false, //金米粒抵扣弹窗 isUseGoldRice: -1, //是否使用金米粒 -1禁用 0不使用 1使用 }, onLoad: function (options) { if (options.data) { let dataObj = JSON.parse(options.data) // let dataObj = { // activityId: 61, // addressId: 0, // delivery: 10, // goodsId: 10075, // goodsNum: 1, // goodsSkuId: 0, // mode: "groupbuy" // } if(dataObj.mode==='groupbuylb'){ this.data.formeType = 2 } console.log(dataObj) this.setData({ dataParams: dataObj, mode: dataObj.mode, buyNumber: dataObj.goodsNum, buySelfNumber: dataObj.goodsNum, }) } }, //切换配送方式 changeTitleTab(e) { let index = e.currentTarget.dataset.index; if (index == this.data.orderType) { return; } this.setData({ orderType: index, ['dataParams.goodsNum']: index == 10 ? this.data.buyNumber : this.data.buySelfNumber }) this.getData(); }, //增加/删减购买数量 线上物流 changeGoodsNumber: utils.throttle(function (e) { let state = e.currentTarget.dataset.state; let item = e.currentTarget.dataset.item; let buyNumber = this.data.buyNumber; // console.log(item) // state:1增加 0删减 if (state == 1) { if (item.skuInfo.stock_num <= 0) { utils.toast("库存不足") return; } if (buyNumber >= item.skuInfo.stock_num) { utils.toast("不能再加了,达到商品最大库存了") return; } if (buyNumber >= item.limit_mount) { let limit_mount = item.limit_mount utils.toast("每人限购"+limit_mount+"件哦~") return; } buyNumber++ } else { if (buyNumber <= 1) { utils.toast("不能再减了") return; } buyNumber-- } this.setData({ buyNumber, ['dataParams.goodsNum']: buyNumber }) this.getData() }, 500), //增加/删减购买数量 门店自提 changeGoodsSelfNumber: utils.throttle(function (e) { let state = e.currentTarget.dataset.state; let item = e.currentTarget.dataset.item; let buySelfNumber = this.data.buySelfNumber; if (state == 1) { if (item.skuInfo.stock_num <= 0) { utils.toast("库存不足") return; } if (buySelfNumber >= item.skuInfo.stock_num) { utils.toast("不能再加了,达到商品最大库存了") return; } if (buySelfNumber >= item.limit_mount) { utils.toast("不能再加了,达到商品最大购买量了") return; } buySelfNumber++ } else { if (buySelfNumber <= 1) { utils.toast("不能再减了") return; } buySelfNumber-- } this.setData({ buySelfNumber, ['dataParams.goodsNum']: buySelfNumber }) this.getData() }, 500), //监听商品数量输入 线上物流 changeBuyNumber(e) { let buyNumber = e.detail.value.replace(/\s+/g, ''); this.setData({ buyNumber, ['dataParams.goodsNum']: buyNumber }) }, //监听商品数量输入 门店自提 changeBuySelfNumber(e) { let buySelfNumber = e.detail.value.replace(/\s+/g, ''); this.setData({ buySelfNumber, ['dataParams.goodsNum']: buySelfNumber }) }, //商品数量输入失去焦点 线上物流 numberBlur(e) { //输入的数量大于库存数时 let item = e.currentTarget.dataset.item, that = this; if (parseInt(this.data.buyNumber) > parseInt(item.skuInfo.stock_num) || parseInt(this.data.buyNumber) > parseInt(item.limit_mount)) { let minNum = Math.min(parseInt(item.skuInfo.stock_num), parseInt(item.limit_mount)) this.setData({ buyNumber: minNum, ['dataParams.goodsNum']: minNum }) if (item.skuInfo.stock_num == 0) { utils.toast("库存不足") return } } setTimeout(() => { that.getData() }, 200) }, //商品数量输入失去焦点 门店自提 selfnumberBlur(e) { //输入的数量大于库存数时 let item = e.currentTarget.dataset.item, that = this; if (parseInt(this.data.buySelfNumber) > parseInt(item.skuInfo.stock_num) || parseInt(this.data.buySelfNumber) > parseInt(item.limit_mount)) { let minNum = Math.min(parseInt(item.skuInfo.stock_num), parseInt(item.limit_mount)) this.setData({ buySelfNumber: minNum, ['dataParams.goodsNum']: minNum }) } setTimeout(() => { that.getData() }, 200) }, toAddr() { wx.navigateTo({ url: "/pages/user/pages/addressList/addressList?sourceState=1" }) }, detailInput: function (e) { this.setData({ remark: e.detail.value }) }, //获取订单列表 getData() { let that = this; wx.showLoading({ title: "加载中", mask: true }); let url = '', dataParams = this.data.dataParams if (!dataParams.goodsSkuId) { dataParams.goodsSkuId = 0 } //本地分享人id 107446 let staffUserId = dataParams.joinId || utils.getStorageSync(storeKeys.SHAREID) || utils.getStorageSync(storeKeys.APPLESID) || ""; // if(this.data.isUseGoldRice == -1){ // this.data.isUseGoldRice = 1; // } // 推广监控ID let promotionMonitorId = utils.getStorageSync(storeKeys.SOURECID) || 0; url = '/api/checkout/order?mode=' + dataParams.mode + '&goodsId=' + dataParams.goodsId + '&goodsSkuId=' + dataParams.goodsSkuId + '&goodsNum=' + dataParams.goodsNum + '&addressId=' + this.data.addressId + '&riceCardId=' + this.data.riceCardId + '&delivery=' + this.data.orderType +"&promotionMonitorId="+promotionMonitorId if (dataParams.joinId) { url += '&joinId=' + staffUserId } else { url += '&activityId=' + dataParams.activityId } console.log(url); http.request({ url: api.URL + url, method: 'GET', token: utils.getStorageSync(storeKeys.TOKEN), success: (res) => { console.log(res) wx.hideLoading(); let orderObj = res.data.data.order; if (this.data.orderType == 20 && !orderObj.isPickup) { this.setData({ orderType: 10, ['goodsObj.isPickup']: false }) return; } //来源直接购买 切换时库存大于输入库存时 重新赋值输入的值 if (this.data.mode == "buyNow") { let goodsList = orderObj.goodsList || [] let buyNumber = this.data.buyNumber; let buySelfNumber = this.data.buySelfNumber; for (let i = 0; i < goodsList.length; i++) { let item = goodsList[i]; if (!item.stock_enough) { // 商品类型,10普通商品;20赠品;30套餐商品 if (item.goods_type != 20) { if (this.data.orderType == 20) { this.setData({ buySelfNumber: item.skuInfo.stock_num || buySelfNumber }) } else { this.setData({ buyNumber: item.skuInfo.stock_num || buyNumber }) } this.setData({ ['dataParams.goodsNum']: item.skuInfo.stock_num }) } break; } } } //选中的优惠价id let couponId = res.data.data.order.couponId; let couponCheckItem = { couponId }; //选中的礼品卡id let riceCardId = res.data.data.order.riceCardId; let riceCardCheckItem = { id: riceCardId } this.setData({ goodsObj: res.data.data.order, // couponName:res.data.data.order.couponName, // couponId, riceCardId, couponCheckItem, riceCardCheckItem, isLoading: false, riceList: res.data.data.order.userRiceCardList, // couponList: res.data.data.order.couponList, couponPrice: utils.toDecimal2(parseFloat(res.data.data.order.orderTotalPrice) - parseFloat(res.data.data.order.orderTotalGroupPrice)) }) // if(!orderObj.address || orderObj.errorStatus==423 || orderObj.hasError || Number(orderObj.orderPayPrice)<=0 ){ // (!orderObj.address && this.data.orderType == 10) || if (orderObj.errorStatus == 423 || orderObj.hasError) { this.setData({ isDisabled: true }) } else { this.setData({ isDisabled: false }) } // if (orderObj.errorStatus == 422) { // this.setData({ // isDisabled: false // }) // utils.toast(orderObj.errorMsg, 2000) // } if (orderObj.errorStatus == 423) { this.setData({ model_addr: true }) } else { this.setData({ model_addr: false }) } }, error: (res) => { // console.log("errr"); // this.setData({ // buySelfNumber: item.skuInfo.stock_num || buySelfNumber, // buyNumber: item.skuInfo.stock_num || buyNumber // }) // if (this.data.goodsObj) { // let goodsList = this.data.goodsObj.goodsList || [] // let buyNumber = this.data.buyNumber; // let buySelfNumber = this.data.buySelfNumber; // for (let i = 0; i < goodsList.length; i++) { // let item = goodsList[i]; // if (!item.stock_enough) { // // 商品类型,10普通商品;20赠品;30套餐商品 // if(item.goods_type != 20){ // if(this.data.orderType == 20){ // this.setData({ // buySelfNumber: item.skuInfo.stock_num || buySelfNumber // }) // }else{ // this.setData({ // buyNumber: item.skuInfo.stock_num || buyNumber // }) // } // this.setData({ // ['dataParams.goodsNum']: item.skuInfo.stock_num // }) // } // break; // } // } // } }, }) }, // 清除无货商品 cleanInvalidGoods: utils.throttle(function (e) { let invalidGoodsList = this.data.goodsObj.invalidGoodsList || []; console.log(invalidGoodsList) let cartIds = []; invalidGoodsList && invalidGoodsList.forEach((item) => { cartIds.push(item.cart_id); }) console.log(cartIds) let data = { cartIds } wx.showLoading({ title: "加载中" }); http.request({ url: api.URL + '/api/cart/delete', method: 'POST', data, token: utils.getStorageSync(storeKeys.TOKEN), success: (res) => { this.getData(); }, error: (res) => { wx.hideLoading(); utils.toast(res.data.message) }, }) }), clickModel() { this.setData({ model_addr: false }) }, commit: utils.throttle(function (e) { if (this.data.orderType == 20 && this.data.goodsObj.invalidGoodsList && this.data.goodsObj.invalidGoodsList.length > 0) { utils.toast("请先清除无货商品!") return; } let that = this; if (this.data.orderType == 10 && !this.data.goodsObj.address) { utils.toast("请先填写收货地址") return; } // if (this.data.goodsObj.errorStatus == 422) { // utils.toast(this.data.goodsObj.errorMsg, 2000) // return // } wx.showLoading({ title: "加载中" }); let dataParams = this.data.dataParams //本地分享人id let staffUserId = dataParams.joinId || ""; dataParams.addressId = this.data.addressId dataParams.remark = this.data.remark dataParams.riceCardId = this.data.riceCardId // dataParams.couponId = this.data.couponId dataParams.delivery = this.data.orderType // dataParams.isUseGoldRice=this.data.isUseGoldRice dataParams.staffUserId = utils.getStorageSync(storeKeys.SHAREID) if (!dataParams.goodsSkuId) { dataParams.goodsSkuId = 0 } if (dataParams.joinId) { dataParams.joinId = staffUserId } console.log(dataParams) console.log("request params:" + JSON.stringify(dataParams)); // if(this.data.mode=='cart'){ // dataParams.cartIds = dataParams.goodsId // delete dataParams.goodsId // } // 推广监控ID let promotionMonitorId = utils.getStorageSync(storeKeys.SOURECID) console.log("下单监控id"+promotionMonitorId) if(promotionMonitorId){ dataParams.promotionMonitorId = promotionMonitorId } console.log(dataParams) http.request({ url: api.URL + '/api/checkout/order', method: 'POST', token: utils.getStorageSync(storeKeys.TOKEN), data: dataParams, success: (res) => { wx.hideLoading(); let orderId = res.data.data.order_id that.data.join_id = res.data.data.extra?res.data.data.extra.join_id:0 if (res.data.data.pay_type == 20) { that.setData({ isCommit: true }) that.wxPayment(res.data.data.payment, orderId); } if (res.data.data.pay_type == 30) { utils.toast('支付成功'); let formeType = that.data.formeType console.log(formeType) // app.getCartNumber(); wx.reLaunch({ url: "/pages/cart/pages/paySuccess/paySuccess?orderType=" + that.data.orderType + "&type="+formeType + "&joinId="+ that.data.join_id }) } }, error: (res) => { console.log(res) that.setData({ isDisabled: true }) }, }) }, 800), //调起微信支付 wxPayment(data, orderId) { let _this = this; let dataParams = this.data.dataParams //本地分享人id let staffUserId = dataParams.joinId || ""; wx.requestPayment({ nonceStr: data.nonceStr, package: 'prepay_id=' + data.prepay_id, paySign: data.paySign, signType: "MD5", timeStamp: data.timeStamp, success(res) { if (res.errMsg == "requestPayment:ok") { // app.getCartNumber(); utils.toast('支付成功'); let formeType = _this.data.formeType console.log(formeType) wx.reLaunch({ url: "/pages/cart/pages/paySuccess/paySuccess?orderType=" + _this.data.orderType+ "&type="+formeType +"&joinId="+ _this.data.join_id }) } }, fail(res) { if (res.errMsg == "requestPayment:fail cancel") { // app.getCartNumber(); utils.toast('已取消支付'); // wx.reLaunch({ // url: "/pages/cart/pages/noPayment/noPayment?orderId=" + orderId // }) http.request({ url: api.URL + '/api/groupbuy.Activity/cancel', method: 'POST', token: utils.getStorageSync(storeKeys.TOKEN), data: { "order_id": orderId }, success: (res) => {}, error: (res) => {} }) } } }) }, // 选择礼品卡 toCheckRiceCard() { this.setData({ cardModel: true }) }, hideModel() { this.setData({ cardModel: false }) }, checkRiceCard(e) { let item0 = { id: -1 } let item = e.currentTarget.dataset.item || item0; console.log(item) this.setData({ riceCardCheckItem: item }) }, confirmRiceCard() { console.log(this.data.riceCardCheckItem.id) if (!this.data.riceCardCheckItem.id) { utils.toast("请选择要使用的礼品卡") return; } this.setData({ cardModel: false, riceCardId: this.data.riceCardCheckItem.id }) this.getData() }, //选择优惠卷 checkCoupon(e) { let couponId = e.currentTarget.dataset.id || -1; let couponCheckItem = { couponId } this.setData({ couponCheckItem }) }, //确认优惠卷 confirmCoupon() { if (!this.data.couponCheckItem.couponId) { utils.toast("请选择要使用的优惠券") return; } this.setData({ couponModel: false, couponId: this.data.couponCheckItem.couponId }) this.getData() }, onShow: function () { if (this.data.isCommit) { return; } this.getData() }, //弹出选择优惠卷 couponModel() { if (!this.data.couponList || this.data.couponList.length <= 0) { return; } this.setData({ couponModel: true }) }, //隐藏选择优惠卷 hideCouponModel() { this.setData({ couponModel: false }) }, //取消下单 cancel: utils.throttle(function (e) { wx.navigateBack({ delta: 1 }) }), //显示优惠详情 showDiscountModel() { this.setData({ discountModel: true }) }, //隐藏优惠详情 hideDiscountModel() { this.setData({ discountModel: false }) }, //显示选择金米粒 showKimmyModel() { this.setData({ kimmyModel: true, isUseGoldRice: this.data.goodsObj.isUseGoldRice }) }, //隐藏选择金米粒 hideKimmyModel() { this.setData({ kimmyModel: false }) }, //切换选择金米粒 checkKimmy(e) { //禁用金米粒 if (this.data.goodsObj.isUseGoldRice == -1) { return; } let isUseGoldRice = e.currentTarget.dataset.id || 0; this.setData({ isUseGoldRice }) }, //确认金米粒 confirmKimmy() { this.setData({ kimmyModel: false }) //禁用金米粒 if (this.data.goodsObj.isUseGoldRice == -1) { return; } console.log(this.data.isUseGoldRice) this.getData() }, })