active-page.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // active-page.js
  2. const { museumApi } = require('../../../utils/api.js');
  3. Page({
  4. data: {
  5. selectedTime: '',
  6. selectedDate: null,
  7. step: 1,
  8. activityId: 0, // 活动ID
  9. type: 1, // 预约类型:1-普通预约,2-活动预约
  10. activityData: {},
  11. isTimeExpired: false, // 时间段是否已过期
  12. currentTicketCount: 0, // 当前选择日期的票数
  13. currentActivityTime: '' // 当前选择日期的活动时间
  14. },
  15. onLoad(options) {
  16. // 页面加载时的逻辑
  17. console.log('active-page接收到的参数:', options);
  18. if (options.activityId) {
  19. this.setData({
  20. activityId: parseInt(options.activityId)
  21. });
  22. }
  23. if (options.type) {
  24. this.setData({
  25. type: parseInt(options.type)
  26. });
  27. }
  28. },
  29. onReady() {
  30. // 页面初次渲染完成后获取活动详情
  31. if (this.data.activityId) {
  32. this.getActivityDetail(this.data.activityId);
  33. }
  34. },
  35. onShow() {
  36. // 页面显示时的逻辑
  37. },
  38. // 日期选择回调
  39. onDateChange(e) {
  40. console.log('选择的日期:', e.detail);
  41. const selectedDate = e.detail.dateString;
  42. // 只有当日期可选时才设置selectedDate
  43. if (selectedDate) {
  44. this.setData({
  45. selectedDate: selectedDate,
  46. selectedTime: '', // 清空时间选择状态
  47. });
  48. // 获取该日期的活动票数和时间信息
  49. this.getActivityTicketInfo(selectedDate);
  50. } else {
  51. // 如果日期不可选,清空选择状态
  52. this.setData({
  53. selectedDate: null,
  54. selectedTime: '',
  55. isTimeExpired: false
  56. });
  57. }
  58. },
  59. // 格式化日期
  60. formatDate(date) {
  61. const year = date.getFullYear();
  62. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  63. const day = date.getDate().toString().padStart(2, '0');
  64. return `${year}-${month}-${day}`;
  65. },
  66. // 检查活动时间是否已过期
  67. checkActivityTimeExpired(activityTime, selectedDate) {
  68. if (!activityTime || !selectedDate) return false;
  69. const now = new Date();
  70. const today = this.formatDate(now);
  71. // 如果选择的不是今天,则不需要检查过期
  72. if (selectedDate !== today) return false;
  73. // 解析活动时间段,获取结束时间
  74. const timeMatch = activityTime.match(/(\d{1,2}):(\d{2})-(\d{1,2}):(\d{2})/);
  75. if (!timeMatch) return false;
  76. const endHour = parseInt(timeMatch[3]);
  77. const endMinute = parseInt(timeMatch[4]);
  78. // 创建今天的结束时间
  79. const endTime = new Date();
  80. endTime.setHours(endHour, endMinute, 0, 0);
  81. // 如果当前时间已经超过结束时间,则过期
  82. return now > endTime;
  83. },
  84. // 选择时间段
  85. selectTime(e) {
  86. // 如果时间已过期,则不允许选择
  87. if (this.data.isTimeExpired) {
  88. wx.showToast({
  89. title: '已超时可预约时间段',
  90. icon: 'none'
  91. });
  92. return;
  93. }
  94. const timeToUse = this.data.currentActivityTime || this.data.activityData.time;
  95. if (this.data.selectedDate && timeToUse) {
  96. this.setData({
  97. selectedTime: 'selected'
  98. });
  99. console.log('选择的时间段:', timeToUse);
  100. } else {
  101. wx.showToast({
  102. title: '请先选择日期',
  103. icon: 'none'
  104. });
  105. }
  106. },
  107. // 下一步
  108. goNext() {
  109. if (!this.data.selectedDate) {
  110. wx.showToast({
  111. title: '请选择日期',
  112. icon: 'none'
  113. });
  114. return;
  115. }
  116. // 检查时间是否已过期
  117. if (this.data.isTimeExpired) {
  118. wx.showToast({
  119. title: '当前时间不在可预约时间段内',
  120. icon: 'none'
  121. });
  122. return;
  123. }
  124. if(this.data.currentTicketCount <= 0){
  125. return;
  126. }
  127. if (this.data.selectedDate && this.data.activityData.title) {
  128. console.log('选择的日期:', this.data.selectedDate);
  129. console.log('活动标题:', this.data.activityData.title);
  130. // 传递活动标题而不是appointmentSlotsId
  131. const title = this.data.activityData.title;
  132. wx.navigateTo({
  133. url: `/pages/index/active-people/active-people?date=${this.data.selectedDate}&title=${encodeURIComponent(title)}&type=2&activityId=${this.data.activityId}`
  134. });
  135. } else {
  136. wx.showToast({
  137. title: '请选择日期',
  138. icon: 'none'
  139. });
  140. }
  141. },
  142. onShareAppMessage() {
  143. return {
  144. title: '克拉玛依博物馆 - 开始预约',
  145. path: '/pages/index/start-preview/start-preview'
  146. };
  147. },
  148. // 获取活动详情
  149. getActivityDetail(activityId) {
  150. museumApi.getActivityDetail(activityId)
  151. .then(response => {
  152. console.log('获取活动详情成功:', response);
  153. if (response) {
  154. const activityData = response;
  155. console.log('活动数据:', activityData);
  156. // 将活动数据设置到页面data中
  157. this.setData({
  158. activityData: activityData
  159. });
  160. // 如果已经选择了日期,获取该日期的票数和时间信息
  161. if (this.data.selectedDate) {
  162. this.getActivityTicketInfo(this.data.selectedDate);
  163. }
  164. } else {
  165. console.log('API响应数据为空:', response);
  166. }
  167. })
  168. .catch(error => {
  169. console.error('获取活动详情失败:', error);
  170. });
  171. },
  172. // 获取活动票数和时间信息
  173. getActivityTicketInfo(date) {
  174. if (!this.data.activityId || !date) {
  175. return;
  176. }
  177. const params = {
  178. activityId: this.data.activityId,
  179. date: date
  180. };
  181. console.log('获取活动票数信息,参数:', params);
  182. museumApi.getActivityticket(params)
  183. .then(response => {
  184. console.log('获取活动票数成功:', response);
  185. if (response) {
  186. // 检查活动时间是否已过期
  187. const isExpired = this.checkActivityTimeExpired(response.time, date);
  188. // 更新当前选择日期的票数和时间信息
  189. this.setData({
  190. currentActivityTime: response.time,
  191. currentTicketCount: response.personCount,
  192. isTimeExpired: isExpired
  193. });
  194. } else {
  195. console.log('获取活动票数API响应数据为空:', response);
  196. }
  197. })
  198. .catch(error => {
  199. console.error('获取活动票数失败:', error);
  200. wx.showToast({
  201. title: '获取票数信息失败',
  202. icon: 'none'
  203. });
  204. });
  205. }
  206. });