const app = getApp(); var util = require('../../utils/util.js'); var api = require('../../utils/service-api.js'); Page({ data: { address: {}, addrDefault: "", hasAddress: false, total: 0, couponsShow: false, //优惠券 delivery: [], key: 0, keydelivery: 0, isShow: true, selected: "", hidden: false, goodsList: [], skuList: [], usefulCoupon: [], coupons: '暂无可用优惠券', // 优惠券金额 couponReceiveId: null, // 优惠券Id postage: '', //邮费 showCoupon: false, //是否存在优惠券 carId: '', //购物车id buttonClicked: false, checkedValue: false, deliverySelect: "物流配送", qsfy: "", pageScen: "", discountExplain: {}, shopInfo:{}, }, onLoad(options) { var _this = this; this.setData({ shopInfo:app.shopInfo }); }, onShow: function () { var _this = this; _this.jsjg(); //获取收货地址 _this.getAddress(); }, //计算订单价格 jsjg() { var _this = this; var param = { deliverySelect: _this.data.deliverySelect, shopId:app.shopInfo.id, }; if (this.data.address) { param["receiveAddrId"] = this.data.address.addrId; } if (this.data.couponReceiveId) { param["couponReceiveId"] = this.data.couponReceiveId; } util.request({ method: "POST", api: api.order.calculationCartOrder, data: param, callback: function(data) { var goodsList = []; goodsList = goodsList.concat(data.rows); var skuList = []; for (var i = 0; i < goodsList.length; i++) { var sku = { "skuId": goodsList[i].cartSkuId, "count": goodsList[i].cartNumber } skuList.push(sku); } if (data.mapInfo.defaultAddress) { _this.setData({ "hasAddress": true, }) } if (data.mapInfo.shopCoupon.receiveId) { _this.setData({ "coupons": data.mapInfo.shopCoupon.cName, "couponReceiveId": data.mapInfo.shopCoupon.receiveId, "showCoupon": true }); } _this.setData({ "goodsList": goodsList, "skuList": skuList, "discountExplain": data.mapInfo.discountExplain, "address": data.mapInfo.defaultAddress, "usefulCoupon": data.mapInfo.usefulCoupon, "shopCoupon": data.mapInfo.shopCoupon, "deliverySelect": data.mapInfo.deliverySelect, }); } }); }, mySelect(e) { var name = e.currentTarget.dataset.name this.setData({ tihuoWay: name, select: false }) }, //获取textarea里的值 txtInput(e) { this.setData({ txtContent: e.detail.value }) }, onShowText(e) { this.setData({ isShow: true, }) }, onReady() { this.getTotalPrice(); }, /** * 计算总价 */ getTotalPrice() { }, bindTextAreaBlur(event) { var value = event.detail.value; this.setData({ txtContent: value, }); }, // 支付 startPayment() { // console.log("支付"); var _this = this; if (!_this.data.address) { wx.showToast({ title: "请选择收货地址", icon: "none" }); return; } wx.showLoading({ title: '加载中', mask: true, duration: 2000 }); util.buttonClicked(this); var goods = this.data.goodsList[0]; var couponReceiveId = _this.data.couponReceiveId; var checkedValue = _this.data.checkedValue; if (checkedValue) { couponReceiveId: '' } var sendData = { receiveAddrId: this.data.address.addrId, //收货地址ID couponReceiveId: couponReceiveId, //优惠券ID remarks: this.data.txtContent, skuId: goods.cartSkuId, productId: goods.cartProductId, number: goods.cartNumber, shopId: app.shopInfo.id, deliverySelect: this.data.deliverySelect, } console.log("订单请求参数", sendData) util.request({ api: "/wxapi/shopOrder/addShopOrder", data: sendData, callback: function(data) { var couponReceiveId = _this.data.couponReceiveId; wx.requestPayment({ 'timeStamp': data.rows[0].timeStamp, 'nonceStr': data.rows[0].nonceStr, 'package': data.rows[0].prepay_id, 'signType': 'MD5', 'paySign': data.rows[0].paySign, 'success': function(res) { // console.log(res.errMsg) console.log("成功后的回调") console.log(res) if (res.errMsg == 'requestPayment:ok') { // console.log(data.mapInfo.orderId); //成功后的回调; util.request({ method: "GET", api: '/wxapi/shopOrder/findOrderPayStatus/' + data.mapInfo.orderId, mask: true, callback: function(payRes) { if (payRes.mapInfo.status == 'success') { wx.redirectTo({ url: '../payOrder/paySuccess', }); wx.showToast({ title: payRes.mapInfo.msg, icon: 'success' }) } else { wx.showToast({ title: payRes.mapInfo.msg, duration: 2000, image: "/image/fail.png" }) wx.redirectTo({ url: '../payOrder/payError', }) } } }); } else { // console.log("支付失败"); wx.redirectTo({ url: '../order/order', }) } //{errMsg: "requestPayment:ok"} }, 'fail': function(res) { wx.redirectTo({ url: '../order/order?status=1', }) } }); } }); }, // 进入收货地址列表 goAddress(e) { wx.navigateTo({ url: '../editAddress/editAddress?pageScen=2', }) }, // 进入添加收货地址 addAddress() { wx.navigateTo({ url: '../editAddress/editAddress?pageScen=2', }) }, //设置地址 setAddress(address) { debugger this.setData({ "address": address, hasAddress: true }); }, //隐藏弹窗 popupTap(e) { this.setData({ couponsShow: false, isShow: false }) }, total(e) { this.setData({ isShow: true }) }, // 获取默认地址 getAddress() { var _this = this; util.request({ api: api.address.queryaddress, callback: function(data) { if (data.rows.length != 0) { _this.setData({ address: data.rows[0], hasAddress: true }); } else { _this.setData({ hasAddress: false }) } } }); }, // 不使用优惠券按钮 checkedTap: function () { var checked = this.data.checkedValue; var couponReceiveId = this.data.couponReceiveId; if (!checked) { couponReceiveId = 1 this.setData({ coupons: '不使用优惠券', couponReceiveId: couponReceiveId }) } this.setData({ checkedValue: !checked, couponsShow: false }) this.jsjg(); }, //优惠券 couponsChoice(e) { this.setData({ couponsShow: true, isShow: true }) }, //优惠券 radioCouponsChange(e) { var checked = e.detail.value; var changed = {} }, //优惠券选择 couponsEvent(e) { var _this = this; var coupon = e.currentTarget.dataset.coupon; var index = e.currentTarget.dataset.index; var id = e.currentTarget.dataset.id; var usefulCoupon = _this.data.usefulCoupon; var changed = {}; for (var i = 0; i < usefulCoupon.length; i++) { if (i == index) { changed['usefulCoupon[' + i + '].checked'] = true; } else { changed['usefulCoupon[' + i + '].checked'] = false } } _this.setData(changed) _this.setData({ coupons: coupon.cName, couponsShow: false, isShow: false, couponReceiveId: id, checkedValue: false }) this.jsjg(); }, })