| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- var _request = require('../../../../utils/request.js')
- var appConfig = require('../../../../utils/config.js')
- var PENDING_VIRTUAL_RECHARGE_ORDER_KEY = 'pendingVirtualRechargeOrderId'
- var RECHARGE_WXPAY = 'recharge_wxpay'
- function isCashVrPayEnabled () {
- return appConfig.cashVrPay === true
- }
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- price: '',
- cashTotal: 0,
- lock: false,
- syncingPendingRecharge: false,
- source:''
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- console.log('options',options);
- if(options && options['source']){
- this.setData({
- source:options['source']
- })
- }
- this.getCashTotal()
- },
- onShow: function () {
- if (isCashVrPayEnabled()) {
- this.syncPendingVirtualRecharge()
- } else {
- wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
- }
- },
- 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 price = parseFloat(that.data.price)
- var money = Math.round(price * 100)
- 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 (isCashVrPayEnabled() && money % 100 !== 0) {
- wx.showToast({
- title: '虚拟支付需输入整数元',
- icon: 'none',
- duration: 2000
- })
- } else {
- if (!that.data.lock) {
- that.setData({
- lock: true
- })
- if (isCashVrPayEnabled()) {
- that.createVirtualRecharge(money)
- } else {
- that.createWxRecharge(money)
- }
- }
- }
- },
- createWxRecharge (money) {
- var that = this
- var url = 'v1/recas_order/generate'
- var params = {
- count: money,
- payway: RECHARGE_WXPAY
- }
- var success = function (res) {
- if (res.data) {
- that.wxRechargeClick(res.data)
- } else {
- that.setData({
- lock: false
- })
- }
- }
- var fail = function () {
- that.setData({
- lock: false
- })
- }
- _request.$post(url, params, success, fail)
- },
- wxRechargeClick (order) {
- var that = this
- var url = 'v1/pay'
- var params = {
- order_id: order.order_id,
- pay_way: order.pay_way || RECHARGE_WXPAY
- }
- var success = function (res) {
- var payData = res.data && res.data.pay_data
- if (!payData) {
- that.setData({
- lock: false
- })
- wx.showToast({
- title: '支付参数异常',
- icon: 'none',
- duration: 2000
- })
- return
- }
- wx.requestPayment({
- timeStamp: payData.timeStamp,
- nonceStr: payData.nonceStr,
- package: payData.package,
- signType: payData.signType,
- paySign: payData.paySign,
- success: function () {
- getApp().globalData.rechargeChanged = true
- that.getCashTotal()
- wx.showToast({
- title: '充值成功',
- icon: 'success',
- duration: 1000
- })
- setTimeout(function () {
- that.backAfterRecharge()
- }, 1000)
- },
- fail: function (err) {
- var errMsg = err && err.errMsg ? err.errMsg : ''
- if (errMsg.indexOf('cancel') >= 0 || errMsg.indexOf('取消') >= 0) {
- wx.showToast({
- title: '用户取消支付',
- icon: 'none',
- duration: 2000
- })
- } else {
- wx.showToast({
- title: '支付失败',
- icon: 'none',
- duration: 2000
- })
- }
- that.setData({
- lock: false
- })
- }
- })
- }
- var fail = function () {
- that.setData({
- lock: false
- })
- }
- _request.$post(url, params, success, fail)
- },
- createVirtualRecharge (money) {
- var that = this
- var url = 'v1/virtual_pay/recharge'
- var params = {
- count: money
- }
- var success = function (res) {
- if (res.data) {
- if (res.data.order_id) {
- wx.setStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY, res.data.order_id)
- }
- that.rechargeClick(res.data)
- }
- }
- var fail = function () {
- that.setData({
- lock: false
- })
- }
- _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)
- var errMsg = err && (err.errMsg || err.err_code || err.errCode) ? (err.errMsg || err.err_code || err.errCode) : ''
- if (errMsg.indexOf('cancel') >= 0 || errMsg.indexOf('取消') >= 0) {
- wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
- that.setData({
- lock: false
- })
- wx.showToast({
- title: '用户取消支付',
- icon: 'none',
- duration: 2000
- })
- return
- }
- wx.showToast({
- title: '支付结果确认中',
- icon: 'none',
- duration: 1500
- })
- that.waitRechargePaid(payData.order_id, 0)
- }
- })
- },
- waitRechargePaid (oid, times) {
- var that = this
- var url = 'v1/virtual_pay/recharge/status'
- var success = function (res) {
- if (res.data && res.data.state === 1) {
- wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
- getApp().globalData.rechargeChanged = true
- 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)
- },
- syncPendingVirtualRecharge () {
- var that = this
- if (that.data.syncingPendingRecharge) {
- return
- }
- var pendingOrderId = wx.getStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
- var url = 'v1/virtual_pay/recharge/sync_pending'
- var success = function (res) {
- that.setData({
- syncingPendingRecharge: false
- })
- if (res.data && res.data.synced_count > 0) {
- wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
- getApp().globalData.rechargeChanged = true
- that.getCashTotal()
- wx.showToast({
- title: '充值成功',
- icon: 'success',
- duration: 1000
- })
- } else if (pendingOrderId) {
- that.waitRechargePaid(pendingOrderId, 0)
- }
- }
- var fail = function () {
- that.setData({
- syncingPendingRecharge: false
- })
- }
- that.setData({
- syncingPendingRecharge: true
- })
- _request.$get(url, {}, success, fail)
- },
- backAfterRecharge () {
- var that = this
- that.setData({
- lock: false
- })
- that.refreshPreviousPage()
- console.log('that.data.source', that.data.source)
- if (that.data.source) {
- wx.navigateBack()
- wx.navigateBack()
- } else {
- wx.navigateBack()
- }
- },
- refreshPreviousPage () {
- var pages = getCurrentPages()
- var delta = this.data.source ? 2 : 1
- var prevPage = pages[pages.length - 1 - delta]
- if (!prevPage) {
- return
- }
- if (prevPage.refreshBalancePage) {
- prevPage.refreshBalancePage()
- } else if (prevPage.refreshCashPage) {
- prevPage.refreshCashPage()
- } else if (prevPage.getCashTotal && prevPage.getCashList) {
- prevPage.getCashTotal()
- prevPage.getCashList()
- }
- },
- onShareAppMessage: function (val) {
- return _request.share({
- sc: 'xcx_user_recharge'
- })
- }
- })
|