微信小店联盟带货小程序

withdraw.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { request } from '../../utils/request'
  2. Page({
  3. data: {
  4. balance: '0.00', // 可提现余额
  5. withdrawAmount: '', // 用户输入的提现金额
  6. withdrawMethods: [
  7. // { id: 'alipay', name: '支付宝', icon: '/static/images/alipay.png' },
  8. { id: 'wechat', name: '微信', icon: '/static/images/wechat.png' },
  9. // { id: 'bank', name: '银行卡', icon: '/static/images/bank.png' }
  10. ],
  11. selectedMethod: 'wechat',
  12. accountInfo: null
  13. },
  14. onLoad() {
  15. this.getUserBalance()
  16. this.getUserAccountInfo()
  17. },
  18. // 获取用户可提现余额
  19. async getUserBalance() {
  20. try {
  21. console.log('开始获取最新收益数据...')
  22. const userInfo = wx.getStorageSync('userInfo');
  23. const res = await request({
  24. url: '/api/user/myEarnings',
  25. method: 'GET',
  26. data: {
  27. user_id: userInfo.id
  28. }
  29. })
  30. console.log('获取最新收益数据结果:', res)
  31. if (res && res.rst) {
  32. console.log('收益数据获取成功,更新storage')
  33. const earningsData = {
  34. this_day_forecast_income: res.rst.this_day_forecast_income || '0.00',
  35. this_month_forecast_income: res.rst.this_month_forecast_income || '0.00',
  36. all_settlement_income: res.rst.all_settlement_income || '0.00',
  37. account_balance: res.rst.account_balance || '0.00',
  38. wait_settlement_income: res.rst.wait_settlement_income || '0.00'
  39. }
  40. wx.setStorageSync('earnings_data', earningsData)
  41. // 更新页面显示的余额
  42. this.setData({
  43. balance: earningsData.account_balance
  44. })
  45. } else {
  46. throw new Error('获取收益数据失败:返回数据格式错误')
  47. }
  48. } catch (error) {
  49. console.error('获取收益数据失败:', error)
  50. wx.showToast({
  51. title: '获取余额失败,请重试',
  52. icon: 'none'
  53. })
  54. // 如果接口调用失败,尝试使用本地缓存的数据
  55. const earningsData = wx.getStorageSync('earnings_data') || {}
  56. const balance = earningsData.account_balance || '0.00'
  57. this.setData({
  58. balance: balance.toString()
  59. })
  60. }
  61. },
  62. // 获取用户账户信息
  63. getUserAccountInfo() {
  64. // TODO: 调用后端API获取账户信息
  65. this.setData({
  66. accountInfo: {
  67. alipay: '138****1234',
  68. wechat: '微信用户',
  69. bank: {
  70. bankName: '中国银行',
  71. cardNumber: '**** **** **** 5678'
  72. }
  73. }
  74. })
  75. },
  76. // 选择提现方式
  77. selectWithdrawMethod(e) {
  78. const { id } = e.currentTarget.dataset
  79. this.setData({
  80. selectedMethod: id
  81. })
  82. },
  83. // 输入提现金额
  84. onAmountInput(e) {
  85. this.setData({
  86. withdrawAmount: e.detail.value
  87. })
  88. },
  89. // 全部提现
  90. withdrawAll() {
  91. this.setData({
  92. withdrawAmount: this.data.balance
  93. })
  94. },
  95. // 提现申请
  96. submitWithdraw() {
  97. const { withdrawAmount, selectedMethod, balance } = this.data
  98. // 验证提现金额
  99. if (!withdrawAmount) {
  100. wx.showToast({
  101. title: '请输入提现金额',
  102. icon: 'none'
  103. })
  104. return
  105. }
  106. const amount = parseFloat(withdrawAmount)
  107. const maxBalance = parseFloat(balance)
  108. if (isNaN(amount) || amount <= 0) {
  109. wx.showToast({
  110. title: '请输入有效的金额',
  111. icon: 'none'
  112. })
  113. return
  114. }
  115. if (amount > maxBalance) {
  116. wx.showToast({
  117. title: '提现金额不能超过可用余额',
  118. icon: 'none'
  119. })
  120. return
  121. }
  122. // 验证提现方式
  123. if (!selectedMethod) {
  124. wx.showToast({
  125. title: '请选择提现方式',
  126. icon: 'none'
  127. })
  128. return
  129. }
  130. this.processWithdraw(amount)
  131. },
  132. // 处理提现流程
  133. async processWithdraw(amount) {
  134. try {
  135. console.log('开始提现流程,金额:', amount)
  136. // 1. 调用后端提现接口
  137. console.log('调用提现接口...')
  138. const userInfo = wx.getStorageSync('userInfo');
  139. const res = await request({
  140. url: '/api/user/withdrawal',
  141. method: 'POST',
  142. data: {
  143. user_id: userInfo.id,
  144. money: amount
  145. }
  146. })
  147. if (res.errno !== '0') {
  148. throw new Error(res.err || '提现申请失败')
  149. }
  150. console.log('提现接口调用成功,获取到package信息')
  151. const transferPackage = res.rst.package
  152. // 2. 调用微信支付转账API
  153. console.log('开始调用微信支付转账API...')
  154. if (wx.canIUse('requestMerchantTransfer')) {
  155. wx.requestMerchantTransfer({
  156. mchId: "1505538321",
  157. appId: "wxa08b928141b4fa1d",
  158. package: transferPackage,
  159. success: (res) => {
  160. // res.err_msg将在页面展示成功后返回应用时返回ok,并不代表付款成功
  161. console.log('success:', res);
  162. wx.showToast({
  163. title: '提现申请成功',
  164. icon: 'success'
  165. })
  166. },
  167. fail: (res) => {
  168. console.log('fail:', res);
  169. },
  170. });
  171. } else {
  172. wx.showModal({
  173. content: '你的微信版本过低,请更新至最新版本。',
  174. showCancel: false,
  175. });
  176. }
  177. console.log('转账API调用成功')
  178. // 3. 刷新余额
  179. this.getUserBalance()
  180. } catch (error) {
  181. console.error('提现过程出错:', error)
  182. wx.showToast({
  183. title: error.message || '提现失败,请重试',
  184. icon: 'none'
  185. })
  186. }
  187. },
  188. // 获取提现方式名称
  189. getMethodName(method) {
  190. const methodMap = {
  191. 'alipay': '支付宝',
  192. 'wechat': '微信',
  193. 'bank': '银行卡'
  194. }
  195. return methodMap[method] || '未知方式'
  196. },
  197. // 管理收款账户
  198. manageAccounts() {
  199. wx.navigateTo({
  200. url: '/pages/bank/list'
  201. })
  202. }
  203. })