// pages/user/my-preview/index.js const { museumApi } = require('../../../utils/api.js'); Page({ /** * 页面的初始数据 */ data: { reservations: [], pageNum: 1, pageSize: 10, hasMore: true, loading: false, type: 0 // 预约类型:0-全部,1-参观,2-活动 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { // 重置数据并加载第一页 this.setData({ reservations: [], pageNum: 1, hasMore: true }); this.loadReservations(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { // 重置数据并重新加载 this.setData({ reservations: [], pageNum: 1, hasMore: true }); this.loadReservations().finally(() => { wx.stopPullDownRefresh(); }); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { this.loadMoreReservations(); }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, /** * 加载预约数据 */ loadReservations() { if (this.data.loading) return Promise.resolve(); this.setData({ loading: true }); return museumApi.getMyReservations({ pageNum: this.data.pageNum, pageSize: this.data.pageSize, type: this.data.type }).then(res => { console.log('预约列表数据:', res); if (res) { const newReservations = this.formatReservationData(res.records || []); const allReservations = this.data.pageNum === 1 ? newReservations : [...this.data.reservations, ...newReservations]; this.setData({ reservations: allReservations, hasMore: res.records && res.records.length === this.data.pageSize, loading: false }); } else { wx.showToast({ title: res.message || '获取预约数据失败', icon: 'none' }); this.setData({ loading: false }); } }).catch(error => { console.error('获取预约数据失败:', error); // wx.showToast({ // title: '网络错误,请重试', // icon: 'none' // }); this.setData({ loading: false }); }); }, /** * 加载更多预约数据 */ loadMoreReservations() { if (!this.data.hasMore || this.data.loading) return; this.setData({ pageNum: this.data.pageNum + 1 }); this.loadReservations(); }, /** * 格式化预约数据 */ formatReservationData(records) { const currentDate = new Date(); currentDate.setHours(0, 0, 0, 0); // 设置为当天0点,便于日期比较 return records.map(item => { const appointmentDate = new Date(item.appointmentTime); appointmentDate.setHours(0, 0, 0, 0); const isExpired = appointmentDate < currentDate; return { id: item.id, type: item.type === 1 ? 'normal' : 'activity', // 1-参观,2-活动 date: this.formatDate(item.appointmentTime), time: item.time || '', activityName: item.activityTitle || '', status: item.status, visitors: item.visitors || [], isExpired: isExpired }; }); }, /** * 格式化日期 */ formatDate(dateString) { if (!dateString) return ''; const date = new Date(dateString); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}.${month}.${day}`; }, /** * 取消预约 */ cancelReservation(e) { const reservationId = e.currentTarget.dataset.id; wx.showModal({ title: '确认取消', content: '确定要取消这个预约吗?', success: (res) => { if (res.confirm) { // 这里调用取消预约的API this.performCancelReservation(reservationId); } } }); }, /** * 执行取消预约操作 */ performCancelReservation(reservationId) { wx.showLoading({ title: '取消中...' }); museumApi.cancelReservation(reservationId).then(res => { wx.hideLoading(); console.log('取消预约结果:', res); if (res) { wx.showToast({ title: '取消成功', icon: 'success' }); // 重新获取列表数据 this.setData({ reservations: [], pageNum: 1, hasMore: true }); this.loadReservations(); } else { wx.showToast({ title: res.message || '取消失败', icon: 'none' }); } }).catch(error => { wx.hideLoading(); console.error('取消预约失败:', error); wx.showToast({ title: '网络错误,请重试', icon: 'none' }); }); } })