// active-page.js const { museumApi } = require('../../../utils/api.js'); Page({ data: { selectedTime: '', selectedDate: null, step: 1, activityId: 0, // 活动ID type: 1, // 预约类型:1-普通预约,2-活动预约 activityData: {}, isTimeExpired: false, // 时间段是否已过期 currentTicketCount: 0, // 当前选择日期的票数 currentActivityTime: '' // 当前选择日期的活动时间 }, onLoad(options) { // 页面加载时的逻辑 console.log('active-page接收到的参数:', options); if (options.activityId) { this.setData({ activityId: parseInt(options.activityId) }); } if (options.type) { this.setData({ type: parseInt(options.type) }); } }, onReady() { // 页面初次渲染完成后获取活动详情 if (this.data.activityId) { this.getActivityDetail(this.data.activityId); } }, onShow() { // 页面显示时的逻辑 }, // 日期选择回调 onDateChange(e) { console.log('选择的日期:', e.detail); const selectedDate = e.detail.dateString; // 只有当日期可选时才设置selectedDate if (selectedDate) { this.setData({ selectedDate: selectedDate, selectedTime: '', // 清空时间选择状态 }); // 获取该日期的活动票数和时间信息 this.getActivityTicketInfo(selectedDate); } else { // 如果日期不可选,清空选择状态 this.setData({ selectedDate: null, selectedTime: '', isTimeExpired: false }); } }, // 格式化日期 formatDate(date) { const year = date.getFullYear(); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const day = date.getDate().toString().padStart(2, '0'); return `${year}-${month}-${day}`; }, // 检查活动时间是否已过期 checkActivityTimeExpired(activityTime, selectedDate) { if (!activityTime || !selectedDate) return false; const now = new Date(); const today = this.formatDate(now); // 如果选择的不是今天,则不需要检查过期 if (selectedDate !== today) return false; // 解析活动时间段,获取结束时间 const timeMatch = activityTime.match(/(\d{1,2}):(\d{2})-(\d{1,2}):(\d{2})/); if (!timeMatch) return false; const endHour = parseInt(timeMatch[3]); const endMinute = parseInt(timeMatch[4]); // 创建今天的结束时间 const endTime = new Date(); endTime.setHours(endHour, endMinute, 0, 0); // 如果当前时间已经超过结束时间,则过期 return now > endTime; }, // 选择时间段 selectTime(e) { // 如果时间已过期,则不允许选择 if (this.data.isTimeExpired) { wx.showToast({ title: '已超时可预约时间段', icon: 'none' }); return; } const timeToUse = this.data.currentActivityTime || this.data.activityData.time; if (this.data.selectedDate && timeToUse) { this.setData({ selectedTime: 'selected' }); console.log('选择的时间段:', timeToUse); } else { wx.showToast({ title: '请先选择日期', icon: 'none' }); } }, // 下一步 goNext() { if (!this.data.selectedDate) { wx.showToast({ title: '请选择日期', icon: 'none' }); return; } // 检查时间是否已过期 if (this.data.isTimeExpired) { wx.showToast({ title: '当前时间不在可预约时间段内', icon: 'none' }); return; } if(this.data.currentTicketCount <= 0){ return; } if (this.data.selectedDate && this.data.activityData.title) { console.log('选择的日期:', this.data.selectedDate); console.log('活动标题:', this.data.activityData.title); // 传递活动标题而不是appointmentSlotsId const title = this.data.activityData.title; wx.navigateTo({ url: `/pages/index/active-people/active-people?date=${this.data.selectedDate}&title=${encodeURIComponent(title)}&type=2&activityId=${this.data.activityId}` }); } else { wx.showToast({ title: '请选择日期', icon: 'none' }); } }, onShareAppMessage() { return { title: '克拉玛依博物馆 - 开始预约', path: '/pages/index/start-preview/start-preview' }; }, // 获取活动详情 getActivityDetail(activityId) { museumApi.getActivityDetail(activityId) .then(response => { console.log('获取活动详情成功:', response); if (response) { const activityData = response; console.log('活动数据:', activityData); // 将活动数据设置到页面data中 this.setData({ activityData: activityData }); // 如果已经选择了日期,获取该日期的票数和时间信息 if (this.data.selectedDate) { this.getActivityTicketInfo(this.data.selectedDate); } } else { console.log('API响应数据为空:', response); } }) .catch(error => { console.error('获取活动详情失败:', error); }); }, // 获取活动票数和时间信息 getActivityTicketInfo(date) { if (!this.data.activityId || !date) { return; } const params = { activityId: this.data.activityId, date: date }; console.log('获取活动票数信息,参数:', params); museumApi.getActivityticket(params) .then(response => { console.log('获取活动票数成功:', response); if (response) { // 检查活动时间是否已过期 const isExpired = this.checkActivityTimeExpired(response.time, date); // 更新当前选择日期的票数和时间信息 this.setData({ currentActivityTime: response.time, currentTicketCount: response.personCount, isTimeExpired: isExpired }); } else { console.log('获取活动票数API响应数据为空:', response); } }) .catch(error => { console.error('获取活动票数失败:', error); wx.showToast({ title: '获取票数信息失败', icon: 'none' }); }); } });