// pages/order/pay/index.js import commonApi from "../../../utils/https/common" import util from "../../../utils/util" let timer; Page({ /** * 页面的初始数据 */ data: { id:null, info:null, second:"00", minute:"00", payIndex:-1 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ id:options.id }) }, daojishi:function(time){ let now = new Date().getTime(); if(now>time){ clearTimeout(timer); timer = null; util.back(); return; } let times = time - now,minute = Math.floor(times / (60 * 1000)),second = Math.round((times - minute * 60 * 1000) / 1000); this.setData({ minute:util.formatNumber(minute), second:util.formatNumber(second) }) timer = setTimeout(()=>{ this.daojishi(time); },1000) }, // 选择支付方式 changePayMethod:function(e){ let index = e.currentTarget.dataset.index; this.setData({ payIndex:index }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { // commonApi.user_post("subwallet.ermb/wallet",{}).then(res=>{ // this.setData({ // payList:res.data // }) // }) commonApi.user_post("order/query",{ order_id:this.data.id }).then(res=>{ if(res.data.create_time && res.data.auto_close_time && res.data.state=='UNPAID'){ let time = (new Date(res.data.create_time.replace(/-/g,'/')).getTime() + Number(res.data.auto_close_time) * 1000); this.daojishi(time); } else { util.back() } this.setData({ info:res.data }) }) this.checkPay(); }, checkPay:function(){ commonApi.user_post("order/query_pay_result",{ order_id:this.data.id }).then(res=>{ if(res.data.code==1){ wx.showToast({ title: '订单已支付', icon:'success' }) setTimeout(()=>{ wx.reLaunch({ url: '/pages/user/order/list', }) },1000) return; } }) }, pay:function(){ let _this = this; commonApi.user_post("order/pay",{ order_id:this.data.id, pay_method:"WEIXIN", pay_platform:"MINI", app_name:"WxXcxConfig" }).then(res=>{ res.data = JSON.parse(res.data) wx.requestPayment({ nonceStr: res.data.nonceStr, package: res.data.package, paySign: res.data.paySign, timeStamp:""+res.data.timeStamp, signType:res.data.signType, success:function(res){ wx.showToast({ title: '支付成功', icon:'success' }) setTimeout(()=>{ wx.redirectTo({ url: '../payresult/index?tid='+_this.data.id }) },1000) }, fail:function(err){ console.log(err) } }) console.log(res) }) }, // 电子钱包支付 walletPay:function(){ let item = this.data.payList[this.data.payIndex]; commonApi.user_post("order/pay",{ order_id:this.data.id, pay_method:"SUBWALLET", pay_platform:"MINI", app_name:"WxXcxConfig", ins_no:item.ins_no }).then(res=>{ let data = res.data?JSON.parse(res.data):{}; if(data && data.RESULT && data.RESULT=='Y'){ commonApi.user_post("order/query_pay_result",{ order_id:this.data.id }).then(res=>{ if(res.data.code==1){ wx.showToast({ title: '支付成功', icon:"success" }) setTimeout(()=>{ wx.redirectTo({ url: '../payresult/index?tid='+this.data.id }) },1000) } }) } else { wx.showToast({ title: data.ERRORMSG || "支付失败,请重试", icon:"none" }) } }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { clearTimeout(timer) timer = null; }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { } })