import { request } from '../../utils/request' Page({ data: { balance: '0.00', // 可提现余额 withdrawAmount: '', // 用户输入的提现金额 withdrawMethods: [ // { id: 'alipay', name: '支付宝', icon: '/static/images/alipay.png' }, { id: 'wechat', name: '微信', icon: '/static/images/wechat.png' }, // { id: 'bank', name: '银行卡', icon: '/static/images/bank.png' } ], selectedMethod: 'wechat', accountInfo: null }, onLoad() { this.getUserBalance() this.getUserAccountInfo() }, // 获取用户可提现余额 async getUserBalance() { try { console.log('开始获取最新收益数据...') const userInfo = wx.getStorageSync('userInfo'); const res = await request({ url: '/api/user/myEarnings', method: 'GET', data: { user_id: userInfo.id } }) console.log('获取最新收益数据结果:', res) if (res && res.rst) { console.log('收益数据获取成功,更新storage') const earningsData = { this_day_forecast_income: res.rst.this_day_forecast_income || '0.00', this_month_forecast_income: res.rst.this_month_forecast_income || '0.00', all_settlement_income: res.rst.all_settlement_income || '0.00', account_balance: res.rst.account_balance || '0.00', wait_settlement_income: res.rst.wait_settlement_income || '0.00' } wx.setStorageSync('earnings_data', earningsData) // 更新页面显示的余额 this.setData({ balance: earningsData.account_balance }) } else { throw new Error('获取收益数据失败:返回数据格式错误') } } catch (error) { console.error('获取收益数据失败:', error) wx.showToast({ title: '获取余额失败,请重试', icon: 'none' }) // 如果接口调用失败,尝试使用本地缓存的数据 const earningsData = wx.getStorageSync('earnings_data') || {} const balance = earningsData.account_balance || '0.00' this.setData({ balance: balance.toString() }) } }, // 获取用户账户信息 getUserAccountInfo() { // TODO: 调用后端API获取账户信息 this.setData({ accountInfo: { alipay: '138****1234', wechat: '微信用户', bank: { bankName: '中国银行', cardNumber: '**** **** **** 5678' } } }) }, // 选择提现方式 selectWithdrawMethod(e) { const { id } = e.currentTarget.dataset this.setData({ selectedMethod: id }) }, // 输入提现金额 onAmountInput(e) { this.setData({ withdrawAmount: e.detail.value }) }, // 全部提现 withdrawAll() { this.setData({ withdrawAmount: this.data.balance }) }, // 提现申请 submitWithdraw() { const { withdrawAmount, selectedMethod, balance } = this.data // 验证提现金额 if (!withdrawAmount) { wx.showToast({ title: '请输入提现金额', icon: 'none' }) return } const amount = parseFloat(withdrawAmount) const maxBalance = parseFloat(balance) if (isNaN(amount) || amount <= 0) { wx.showToast({ title: '请输入有效的金额', icon: 'none' }) return } if (amount > maxBalance) { wx.showToast({ title: '提现金额不能超过可用余额', icon: 'none' }) return } // 验证提现方式 if (!selectedMethod) { wx.showToast({ title: '请选择提现方式', icon: 'none' }) return } this.processWithdraw(amount) }, // 处理提现流程 async processWithdraw(amount) { try { console.log('开始提现流程,金额:', amount) // 1. 调用后端提现接口 console.log('调用提现接口...') const userInfo = wx.getStorageSync('userInfo'); const res = await request({ url: '/api/user/withdrawal', method: 'POST', data: { user_id: userInfo.id, money: amount } }) if (res.errno !== '0') { throw new Error(res.err || '提现申请失败') } console.log('提现接口调用成功,获取到package信息') const transferPackage = res.rst.package // 2. 调用微信支付转账API console.log('开始调用微信支付转账API...') if (wx.canIUse('requestMerchantTransfer')) { wx.requestMerchantTransfer({ mchId: "1505538321", appId: "wxa08b928141b4fa1d", package: transferPackage, success: (res) => { // res.err_msg将在页面展示成功后返回应用时返回ok,并不代表付款成功 console.log('success:', res); wx.showToast({ title: '提现申请成功', icon: 'success' }) }, fail: (res) => { console.log('fail:', res); }, }); } else { wx.showModal({ content: '你的微信版本过低,请更新至最新版本。', showCancel: false, }); } console.log('转账API调用成功') // 3. 刷新余额 this.getUserBalance() } catch (error) { console.error('提现过程出错:', error) wx.showToast({ title: error.message || '提现失败,请重试', icon: 'none' }) } }, // 获取提现方式名称 getMethodName(method) { const methodMap = { 'alipay': '支付宝', 'wechat': '微信', 'bank': '银行卡' } return methodMap[method] || '未知方式' }, // 管理收款账户 manageAccounts() { wx.navigateTo({ url: '/pages/bank/list' }) } })