// subpages/space/list/list.js import { spacePage } from "../../../api/index"; Page({ /** * 页面的初始数据 */ data: { list: [], pageNum: 1, pageSize: 10, total: 0, loading: false, hasMore: true, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) {}, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ onShow() { this.getList(true); // 页面显示时刷新数据 }, /** * 生命周期函数--监听页面隐藏 */ onHide() {}, /** * 生命周期函数--监听页面卸载 */ onUnload() {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { this.getList(true); // 下拉刷新 }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { this.loadMore(); // 上拉加载更多 }, /** * 用户点击右上角分享 */ onShareAppMessage() {}, toReserve(e) { wx.navigateTo({ url: `/subpages/space/reserve/reserve?id=${e.currentTarget.dataset.item.id}&roomName=${e.currentTarget.dataset.item.name}&openDate=${e.currentTarget.dataset.item.appointmentWeekText}&openTimeText=${e.currentTarget.dataset.item.appointmentTimeText}&imgUrls=${e.currentTarget.dataset.item.imgUrls[0]}&address=${e.currentTarget.dataset.item.address}`, }); }, getList(isRefresh = false) { // 如果正在加载中,直接返回 if (this.data.loading) return; // 如果是刷新,重置分页参数 if (isRefresh) { this.setData({ pageNum: 1, list: [], hasMore: true, }); } // 如果没有更多数据,直接返回 if (!this.data.hasMore && !isRefresh) { wx.showToast({ icon: "none", title: "没有更多了", }); return; } this.setData({ loading: true }); let parm = { pageSize: this.data.pageSize, pageNum: this.data.pageNum, }; spacePage(parm) .then((res) => { if (res.code === 200) { const newData = res.data.records.map((item) => { return { ...item, // 处理可预约周几的显示 appointmentWeekText: this.formatAppointmentWeek( item.appointmentWeek ), // 处理预约时间段的显示 appointmentTimeText: this.formatAppointmentTime( item.appointmentTimeAm, item.appointmentTimePm ), }; }); const currentList = isRefresh ? [] : this.data.list; const updatedList = currentList.concat(newData); // 判断是否还有更多数据 const hasMore = newData.length === this.data.pageSize && updatedList.length < res.data.total; this.setData({ list: updatedList, total: res.data.total, hasMore: hasMore, loading: false, }); // 如果是下拉刷新,停止刷新动画 if (isRefresh) { wx.stopPullDownRefresh(); } } }) .catch((err) => { this.setData({ loading: false }); wx.showToast({ title: err.msg || "加载失败", duration: 2000, icon: "none", }); // 如果是下拉刷新,停止刷新动画 if (isRefresh) { wx.stopPullDownRefresh(); } }); }, scrolltolower() { this.loadMore(); }, loadMore() { if (!this.data.hasMore) { wx.showToast({ icon: "none", title: "没有更多了", }); return; } // 增加页码 this.setData({ pageNum: this.data.pageNum + 1, }); this.getList(); }, // 格式化可预约周几 formatAppointmentWeek(appointmentWeek) { if (!appointmentWeek || !Array.isArray(appointmentWeek)) { return ""; } // 将中文周几转换为显示格式 const weekMap = { 一: "周一", 二: "周二", 三: "周三", 四: "周四", 五: "周五", 六: "周六", 日: "周日", }; return appointmentWeek.map((week) => weekMap[week] || week).join(","); }, // 格式化预约时间段 formatAppointmentTime(appointmentTimeAm, appointmentTimePm) { const timeSlots = []; if (appointmentTimeAm) { timeSlots.push(`上午 ${appointmentTimeAm}`); } if (appointmentTimePm) { timeSlots.push(`下午 ${appointmentTimePm}`); } return timeSlots.join(" "); }, });