recharge.js 6.4 KB

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