recharge.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. var _request = require('../../../../utils/request.js')
  2. var appConfig = require('../../../../utils/config.js')
  3. var PENDING_VIRTUAL_RECHARGE_ORDER_KEY = 'pendingVirtualRechargeOrderId'
  4. var RECHARGE_WXPAY = 'recharge_wxpay'
  5. function isCashVrPayEnabled () {
  6. return appConfig.cashVrPay === true
  7. }
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. price: '',
  14. cashTotal: 0,
  15. lock: false,
  16. syncingPendingRecharge: false,
  17. source:''
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function (options) {
  23. console.log('options',options);
  24. if(options && options['source']){
  25. this.setData({
  26. source:options['source']
  27. })
  28. }
  29. this.getCashTotal()
  30. },
  31. onShow: function () {
  32. if (isCashVrPayEnabled()) {
  33. this.syncPendingVirtualRecharge()
  34. } else {
  35. wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
  36. }
  37. },
  38. getCashTotal () {
  39. var that = this
  40. var url = 'v1/user/cash/balance/info'
  41. var params = {
  42. }
  43. var success = function (res) {
  44. that.setData({
  45. cashTotal: res.data
  46. })
  47. }
  48. _request.$get(url, params, success)
  49. },
  50. rechargePrice (e) {
  51. console.log(e)
  52. var that = this
  53. that.setData({
  54. price: e.detail.value
  55. })
  56. },
  57. createRecharge () {
  58. var that = this
  59. var price = parseFloat(that.data.price)
  60. var money = Math.round(price * 100)
  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 (isCashVrPayEnabled() && 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. if (isCashVrPayEnabled()) {
  85. that.createVirtualRecharge(money)
  86. } else {
  87. that.createWxRecharge(money)
  88. }
  89. }
  90. }
  91. },
  92. createWxRecharge (money) {
  93. var that = this
  94. var url = 'v1/recas_order/generate'
  95. var params = {
  96. count: money,
  97. payway: RECHARGE_WXPAY
  98. }
  99. var success = function (res) {
  100. if (res.data) {
  101. that.wxRechargeClick(res.data)
  102. } else {
  103. that.setData({
  104. lock: false
  105. })
  106. }
  107. }
  108. var fail = function () {
  109. that.setData({
  110. lock: false
  111. })
  112. }
  113. _request.$post(url, params, success, fail)
  114. },
  115. wxRechargeClick (order) {
  116. var that = this
  117. var url = 'v1/pay'
  118. var params = {
  119. order_id: order.order_id,
  120. pay_way: order.pay_way || RECHARGE_WXPAY
  121. }
  122. var success = function (res) {
  123. var payData = res.data && res.data.pay_data
  124. if (!payData) {
  125. that.setData({
  126. lock: false
  127. })
  128. wx.showToast({
  129. title: '支付参数异常',
  130. icon: 'none',
  131. duration: 2000
  132. })
  133. return
  134. }
  135. wx.requestPayment({
  136. timeStamp: payData.timeStamp,
  137. nonceStr: payData.nonceStr,
  138. package: payData.package,
  139. signType: payData.signType,
  140. paySign: payData.paySign,
  141. success: function () {
  142. getApp().globalData.rechargeChanged = true
  143. that.getCashTotal()
  144. wx.showToast({
  145. title: '充值成功',
  146. icon: 'success',
  147. duration: 1000
  148. })
  149. setTimeout(function () {
  150. that.backAfterRecharge()
  151. }, 1000)
  152. },
  153. fail: function (err) {
  154. var errMsg = err && err.errMsg ? err.errMsg : ''
  155. if (errMsg.indexOf('cancel') >= 0 || errMsg.indexOf('取消') >= 0) {
  156. wx.showToast({
  157. title: '用户取消支付',
  158. icon: 'none',
  159. duration: 2000
  160. })
  161. } else {
  162. wx.showToast({
  163. title: '支付失败',
  164. icon: 'none',
  165. duration: 2000
  166. })
  167. }
  168. that.setData({
  169. lock: false
  170. })
  171. }
  172. })
  173. }
  174. var fail = function () {
  175. that.setData({
  176. lock: false
  177. })
  178. }
  179. _request.$post(url, params, success, fail)
  180. },
  181. createVirtualRecharge (money) {
  182. var that = this
  183. var url = 'v1/virtual_pay/recharge'
  184. var params = {
  185. count: money
  186. }
  187. var success = function (res) {
  188. if (res.data) {
  189. if (res.data.order_id) {
  190. wx.setStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY, res.data.order_id)
  191. }
  192. that.rechargeClick(res.data)
  193. }
  194. }
  195. var fail = function () {
  196. that.setData({
  197. lock: false
  198. })
  199. }
  200. _request.$post(url, params, success, fail)
  201. },
  202. rechargeClick (payData) {
  203. var that = this
  204. console.log('requestVirtualPayment payData', payData)
  205. if (!wx.requestVirtualPayment && !wx.canIUse('requestVirtualPayment')) {
  206. that.setData({
  207. lock: false
  208. })
  209. wx.showToast({
  210. title: '当前微信版本不支持虚拟支付',
  211. icon: 'none',
  212. duration: 2000
  213. })
  214. return
  215. }
  216. wx.requestVirtualPayment({
  217. env: payData.env,
  218. mode: payData.mode,
  219. offerId: payData.offerId,
  220. signData: payData.signData,
  221. paySig: payData.paySig,
  222. signature: payData.signature,
  223. success: function (res) {
  224. console.log('requestVirtualPayment success', res)
  225. wx.showToast({
  226. title: '充值处理中',
  227. icon: 'success',
  228. duration: 1000
  229. })
  230. that.waitRechargePaid(payData.order_id, 0)
  231. },
  232. fail: function (err) {
  233. console.error('requestVirtualPayment fail', err)
  234. var errMsg = err && (err.errMsg || err.err_code || err.errCode) ? (err.errMsg || err.err_code || err.errCode) : ''
  235. if (errMsg.indexOf('cancel') >= 0 || errMsg.indexOf('取消') >= 0) {
  236. wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
  237. that.setData({
  238. lock: false
  239. })
  240. wx.showToast({
  241. title: '用户取消支付',
  242. icon: 'none',
  243. duration: 2000
  244. })
  245. return
  246. }
  247. wx.showToast({
  248. title: '支付结果确认中',
  249. icon: 'none',
  250. duration: 1500
  251. })
  252. that.waitRechargePaid(payData.order_id, 0)
  253. }
  254. })
  255. },
  256. waitRechargePaid (oid, times) {
  257. var that = this
  258. var url = 'v1/virtual_pay/recharge/status'
  259. var success = function (res) {
  260. if (res.data && res.data.state === 1) {
  261. wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
  262. getApp().globalData.rechargeChanged = true
  263. that.getCashTotal()
  264. wx.showToast({
  265. title: '充值成功',
  266. icon: 'success',
  267. duration: 1000
  268. })
  269. setTimeout(function () {
  270. that.backAfterRecharge()
  271. }, 1000)
  272. } else if (times < 5) {
  273. setTimeout(function () {
  274. that.waitRechargePaid(oid, times + 1)
  275. }, 1000)
  276. } else {
  277. that.setData({
  278. lock: false
  279. })
  280. wx.showToast({
  281. title: '支付已提交,到账稍后刷新',
  282. icon: 'none',
  283. duration: 2000
  284. })
  285. that.getCashTotal()
  286. }
  287. }
  288. var fail = function () {
  289. that.setData({
  290. lock: false
  291. })
  292. }
  293. _request.$get(url, { order_id: oid }, success, fail)
  294. },
  295. syncPendingVirtualRecharge () {
  296. var that = this
  297. if (that.data.syncingPendingRecharge) {
  298. return
  299. }
  300. var pendingOrderId = wx.getStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
  301. var url = 'v1/virtual_pay/recharge/sync_pending'
  302. var success = function (res) {
  303. that.setData({
  304. syncingPendingRecharge: false
  305. })
  306. if (res.data && res.data.synced_count > 0) {
  307. wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
  308. getApp().globalData.rechargeChanged = true
  309. that.getCashTotal()
  310. wx.showToast({
  311. title: '充值成功',
  312. icon: 'success',
  313. duration: 1000
  314. })
  315. } else if (pendingOrderId) {
  316. that.waitRechargePaid(pendingOrderId, 0)
  317. }
  318. }
  319. var fail = function () {
  320. that.setData({
  321. syncingPendingRecharge: false
  322. })
  323. }
  324. that.setData({
  325. syncingPendingRecharge: true
  326. })
  327. _request.$get(url, {}, success, fail)
  328. },
  329. backAfterRecharge () {
  330. var that = this
  331. that.setData({
  332. lock: false
  333. })
  334. that.refreshPreviousPage()
  335. console.log('that.data.source', that.data.source)
  336. if (that.data.source) {
  337. wx.navigateBack()
  338. wx.navigateBack()
  339. } else {
  340. wx.navigateBack()
  341. }
  342. },
  343. refreshPreviousPage () {
  344. var pages = getCurrentPages()
  345. var delta = this.data.source ? 2 : 1
  346. var prevPage = pages[pages.length - 1 - delta]
  347. if (!prevPage) {
  348. return
  349. }
  350. if (prevPage.refreshBalancePage) {
  351. prevPage.refreshBalancePage()
  352. } else if (prevPage.refreshCashPage) {
  353. prevPage.refreshCashPage()
  354. } else if (prevPage.getCashTotal && prevPage.getCashList) {
  355. prevPage.getCashTotal()
  356. prevPage.getCashList()
  357. }
  358. },
  359. onShareAppMessage: function (val) {
  360. return _request.share({
  361. sc: 'xcx_user_recharge'
  362. })
  363. }
  364. })