|
|
import http from '../../../utils/api'const baseUrl = require('../../../utils/baseUrl')
Page({ data: { baseUrl: baseUrl, user: {}, // 聊天申请列表数据
applyList: [], // 分页相关
page: 1, pageSize: 10, total: 0, hasMore: true, // 是否还有更多数据
isLoading: false, // 是否正在加载
isFirstLoad: true, // 是否首次加载
},
onLoad() { this.getzj() this.getsessions(true) // true 表示首次加载(重置列表)
},
// 个人专家信息
getzj() { const user = wx.getStorageSync('userInfo') console.log(222, user) this.setData({ user: user }) },
// 咨询列表 - 支持分页加载
getsessions(isRefresh = false) { // 防止重复请求
if (this.data.isLoading) return // 如果是加载更多但已无更多数据,直接返回
if (!isRefresh && !this.data.hasMore) return
this.setData({ isLoading: true })
// 准备请求参数
const params = { page: isRefresh ? 1 : this.data.page, pageSize: this.data.pageSize }
http.sessions({ data: params, success: (res) => { console.log('接口返回数据:', res) let newList = [] let total = 0 if (Array.isArray(res)) { // 如果直接返回数组
newList = res total = res.length } else if (res.data && Array.isArray(res.data)) { // 如果返回 { data: [], total: 100 }
newList = res.data total = res.total || res.data.length } else if (res.list && Array.isArray(res.list)) { // 如果返回 { list: [], total: 100 }
newList = res.list total = res.total || res.list.length } else { console.error('接口返回格式不正确', res) newList = [] total = 0 }
// 计算是否有更多数据
const currentPage = isRefresh ? 1 : this.data.page const pageSize = this.data.pageSize const hasMore = currentPage * pageSize < total
this.setData({ applyList: isRefresh ? newList : [...this.data.applyList, ...newList], total: total, page: isRefresh ? 2 : this.data.page + 1, // 下次请求的页码
hasMore: hasMore, isLoading: false, isFirstLoad: false }) }, fail: (err) => { console.error('加载失败:', err) wx.showToast({ title: '加载失败', icon: 'none' }) this.setData({ isLoading: false, isFirstLoad: false }) } }) },
// 加载更多(上拉触底触发)
loadMore() { // 如果有更多数据且不在加载中,则加载下一页
if (this.data.hasMore && !this.data.isLoading) { this.getsessions(false) } },
// 处理点击申请项
handleApply(e) { console.log(111, e) const id = e.currentTarget.dataset.id wx.navigateTo({ url: `/pagesA/pages/expertChat/expertChat?id=${id}`, }) },
// 可选:下拉刷新功能
onPullDownRefresh() { this.setData({ page: 1, hasMore: true }) this.getsessions(true) // 停止下拉刷新
wx.stopPullDownRefresh() }})
|