微信小店联盟带货小程序

index.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const DarkModeUtil = require('../../utils/dark-mode.js')
  2. import { request } from '../../utils/request'
  3. import { getLoginInfo } from '../../utils/login'
  4. Page({
  5. data: {
  6. isDarkMode: false,
  7. loading: true,
  8. earnings: {
  9. account_balance: '0.00', // 账户余额
  10. this_day_forecast_income: '0.00', // 今日预估收入
  11. last_day_forecast_income: '0.00', // 昨日预估收入
  12. this_month_forecast_income: '0.00', // 本月预估收入
  13. last_month_forecast_income: '0.00', // 上月预估收入
  14. total_forecast_income: '0.00', // 总预估收入
  15. this_month_settlement_income: '0.00', // 本月结算收入
  16. all_settlement_income: '0.00', // 累计结算收入
  17. wait_settlement_income: '0.00', // 待结算收入
  18. withdraw_amount: '0.00' // 已提现金额
  19. },
  20. trends: {
  21. daily: 0, // 日收入趋势:1上升,-1下降,0持平
  22. monthly: 0 // 月收入趋势:1上升,-1下降,0持平
  23. },
  24. incomeList: [],
  25. page: 1,
  26. pageSize: 10,
  27. hasMore: true
  28. },
  29. onLoad() {
  30. // 应用深色模式
  31. DarkModeUtil.applyPageDarkMode(this)
  32. // 监听深色模式变化
  33. DarkModeUtil.listenDarkModeChange(this)
  34. // 加载收益数据
  35. this.loadEarningsData()
  36. // 加载收入明细列表
  37. this.loadIncomeList()
  38. },
  39. // 计算趋势
  40. calculateTrend(current, previous) {
  41. const currentValue = parseFloat(current) || 0
  42. const previousValue = parseFloat(previous) || 0
  43. if (currentValue > previousValue) return 1
  44. if (currentValue < previousValue) return -1
  45. return 0
  46. },
  47. // 加载收益数据
  48. async loadEarningsData() {
  49. try {
  50. const userInfo = getLoginInfo()
  51. const res = await request({
  52. url: '/api/user/myEarnings',
  53. method: 'GET',
  54. data: {
  55. user_id: userInfo.id
  56. }
  57. })
  58. if (res.errno === '0' && res.rst) {
  59. // 计算日收入和月收入趋势
  60. const dailyTrend = this.calculateTrend(
  61. res.rst.this_day_forecast_income,
  62. res.rst.last_day_forecast_income
  63. )
  64. const monthlyTrend = this.calculateTrend(
  65. res.rst.this_month_forecast_income,
  66. res.rst.last_month_forecast_income
  67. )
  68. this.setData({
  69. earnings: {
  70. account_balance: res.rst.account_balance || '0.00',
  71. this_day_forecast_income: res.rst.this_day_forecast_income || '0.00',
  72. last_day_forecast_income: res.rst.last_day_forecast_income || '0.00',
  73. this_month_forecast_income: res.rst.this_month_forecast_income || '0.00',
  74. last_month_forecast_income: res.rst.last_month_forecast_income || '0.00',
  75. total_forecast_income: res.rst.total_forecast_income || '0.00',
  76. this_month_settlement_income: res.rst.this_month_settlement_income || '0.00',
  77. all_settlement_income: res.rst.all_settlement_income || '0.00',
  78. wait_settlement_income: res.rst.wait_settlement_income || '0.00',
  79. withdraw_amount: res.rst.withdraw_amount || '0.00'
  80. },
  81. trends: {
  82. daily: dailyTrend,
  83. monthly: monthlyTrend
  84. },
  85. loading: false
  86. })
  87. // 保存原始数据到本地存储
  88. wx.setStorageSync('earnings_data', res.rst)
  89. } else {
  90. console.error('获取收益数据失败:', res)
  91. wx.showToast({
  92. title: res.err || '获取收益数据失败',
  93. icon: 'none'
  94. })
  95. }
  96. } catch (error) {
  97. console.error('获取收益数据失败:', error)
  98. wx.showToast({
  99. title: '获取收益数据失败',
  100. icon: 'none'
  101. })
  102. }
  103. },
  104. // 加载收入明细
  105. async loadIncomeList(refresh = false) {
  106. if (refresh) {
  107. this.setData({
  108. page: 1,
  109. hasMore: true,
  110. incomeList: []
  111. })
  112. }
  113. if (!this.data.hasMore) return
  114. try {
  115. const userInfo = getLoginInfo()
  116. const res = await request({
  117. url: '/api/user/myEarningsDetail',
  118. method: 'GET',
  119. data: {
  120. user_id: userInfo.id,
  121. page: this.data.page,
  122. page_size: this.data.pageSize
  123. }
  124. })
  125. if (res.errno === '0' && res.rst && res.rst.data) {
  126. const newList = res.rst.data.map(item => ({
  127. id: item.id,
  128. type: item.type === 1 ? '佣金收入' : '提现',
  129. amount: item.rebate || '0.00',
  130. status: this.getStatusText(item.state_of_embodiment),
  131. createTime: item.create_at,
  132. note: item.note || ''
  133. }))
  134. this.setData({
  135. incomeList: [...this.data.incomeList, ...newList],
  136. page: this.data.page + 1,
  137. hasMore: newList.length >= this.data.pageSize
  138. })
  139. } else {
  140. console.error('获取收入明细失败:', res)
  141. wx.showToast({
  142. title: res.err || '获取收入明细失败',
  143. icon: 'none'
  144. })
  145. }
  146. } catch (error) {
  147. console.error('获取收入明细失败:', error)
  148. wx.showToast({
  149. title: '获取收入明细失败',
  150. icon: 'none'
  151. })
  152. }
  153. },
  154. // 获取状态文本
  155. getStatusText(status) {
  156. switch (status) {
  157. case 0: return '申请中'
  158. case 1: return '已打款'
  159. case 2: return '已作废'
  160. default: return '未知状态'
  161. }
  162. },
  163. // 申请提现
  164. applyWithdraw() {
  165. if (parseFloat(this.data.earnings.account_balance) <= 0) {
  166. wx.showToast({
  167. title: '暂无可提现金额',
  168. icon: 'none'
  169. })
  170. return
  171. }
  172. // TODO: 实现提现功能
  173. },
  174. // 下拉刷新
  175. async onPullDownRefresh() {
  176. try {
  177. await this.loadEarningsData()
  178. await this.loadIncomeList(true)
  179. wx.stopPullDownRefresh()
  180. } catch (error) {
  181. console.error('刷新数据失败:', error)
  182. wx.stopPullDownRefresh()
  183. }
  184. },
  185. // 触底加载更多
  186. onReachBottom() {
  187. this.loadIncomeList()
  188. }
  189. })