recharge.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if (!wx.requestVirtualPayment) {
  91. that.setData({
  92. lock: false
  93. })
  94. wx.showToast({
  95. title: '当前微信版本不支持虚拟支付',
  96. icon: 'none',
  97. duration: 2000
  98. })
  99. return
  100. }
  101. wx.requestVirtualPayment({
  102. env: payData.env,
  103. mode: payData.mode,
  104. offerId: payData.offerId,
  105. signData: payData.signData,
  106. paySig: payData.paySig,
  107. signature: payData.signature,
  108. success: function () {
  109. wx.showToast({
  110. title: '充值处理中',
  111. icon: 'success',
  112. duration: 1000
  113. })
  114. that.waitRechargePaid(payData.order_id, 0)
  115. },
  116. fail: function () {
  117. that.setData({
  118. lock: false
  119. })
  120. }
  121. })
  122. },
  123. waitRechargePaid (oid, times) {
  124. var that = this
  125. var url = 'v1/virtual_pay/recharge/status'
  126. var success = function (res) {
  127. if (res.data && res.data.state === 1) {
  128. that.getCashTotal()
  129. wx.showToast({
  130. title: '充值成功',
  131. icon: 'success',
  132. duration: 1000
  133. })
  134. setTimeout(function () {
  135. that.backAfterRecharge()
  136. }, 1000)
  137. } else if (times < 5) {
  138. setTimeout(function () {
  139. that.waitRechargePaid(oid, times + 1)
  140. }, 1000)
  141. } else {
  142. that.setData({
  143. lock: false
  144. })
  145. wx.showToast({
  146. title: '支付已提交,到账稍后刷新',
  147. icon: 'none',
  148. duration: 2000
  149. })
  150. that.getCashTotal()
  151. }
  152. }
  153. var fail = function () {
  154. that.setData({
  155. lock: false
  156. })
  157. }
  158. _request.$get(url, { order_id: oid }, success, fail)
  159. },
  160. backAfterRecharge () {
  161. var that = this
  162. that.setData({
  163. lock: false
  164. })
  165. console.log('that.data.source', that.data.source)
  166. if (that.data.source) {
  167. wx.navigateBack()
  168. wx.navigateBack()
  169. } else {
  170. wx.navigateBack()
  171. }
  172. },
  173. onShareAppMessage: function (val) {
  174. return _request.share({
  175. sc: 'xcx_user_recharge'
  176. })
  177. }
  178. })