var _request = require('../../../../utils/request.js') Page({ /** * 页面的初始数据 */ data: { price: '', cashTotal: 0, lock: false, source:'' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { console.log('options',options); if(options && options['source']){ this.setData({ source:options['source'] }) } this.getCashTotal() }, getCashTotal () { var that = this var url = 'v1/user/cash/balance/info' var params = { } var success = function (res) { that.setData({ cashTotal: res.data }) } _request.$get(url, params, success) }, rechargePrice (e) { console.log(e) var that = this that.setData({ price: e.detail.value }) }, createRecharge () { var that = this var url = 'v1/virtual_pay/recharge' var price = parseFloat(that.data.price) var money = Math.round(price * 100) var params = { count: money } var success = function (res) { if (res.data) { that.rechargeClick(res.data) } } var fail = function () { that.setData({ lock: false }) } if (!price || isNaN(price)) { wx.showToast({ title: '充值金额不能为空', icon: 'none', duration: 2000 }) } else if (money < 100) { wx.showToast({ title: '充值金额要大于1元', icon: 'none', duration: 2000 }) } else if (money % 100 !== 0) { wx.showToast({ title: '虚拟支付需输入整数元', icon: 'none', duration: 2000 }) } else { if (!that.data.lock) { that.setData({ lock: true }) _request.$post(url, params, success, fail) } } }, rechargeClick (payData) { var that = this console.log('requestVirtualPayment payData', payData) if (!wx.requestVirtualPayment && !wx.canIUse('requestVirtualPayment')) { that.setData({ lock: false }) wx.showToast({ title: '当前微信版本不支持虚拟支付', icon: 'none', duration: 2000 }) return } wx.requestVirtualPayment({ env: payData.env, mode: payData.mode, offerId: payData.offerId, signData: payData.signData, paySig: payData.paySig, signature: payData.signature, success: function (res) { console.log('requestVirtualPayment success', res) wx.showToast({ title: '充值处理中', icon: 'success', duration: 1000 }) that.waitRechargePaid(payData.order_id, 0) }, fail: function (err) { console.error('requestVirtualPayment fail', err) that.setData({ lock: false }) wx.showModal({ confirmColor: '#eab86a', content: (err && (err.errMsg || err.err_code || err.errCode)) ? ('虚拟支付调起失败:' + (err.errMsg || err.err_code || err.errCode)) : '虚拟支付调起失败', showCancel: false }) } }) }, waitRechargePaid (oid, times) { var that = this var url = 'v1/virtual_pay/recharge/status' var success = function (res) { if (res.data && res.data.state === 1) { that.getCashTotal() wx.showToast({ title: '充值成功', icon: 'success', duration: 1000 }) setTimeout(function () { that.backAfterRecharge() }, 1000) } else if (times < 5) { setTimeout(function () { that.waitRechargePaid(oid, times + 1) }, 1000) } else { that.setData({ lock: false }) wx.showToast({ title: '支付已提交,到账稍后刷新', icon: 'none', duration: 2000 }) that.getCashTotal() } } var fail = function () { that.setData({ lock: false }) } _request.$get(url, { order_id: oid }, success, fail) }, backAfterRecharge () { var that = this that.setData({ lock: false }) console.log('that.data.source', that.data.source) if (that.data.source) { wx.navigateBack() wx.navigateBack() } else { wx.navigateBack() } }, onShareAppMessage: function (val) { return _request.share({ sc: 'xcx_user_recharge' }) } })