123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- // 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'
- });
- });
- }
- })
|