| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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
- if (!wx.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 () {
- wx.showToast({
- title: '充值处理中',
- icon: 'success',
- duration: 1000
- })
- that.waitRechargePaid(payData.order_id, 0)
- },
- fail: function () {
- that.setData({
- lock: 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'
- })
- }
- })
|