与牧同行-兽医端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.3 KiB

  1. import http from '../../../utils/api'
  2. const baseUrl = require('../../../utils/baseUrl')
  3. Page({
  4. data: {
  5. baseUrl: baseUrl,
  6. user: {},
  7. // 聊天申请列表数据
  8. applyList: [],
  9. // 分页相关
  10. page: 1,
  11. pageSize: 10,
  12. total: 0,
  13. hasMore: true, // 是否还有更多数据
  14. isLoading: false, // 是否正在加载
  15. isFirstLoad: true, // 是否首次加载
  16. },
  17. onLoad() {
  18. this.getzj()
  19. this.getsessions(true) // true 表示首次加载(重置列表)
  20. },
  21. // 个人专家信息
  22. getzj() {
  23. const user = wx.getStorageSync('userInfo')
  24. console.log(222, user)
  25. this.setData({
  26. user: user
  27. })
  28. },
  29. // 咨询列表 - 支持分页加载
  30. getsessions(isRefresh = false) {
  31. // 防止重复请求
  32. if (this.data.isLoading) return
  33. // 如果是加载更多但已无更多数据,直接返回
  34. if (!isRefresh && !this.data.hasMore) return
  35. this.setData({ isLoading: true })
  36. // 准备请求参数
  37. const params = {
  38. page: isRefresh ? 1 : this.data.page,
  39. pageSize: this.data.pageSize
  40. }
  41. http.sessions({
  42. data: params,
  43. success: (res) => {
  44. console.log('接口返回数据:', res)
  45. let newList = []
  46. let total = 0
  47. if (Array.isArray(res)) {
  48. // 如果直接返回数组
  49. newList = res
  50. total = res.length
  51. } else if (res.data && Array.isArray(res.data)) {
  52. // 如果返回 { data: [], total: 100 }
  53. newList = res.data
  54. total = res.total || res.data.length
  55. } else if (res.list && Array.isArray(res.list)) {
  56. // 如果返回 { list: [], total: 100 }
  57. newList = res.list
  58. total = res.total || res.list.length
  59. } else {
  60. console.error('接口返回格式不正确', res)
  61. newList = []
  62. total = 0
  63. }
  64. // 计算是否有更多数据
  65. const currentPage = isRefresh ? 1 : this.data.page
  66. const pageSize = this.data.pageSize
  67. const hasMore = currentPage * pageSize < total
  68. this.setData({
  69. applyList: isRefresh ? newList : [...this.data.applyList, ...newList],
  70. total: total,
  71. page: isRefresh ? 2 : this.data.page + 1, // 下次请求的页码
  72. hasMore: hasMore,
  73. isLoading: false,
  74. isFirstLoad: false
  75. })
  76. },
  77. fail: (err) => {
  78. console.error('加载失败:', err)
  79. wx.showToast({
  80. title: '加载失败',
  81. icon: 'none'
  82. })
  83. this.setData({
  84. isLoading: false,
  85. isFirstLoad: false
  86. })
  87. }
  88. })
  89. },
  90. // 加载更多(上拉触底触发)
  91. loadMore() {
  92. // 如果有更多数据且不在加载中,则加载下一页
  93. if (this.data.hasMore && !this.data.isLoading) {
  94. this.getsessions(false)
  95. }
  96. },
  97. // 处理点击申请项
  98. handleApply(e) {
  99. console.log(111, e)
  100. const id = e.currentTarget.dataset.id
  101. wx.navigateTo({
  102. url: `/pagesA/pages/expertChat/expertChat?id=${id}`,
  103. })
  104. },
  105. // 可选:下拉刷新功能
  106. onPullDownRefresh() {
  107. this.setData({
  108. page: 1,
  109. hasMore: true
  110. })
  111. this.getsessions(true)
  112. // 停止下拉刷新
  113. wx.stopPullDownRefresh()
  114. }
  115. })