withdraw.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. var _request = require('../../../../utils/request.js')
  2. function formatWithdrawStateText (state) {
  3. if (state === 'WAIT_USER_CONFIRM') {
  4. return '待确认,确认后到帐'
  5. }
  6. return state || ''
  7. }
  8. function uniqueWithdrawList (list) {
  9. var result = []
  10. var seen = {}
  11. ;(list || []).forEach(function (item) {
  12. var key = item.order_id || item.id
  13. if (!key) {
  14. result.push(item)
  15. return
  16. }
  17. if (seen[key]) {
  18. return
  19. }
  20. seen[key] = true
  21. result.push(item)
  22. })
  23. return result
  24. }
  25. Page({
  26. /**
  27. * 页面的初始数据
  28. */
  29. data: {
  30. price: '',
  31. page: 1,
  32. per_page: 10,
  33. cashList: [],
  34. cashList_more: true,
  35. cashList_change: false,
  36. cashTotal: 0,
  37. confirmedTransferIds: {}
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: function (options) {
  43. this.refreshCashList()
  44. },
  45. onShow () {
  46. if (getApp().globalData.withdrawChanged) {
  47. getApp().globalData.withdrawChanged = false
  48. this.refreshCashList()
  49. }
  50. },
  51. onPullDownRefresh: function () {
  52. this.setData({
  53. page: 1,
  54. cashList_more: true,
  55. cashList_change: true
  56. })
  57. this.getCashList()
  58. wx.stopPullDownRefresh()
  59. },
  60. onReachBottom: function () {
  61. if (this.data.cashList_more) {
  62. var page = this.data.page + 1
  63. this.setData({
  64. page: page
  65. })
  66. this.getCashList()
  67. }
  68. },
  69. refreshCashList () {
  70. this.setData({
  71. page: 1,
  72. cashList: [],
  73. cashList_more: true,
  74. cashList_change: false
  75. })
  76. this.getCashTotal()
  77. this.getCashList()
  78. },
  79. getCashTotal () {
  80. var that = this
  81. var url = 'v1/user/cash/balance/info'
  82. var params = {
  83. }
  84. var success = function (res) {
  85. that.setData({
  86. cashTotal: res.data
  87. })
  88. }
  89. _request.$get(url, params, success)
  90. },
  91. getCashList () {
  92. console.log(this.data.page)
  93. var that = this
  94. var requestPage = that.data.page
  95. var url = 'v1/user/takecash/flow'
  96. var params = {
  97. page: requestPage,
  98. per_page: that.data.per_page
  99. }
  100. var success = function (res) {
  101. var shouldReplace = requestPage === 1 || that.data.cashList_change
  102. if (that.data.cashList_change) {
  103. that.setData({
  104. cashList: [],
  105. cashList_change: false
  106. })
  107. }
  108. var confirmedTransferIds = that.data.confirmedTransferIds || {}
  109. var list = res.data.list || []
  110. list.forEach(function (item) {
  111. if (item.state_cn === 'WAIT_USER_CONFIRM') {
  112. item.state_cn = formatWithdrawStateText(item.state_cn)
  113. } else if (item.transfer_state === 'WAIT_USER_CONFIRM') {
  114. item.state_cn = formatWithdrawStateText(item.transfer_state)
  115. }
  116. if (confirmedTransferIds[item.id]) {
  117. item.can_confirm_transfer = false
  118. }
  119. })
  120. var result = uniqueWithdrawList((shouldReplace ? [] : that.data.cashList).concat(list))
  121. var listMore = res.data.list_count > result.length
  122. that.setData({
  123. cashList: result,
  124. cashList_more: listMore
  125. })
  126. }
  127. _request.$get(url, params, success)
  128. },
  129. confirmMerchantTransfer (e) {
  130. var that = this
  131. var index = e.currentTarget.dataset.index
  132. var item = that.data.cashList[index] || {}
  133. if (item.confirming_transfer) {
  134. return
  135. }
  136. if (!item.can_confirm_transfer || !item.package_info) {
  137. wx.showToast({
  138. title: '当前提现无需确认收款',
  139. icon: 'none',
  140. duration: 2000
  141. })
  142. return
  143. }
  144. if (!wx.canIUse || !wx.canIUse('requestMerchantTransfer')) {
  145. wx.showToast({
  146. title: '当前微信版本不支持确认收款,请升级微信',
  147. icon: 'none',
  148. duration: 2000
  149. })
  150. return
  151. }
  152. var cashList = that.data.cashList
  153. cashList[index].can_confirm_transfer = false
  154. cashList[index].confirming_transfer = true
  155. that.setData({
  156. cashList: cashList
  157. })
  158. wx.requestMerchantTransfer({
  159. mchId: String(item.mch_id || ''),
  160. appId: String(item.app_id || ''),
  161. package: item.package_info,
  162. success: function () {
  163. wx.showToast({
  164. title: '确认收款已提交',
  165. icon: 'none',
  166. duration: 2000
  167. })
  168. var confirmedTransferIds = that.data.confirmedTransferIds || {}
  169. confirmedTransferIds[item.id] = true
  170. getApp().globalData.withdrawChanged = true
  171. that.setData({
  172. page: 1,
  173. cashList_more: true,
  174. cashList_change: true,
  175. confirmedTransferIds: confirmedTransferIds
  176. })
  177. that.getCashList()
  178. },
  179. fail: function (res) {
  180. var cashList = that.data.cashList
  181. if (cashList[index]) {
  182. cashList[index].can_confirm_transfer = true
  183. cashList[index].confirming_transfer = false
  184. that.setData({
  185. cashList: cashList
  186. })
  187. }
  188. wx.showToast({
  189. title: (res && res.errMsg) ? res.errMsg : '确认收款失败',
  190. icon: 'none',
  191. duration: 3000
  192. })
  193. }
  194. })
  195. },
  196. onShareAppMessage: function (val) {
  197. return _request.share({
  198. sc: 'xcx_user_withdraw'
  199. })
  200. }
  201. })