recharge.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. wx.showToast({
  135. title: '用户取消支付',
  136. icon: 'none',
  137. duration: 2000
  138. })
  139. return
  140. }
  141. wx.showToast({
  142. title: '支付结果确认中',
  143. icon: 'none',
  144. duration: 1500
  145. })
  146. that.waitRechargePaid(payData.order_id, 0)
  147. }
  148. })
  149. },
  150. waitRechargePaid (oid, times) {
  151. var that = this
  152. var url = 'v1/virtual_pay/recharge/status'
  153. var success = function (res) {
  154. if (res.data && res.data.state === 1) {
  155. wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
  156. getApp().globalData.rechargeChanged = true
  157. that.getCashTotal()
  158. wx.showToast({
  159. title: '充值成功',
  160. icon: 'success',
  161. duration: 1000
  162. })
  163. setTimeout(function () {
  164. that.backAfterRecharge()
  165. }, 1000)
  166. } else if (times < 5) {
  167. setTimeout(function () {
  168. that.waitRechargePaid(oid, times + 1)
  169. }, 1000)
  170. } else {
  171. that.setData({
  172. lock: false
  173. })
  174. wx.showToast({
  175. title: '支付已提交,到账稍后刷新',
  176. icon: 'none',
  177. duration: 2000
  178. })
  179. that.getCashTotal()
  180. }
  181. }
  182. var fail = function () {
  183. that.setData({
  184. lock: false
  185. })
  186. }
  187. _request.$get(url, { order_id: oid }, success, fail)
  188. },
  189. syncPendingVirtualRecharge () {
  190. var that = this
  191. if (that.data.syncingPendingRecharge) {
  192. return
  193. }
  194. var pendingOrderId = wx.getStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
  195. var url = 'v1/virtual_pay/recharge/sync_pending'
  196. var success = function (res) {
  197. that.setData({
  198. syncingPendingRecharge: false
  199. })
  200. if (res.data && res.data.synced_count > 0) {
  201. wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
  202. getApp().globalData.rechargeChanged = true
  203. that.getCashTotal()
  204. wx.showToast({
  205. title: '充值成功',
  206. icon: 'success',
  207. duration: 1000
  208. })
  209. } else if (pendingOrderId) {
  210. that.waitRechargePaid(pendingOrderId, 0)
  211. }
  212. }
  213. var fail = function () {
  214. that.setData({
  215. syncingPendingRecharge: false
  216. })
  217. }
  218. that.setData({
  219. syncingPendingRecharge: true
  220. })
  221. _request.$get(url, {}, success, fail)
  222. },
  223. backAfterRecharge () {
  224. var that = this
  225. that.setData({
  226. lock: false
  227. })
  228. that.refreshPreviousPage()
  229. console.log('that.data.source', that.data.source)
  230. if (that.data.source) {
  231. wx.navigateBack()
  232. wx.navigateBack()
  233. } else {
  234. wx.navigateBack()
  235. }
  236. },
  237. refreshPreviousPage () {
  238. var pages = getCurrentPages()
  239. var delta = this.data.source ? 2 : 1
  240. var prevPage = pages[pages.length - 1 - delta]
  241. if (!prevPage) {
  242. return
  243. }
  244. if (prevPage.refreshBalancePage) {
  245. prevPage.refreshBalancePage()
  246. } else if (prevPage.refreshCashPage) {
  247. prevPage.refreshCashPage()
  248. } else if (prevPage.getCashTotal && prevPage.getCashList) {
  249. prevPage.getCashTotal()
  250. prevPage.getCashList()
  251. }
  252. },
  253. onShareAppMessage: function (val) {
  254. return _request.share({
  255. sc: 'xcx_user_recharge'
  256. })
  257. }
  258. })