recharge.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. var _request = require('../../../../utils/request.js')
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. price: '',
  8. cashTotal: 0,
  9. lock: false,
  10. source:''
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad: function (options) {
  16. console.log('options',options);
  17. if(options && options['source']){
  18. this.setData({
  19. source:options['source']
  20. })
  21. }
  22. this.getCashTotal()
  23. },
  24. getCashTotal () {
  25. var that = this
  26. var url = 'v1/user/cash/balance/info'
  27. var params = {
  28. }
  29. var success = function (res) {
  30. that.setData({
  31. cashTotal: res.data
  32. })
  33. }
  34. _request.$get(url, params, success)
  35. },
  36. rechargePrice (e) {
  37. console.log(e)
  38. var that = this
  39. that.setData({
  40. price: e.detail.value
  41. })
  42. },
  43. createRecharge () {
  44. var that = this
  45. var url = 'v1/virtual_pay/recharge'
  46. var price = parseFloat(that.data.price)
  47. var money = Math.round(price * 100)
  48. var params = {
  49. count: money
  50. }
  51. var success = function (res) {
  52. if (res.data) {
  53. that.rechargeClick(res.data)
  54. }
  55. }
  56. var fail = function () {
  57. that.setData({
  58. lock: false
  59. })
  60. }
  61. if (!price || isNaN(price)) {
  62. wx.showToast({
  63. title: '充值金额不能为空',
  64. icon: 'none',
  65. duration: 2000
  66. })
  67. } else if (money < 100) {
  68. wx.showToast({
  69. title: '充值金额要大于1元',
  70. icon: 'none',
  71. duration: 2000
  72. })
  73. } else if (money % 100 !== 0) {
  74. wx.showToast({
  75. title: '虚拟支付需输入整数元',
  76. icon: 'none',
  77. duration: 2000
  78. })
  79. } else {
  80. if (!that.data.lock) {
  81. that.setData({
  82. lock: true
  83. })
  84. _request.$post(url, params, success, fail)
  85. }
  86. }
  87. },
  88. rechargeClick (payData) {
  89. var that = this
  90. console.log('requestVirtualPayment payData', payData)
  91. if (!wx.requestVirtualPayment && !wx.canIUse('requestVirtualPayment')) {
  92. that.setData({
  93. lock: false
  94. })
  95. wx.showToast({
  96. title: '当前微信版本不支持虚拟支付',
  97. icon: 'none',
  98. duration: 2000
  99. })
  100. return
  101. }
  102. wx.requestVirtualPayment({
  103. env: payData.env,
  104. mode: payData.mode,
  105. offerId: payData.offerId,
  106. signData: payData.signData,
  107. paySig: payData.paySig,
  108. signature: payData.signature,
  109. success: function (res) {
  110. console.log('requestVirtualPayment success', res)
  111. wx.showToast({
  112. title: '充值处理中',
  113. icon: 'success',
  114. duration: 1000
  115. })
  116. that.waitRechargePaid(payData.order_id, 0)
  117. },
  118. fail: function (err) {
  119. console.error('requestVirtualPayment fail', err)
  120. that.setData({
  121. lock: false
  122. })
  123. wx.showModal({
  124. confirmColor: '#eab86a',
  125. content: (err && (err.errMsg || err.err_code || err.errCode)) ? ('虚拟支付调起失败:' + (err.errMsg || err.err_code || err.errCode)) : '虚拟支付调起失败',
  126. showCancel: false
  127. })
  128. }
  129. })
  130. },
  131. waitRechargePaid (oid, times) {
  132. var that = this
  133. var url = 'v1/virtual_pay/recharge/status'
  134. var success = function (res) {
  135. if (res.data && res.data.state === 1) {
  136. getApp().globalData.rechargeChanged = true
  137. that.getCashTotal()
  138. wx.showToast({
  139. title: '充值成功',
  140. icon: 'success',
  141. duration: 1000
  142. })
  143. setTimeout(function () {
  144. that.backAfterRecharge()
  145. }, 1000)
  146. } else if (times < 5) {
  147. setTimeout(function () {
  148. that.waitRechargePaid(oid, times + 1)
  149. }, 1000)
  150. } else {
  151. that.setData({
  152. lock: false
  153. })
  154. wx.showToast({
  155. title: '支付已提交,到账稍后刷新',
  156. icon: 'none',
  157. duration: 2000
  158. })
  159. that.getCashTotal()
  160. }
  161. }
  162. var fail = function () {
  163. that.setData({
  164. lock: false
  165. })
  166. }
  167. _request.$get(url, { order_id: oid }, success, fail)
  168. },
  169. backAfterRecharge () {
  170. var that = this
  171. that.setData({
  172. lock: false
  173. })
  174. that.refreshPreviousPage()
  175. console.log('that.data.source', that.data.source)
  176. if (that.data.source) {
  177. wx.navigateBack()
  178. wx.navigateBack()
  179. } else {
  180. wx.navigateBack()
  181. }
  182. },
  183. refreshPreviousPage () {
  184. var pages = getCurrentPages()
  185. var delta = this.data.source ? 2 : 1
  186. var prevPage = pages[pages.length - 1 - delta]
  187. if (!prevPage) {
  188. return
  189. }
  190. if (prevPage.refreshBalancePage) {
  191. prevPage.refreshBalancePage()
  192. } else if (prevPage.refreshCashPage) {
  193. prevPage.refreshCashPage()
  194. } else if (prevPage.getCashTotal && prevPage.getCashList) {
  195. prevPage.getCashTotal()
  196. prevPage.getCashList()
  197. }
  198. },
  199. onShareAppMessage: function (val) {
  200. return _request.share({
  201. sc: 'xcx_user_recharge'
  202. })
  203. }
  204. })