const DarkModeUtil = require('../../utils/dark-mode.js') import { request } from '../../utils/request' import { getLoginInfo } from '../../utils/login' Page({ data: { isDarkMode: false, loading: true, earnings: { account_balance: '0.00', // 账户余额 this_day_forecast_income: '0.00', // 今日预估收入 last_day_forecast_income: '0.00', // 昨日预估收入 this_month_forecast_income: '0.00', // 本月预估收入 last_month_forecast_income: '0.00', // 上月预估收入 total_forecast_income: '0.00', // 总预估收入 this_month_settlement_income: '0.00', // 本月结算收入 all_settlement_income: '0.00', // 累计结算收入 wait_settlement_income: '0.00', // 待结算收入 withdraw_amount: '0.00' // 已提现金额 }, trends: { daily: 0, // 日收入趋势:1上升,-1下降,0持平 monthly: 0 // 月收入趋势:1上升,-1下降,0持平 }, incomeList: [], page: 1, pageSize: 10, hasMore: true }, onLoad() { // 应用深色模式 DarkModeUtil.applyPageDarkMode(this) // 监听深色模式变化 DarkModeUtil.listenDarkModeChange(this) // 加载收益数据 this.loadEarningsData() // 加载收入明细列表 this.loadIncomeList() }, // 计算趋势 calculateTrend(current, previous) { const currentValue = parseFloat(current) || 0 const previousValue = parseFloat(previous) || 0 if (currentValue > previousValue) return 1 if (currentValue < previousValue) return -1 return 0 }, // 加载收益数据 async loadEarningsData() { try { const userInfo = getLoginInfo() const res = await request({ url: '/api/user/myEarnings', method: 'GET', data: { user_id: userInfo.id } }) if (res.errno === '0' && res.rst) { // 计算日收入和月收入趋势 const dailyTrend = this.calculateTrend( res.rst.this_day_forecast_income, res.rst.last_day_forecast_income ) const monthlyTrend = this.calculateTrend( res.rst.this_month_forecast_income, res.rst.last_month_forecast_income ) this.setData({ earnings: { account_balance: res.rst.account_balance || '0.00', this_day_forecast_income: res.rst.this_day_forecast_income || '0.00', last_day_forecast_income: res.rst.last_day_forecast_income || '0.00', this_month_forecast_income: res.rst.this_month_forecast_income || '0.00', last_month_forecast_income: res.rst.last_month_forecast_income || '0.00', total_forecast_income: res.rst.total_forecast_income || '0.00', this_month_settlement_income: res.rst.this_month_settlement_income || '0.00', all_settlement_income: res.rst.all_settlement_income || '0.00', wait_settlement_income: res.rst.wait_settlement_income || '0.00', withdraw_amount: res.rst.withdraw_amount || '0.00' }, trends: { daily: dailyTrend, monthly: monthlyTrend }, loading: false }) // 保存原始数据到本地存储 wx.setStorageSync('earnings_data', res.rst) } else { console.error('获取收益数据失败:', res) wx.showToast({ title: res.err || '获取收益数据失败', icon: 'none' }) } } catch (error) { console.error('获取收益数据失败:', error) wx.showToast({ title: '获取收益数据失败', icon: 'none' }) } }, // 加载收入明细 async loadIncomeList(refresh = false) { if (refresh) { this.setData({ page: 1, hasMore: true, incomeList: [] }) } if (!this.data.hasMore) return try { const userInfo = getLoginInfo() const res = await request({ url: '/api/user/myEarningsDetail', method: 'GET', data: { user_id: userInfo.id, page: this.data.page, page_size: this.data.pageSize } }) if (res.errno === '0' && res.rst && res.rst.data) { const newList = res.rst.data.map(item => ({ id: item.id, type: item.type === 1 ? '佣金收入' : '提现', amount: item.rebate || '0.00', status: this.getStatusText(item.state_of_embodiment), createTime: item.create_at, note: item.note || '' })) this.setData({ incomeList: [...this.data.incomeList, ...newList], page: this.data.page + 1, hasMore: newList.length >= this.data.pageSize }) } else { console.error('获取收入明细失败:', res) wx.showToast({ title: res.err || '获取收入明细失败', icon: 'none' }) } } catch (error) { console.error('获取收入明细失败:', error) wx.showToast({ title: '获取收入明细失败', icon: 'none' }) } }, // 获取状态文本 getStatusText(status) { switch (status) { case 0: return '申请中' case 1: return '已打款' case 2: return '已作废' default: return '未知状态' } }, // 申请提现 applyWithdraw() { if (parseFloat(this.data.earnings.account_balance) <= 0) { wx.showToast({ title: '暂无可提现金额', icon: 'none' }) return } // TODO: 实现提现功能 }, // 下拉刷新 async onPullDownRefresh() { try { await this.loadEarningsData() await this.loadIncomeList(true) wx.stopPullDownRefresh() } catch (error) { console.error('刷新数据失败:', error) wx.stopPullDownRefresh() } }, // 触底加载更多 onReachBottom() { this.loadIncomeList() } })