cash.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. var _request = require('../../../../utils/request.js')
  2. var util = require('../../../../utils/util.js')
  3. var MAX_MERCHANT_TRANSFER_CASH = 20000
  4. var PENDING_VIRTUAL_RECHARGE_ORDER_KEY = 'pendingVirtualRechargeOrderId'
  5. Page({
  6. data: {
  7. page: 1,
  8. per_page: 20,
  9. list: [],
  10. more: true,
  11. balance: 0,
  12. confirmDialog:false,
  13. min_cash:0,
  14. max_cahs:0,
  15. fee:0,
  16. withdraw_num:0,
  17. view_withdraw:0,
  18. view_fee:0,
  19. syncingPendingRecharge:false,
  20. submittingTakeCash:false,
  21. transferConfirmDialog:false,
  22. pendingTakeCash:null,
  23. transferConfirming:false,
  24. transferCanceling:false
  25. },
  26. onLoad: function (options) {
  27. this.getBalanceInfo()
  28. this.getBalanceList()
  29. // this.getCashTotal()
  30. },
  31. onShow () {
  32. this.syncPendingVirtualRecharge()
  33. if (getApp().globalData.rechargeChanged) {
  34. getApp().globalData.rechargeChanged = false
  35. this.refreshBalancePage()
  36. }
  37. },
  38. onReachBottom: function () {
  39. if (this.data.more) {
  40. var page = this.data.page + 1
  41. this.setData({
  42. page: page
  43. })
  44. this.getBalanceList()
  45. }
  46. },
  47. getBalanceInfo () {
  48. var that = this
  49. var url = 'v1/user/cash/balance/info'
  50. var params = {
  51. }
  52. var success = function (res) {
  53. var result = res.data.available
  54. that.setData({
  55. balance: result,
  56. cashTotal: res.data
  57. })
  58. that.getWithDrawlimit()
  59. }
  60. _request.$get(url, params, success)
  61. },
  62. getBalanceList () {
  63. var that = this
  64. var url = 'v1/user/cash/balances'
  65. var params = {
  66. page: this.data.page,
  67. per_page: this.data.per_page
  68. }
  69. var success = function (res) {
  70. var result = that.data.list.concat(res.data.balance_list || [])
  71. that.setData({
  72. list: result
  73. })
  74. var listMore = res.data.balance_count > that.data.list.length
  75. that.setData({
  76. more: listMore
  77. })
  78. }
  79. _request.$get(url, params, success)
  80. },
  81. refreshBalancePage () {
  82. this.setData({
  83. page: 1,
  84. list: [],
  85. more: true
  86. })
  87. this.getBalanceInfo()
  88. this.getBalanceList()
  89. },
  90. syncPendingVirtualRecharge () {
  91. var that = this
  92. if (that.data.syncingPendingRecharge) {
  93. return
  94. }
  95. var url = 'v1/virtual_pay/recharge/sync_pending'
  96. var success = function (res) {
  97. that.setData({
  98. syncingPendingRecharge: false
  99. })
  100. if (res.data && res.data.synced_count > 0) {
  101. wx.removeStorageSync(PENDING_VIRTUAL_RECHARGE_ORDER_KEY)
  102. getApp().globalData.rechargeChanged = false
  103. wx.showToast({
  104. title: '充值成功',
  105. icon: 'success',
  106. duration: 1000
  107. })
  108. that.refreshBalancePage()
  109. }
  110. }
  111. var fail = function () {
  112. that.setData({
  113. syncingPendingRecharge: false
  114. })
  115. }
  116. that.setData({
  117. syncingPendingRecharge: true
  118. })
  119. _request.$get(url, {}, success, fail)
  120. },
  121. getDetail (val) {
  122. var id = val.currentTarget.dataset.val
  123. wx.navigateTo({
  124. url: '/packageUser/pages/user/cashDetail/cashDetail?id=' + id
  125. })
  126. },
  127. onShareAppMessage: function (val) {
  128. return _request.share({
  129. sc: 'xcx_user_radish'
  130. })
  131. },
  132. getCashTotal () {
  133. var that = this
  134. var url = 'v1/user/cash/balance/info'
  135. var params = {
  136. }
  137. var success = function (res) {
  138. console.log('res.data',res.data)
  139. that.setData({
  140. cashTotal: res.data
  141. })
  142. }
  143. _request.$get(url, params, success)
  144. },
  145. getWithDrawlimit () {
  146. var that = this
  147. var url = 'v1/user/takecash/limit'
  148. var params = {}
  149. var success = function (res) {
  150. var maxCash = Math.min(res.data.max_limit_cash || MAX_MERCHANT_TRANSFER_CASH, MAX_MERCHANT_TRANSFER_CASH)
  151. var withdrawnum = that.data.balance > maxCash ? maxCash : that.data.balance;
  152. that.setData({
  153. min_cash: res.data.min_limit_cash,
  154. max_cash: maxCash,
  155. fee:res.data.fee_bl ? res.data.fee_bl : 0,
  156. withdraw_num: withdrawnum,
  157. view_withdraw:parseFloat(withdrawnum/100).toFixed(2),
  158. view_fee:parseFloat(withdrawnum * (res.data.fee_bl?res.data.fee_bl/100:0)/100).toFixed(2)
  159. })
  160. }
  161. _request.$get(url, params, success)
  162. },
  163. goWithdrawClick () {
  164. wx.navigateTo({
  165. url: '/packageUser/pages/user/withdraw/withdraw'
  166. })
  167. },
  168. getBankInfoAndTip(){
  169. var that = this
  170. that.showWithdrawConfirm()
  171. },
  172. showWithdrawConfirm () {
  173. var that = this
  174. // var url = 'v1/user/get_bank_info'
  175. // var params = {
  176. // }
  177. // var success = function (res) {
  178. // that.setData({
  179. // has_bank:res.data.has_bank
  180. // })
  181. // if(that.data.has_bank){
  182. that.data.confirmDialog = !that.data.confirmDialog
  183. if (that.data.withdraw_num > that.data.min_cash) {
  184. that.setData({
  185. confirmDialog: that.data.confirmDialog
  186. })
  187. } else {
  188. wx.showToast({
  189. title: '单次提现金额范围,大于'+parseFloat(that.data.min_cash/100).toFixed(2) +'元 单次最高'+parseFloat(that.data.max_cash/100).toFixed(2) +'元',
  190. icon: 'none',
  191. duration: 2000
  192. })
  193. }
  194. // }
  195. // _request.$get(url, params, success)
  196. },
  197. confrimClick () {
  198. var that = this
  199. console.log('that.data.has_bank',that.data.has_bank)
  200. // if (that.data.has_bank) {
  201. that.withdrawClick()
  202. // } else {
  203. // that.setData({
  204. // confirmDialog: false,
  205. // identityDialog: true
  206. // })
  207. // }
  208. },
  209. closedDialog () {
  210. var that = this
  211. that.setData({
  212. confirmDialog: false
  213. })
  214. },
  215. withdrawClick () {
  216. var that = this
  217. if (that.data.submittingTakeCash) {
  218. return
  219. }
  220. var url = 'v1/user/takecash'
  221. var params = {
  222. amount: that.data.withdraw_num
  223. }
  224. that.setData({
  225. submittingTakeCash: true
  226. })
  227. var success = function (res) {
  228. that.setData({
  229. submittingTakeCash: false
  230. })
  231. if (res.data) {
  232. that.setData({
  233. confirmDialog: false
  234. })
  235. if (res.data.can_confirm_transfer && res.data.package_info) {
  236. that.showTakeCashTransferConfirm(res.data)
  237. } else {
  238. that.finishTakeCashTransfer('提现成功~')
  239. }
  240. }
  241. }
  242. var fail = function () {
  243. that.setData({
  244. submittingTakeCash: false
  245. })
  246. }
  247. _request.$post(url, params, success, fail)
  248. },
  249. showTakeCashTransferConfirm (order) {
  250. this.setData({
  251. pendingTakeCash: order,
  252. transferConfirmDialog: true,
  253. transferConfirming: false,
  254. transferCanceling: false
  255. })
  256. },
  257. confirmTakeCashTransfer () {
  258. var that = this
  259. var order = that.data.pendingTakeCash || {}
  260. if (that.data.transferConfirming) {
  261. return
  262. }
  263. if (!order.package_info) {
  264. wx.showToast({
  265. title: '提现确认信息缺失,请稍后重试',
  266. icon: 'none',
  267. duration: 2000
  268. })
  269. return
  270. }
  271. if (!wx.canIUse || !wx.canIUse('requestMerchantTransfer')) {
  272. wx.showToast({
  273. title: '当前微信版本不支持确认收款,请升级微信',
  274. icon: 'none',
  275. duration: 2000
  276. })
  277. return
  278. }
  279. that.setData({
  280. transferConfirming: true
  281. })
  282. wx.requestMerchantTransfer({
  283. mchId: String(order.mch_id || ''),
  284. appId: String(order.app_id || ''),
  285. package: order.package_info,
  286. success: function () {
  287. that.setData({
  288. transferConfirmDialog: false,
  289. pendingTakeCash: null,
  290. transferConfirming: false
  291. })
  292. that.finishTakeCashTransfer('确认收款已提交')
  293. },
  294. fail: function (res) {
  295. that.setData({
  296. transferConfirming: false
  297. })
  298. var errMsg = (res && res.errMsg) ? res.errMsg : ''
  299. wx.showToast({
  300. title: errMsg.indexOf('cancel') >= 0 ? '用户取消确认,可继续确认或取消提现' : '确认收款失败,请稍后重试',
  301. icon: 'none',
  302. duration: 3000
  303. })
  304. }
  305. })
  306. },
  307. cancelTakeCashTransfer () {
  308. var that = this
  309. var order = that.data.pendingTakeCash || {}
  310. if (that.data.transferCanceling) {
  311. return
  312. }
  313. if (!order.order_id && !order.id) {
  314. that.setData({
  315. transferConfirmDialog: false,
  316. pendingTakeCash: null
  317. })
  318. return
  319. }
  320. that.setData({
  321. transferCanceling: true
  322. })
  323. var params = {
  324. order_id: order.order_id || '',
  325. id: order.id || ''
  326. }
  327. var success = function () {
  328. that.setData({
  329. transferConfirmDialog: false,
  330. pendingTakeCash: null,
  331. transferCanceling: false
  332. })
  333. wx.showToast({
  334. title: '已取消提现',
  335. icon: 'none',
  336. duration: 2000
  337. })
  338. getApp().globalData.withdrawChanged = true
  339. that.refreshBalancePage()
  340. that.getCashTotal()
  341. }
  342. var fail = function () {
  343. that.setData({
  344. transferCanceling: false
  345. })
  346. }
  347. _request.$post('v1/user/takecash/cancel', params, success, fail)
  348. },
  349. finishTakeCashTransfer (title) {
  350. wx.showToast({
  351. title: title || '提现成功~',
  352. icon: 'none',
  353. duration: 2000
  354. })
  355. getApp().globalData.withdrawChanged = true
  356. this.refreshBalancePage()
  357. this.getCashTotal()
  358. wx.navigateTo({
  359. url: '/packageUser/pages/user/withdraw/withdraw'
  360. })
  361. },
  362. })