// pages/order/order.js let Util = require("../../utils/util"); Page({ /** * 页面的初始数据 */ data: { orderid: null, userId: null, chargPileId: null, order: null, startTime: null, endTime: null, totalTime: null, orderFlag:false }, payOrder(e) { console.log('去支付'); console.log(this.order); let that = this; let { order, userId } = this.data; wx.login({ success(res) { console.log(res); if (res.code) { // 发起网络请求 wx.request({ url: getApp().globalData.postHeadAgreement +'://cdgl.xinyhy.cn/restapi/wechatpay/chargpayfor', data: { outTradeNo: order.orderid, //订单号 totalFee: order.chargallmoney, //钱 code: res.code, user_id: userId }, method: 'POST', header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8', }, success(res1) { let { data } = res1; console.log(res1); if (data && data.code == 200) { if (data.result) { //wx.showModal({ // showCancel: false, // content: '请微信支付' // }); wx.requestPayment({ timeStamp: data.result.timeStamp, nonceStr: data.result.nonceStr, package: data.result.package, signType: data.result.signType, paySign: data.result.paySign, success(res2) { wx.request({ url: getApp().globalData.postHeadAgreement +'://cdgl.xinyhy.cn/restapi/pileLog/wechatpayback', data: { //chargPileId, //userId, orderid: order.orderid }, method: 'POST', success(res3) { let { data } = res3; if (data && data.code == 200) { that.setData({ orderFlag:true }); wx.showModal({ showCancel: false, content: '支付订单成功' }); } else { wx.showModal({ showCancel: false, content: '支付订单失败' }); } } }); }, fail(res3) { console.log(res3); } }); } else { that.setData({ orderFlag: true }); wx.showModal({ showCancel: false, content: '支付订单成功' }); } } else { wx.showModal({ showCancel: false, content: '支付订单失败' }); } } }); } else { console.log('登录失败!' + res.errMsg) } } }); }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { let i = 0; let { orderid, userId, chargPileId } = options; this.setData({ orderid, userId, chargPileId }); var that = this; wx.showLoading({ title: '订单生成中...', mask:true }); var timeoutflag = setInterval(function() { i++; wx.request({ url: getApp().globalData.postHeadAgreement +'://cdgl.xinyhy.cn/restapi/pileLog/wechatsettlement', data: { chargPileId, userId, orderid }, method: 'POST', success(res) { let { data } = res; let { result: order } = data; if (data && data.code == 200) { if (order.transstatus==3){ wx.hideLoading(); clearInterval(timeoutflag); } // if (order.chargallmoney && order.chargpower) { // wx.hideLoading(); // clearInterval(timeoutflag); // } //wx.showModal({ //showCancel: false, //content: '获取订单成功' //}); var startDate = new Date(order.chargstarttime); var endDate = new Date(order.chargendtime); var totalTime = Math.floor((endDate.getTime() - startDate.getTime()) / 60000); var totalTimeHour = Math.floor(totalTime / 60); var totalTimeMinute = totalTime % 60; var startTime = Util.formatNumber(startDate.getMonth() + 1) + '月' + Util.formatNumber(startDate.getDate()) + '日 ' + Util.formatNumber(startDate.getHours()) + ':' + Util.formatNumber(startDate.getMinutes()) + ':' + Util.formatNumber(startDate.getSeconds()); var endTime = Util.formatNumber(endDate.getMonth() + 1) + '月' + Util.formatNumber(endDate.getDate()) + '日 ' + Util.formatNumber(endDate.getHours()) + ':' + Util.formatNumber(endDate.getMinutes()) + ':' + Util.formatNumber(endDate.getSeconds()); totalTime = Util.formatNumber(totalTimeHour) + "时" + Util.formatNumber(totalTimeMinute) + "分"; that.setData({ order, startTime, endTime, totalTime }); //console.log(that.data.result); } else { //wx.hideLoading(); if (i>=60){ clearInterval(timeoutflag); wx.showModal({ showCancel: false, content: '获取订单失败' }); } } }, fail(err) { wx.hideLoading(); clearInterval(timeoutflag); wx.showModal({ showCancel: false, content: '获取订单失败' }); } }); }, 1000); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { } })