start-preview.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. const { museumApi } = require('../../../utils/api.js');
  2. Page({
  3. data: {
  4. selectedTime: '',
  5. selectedDate: null,
  6. step: 1,
  7. morningAvailability: '余票充足',
  8. afternoonAvailability: '余票充足',
  9. morningTime: '10:00-14:00',
  10. afternoonTime: '14:00-18:00',
  11. morningId: null,
  12. afternoonId: null
  13. },
  14. onLoad(options) {
  15. // 页面加载时的逻辑
  16. },
  17. onShow() {
  18. // 页面显示时的逻辑
  19. },
  20. // 日期选择回调
  21. onDateChange(e) {
  22. console.log('选择的日期:', e.detail);
  23. const selectedDate = e.detail.dateString;
  24. // 只有当日期可选时才设置selectedDate和selectedTime
  25. if (selectedDate) {
  26. this.setData({
  27. selectedDate: selectedDate,
  28. selectedTime: 'morning' // 默认选中上午
  29. });
  30. // 调用API获取当日时段信息
  31. this.getSlotsByDate(selectedDate);
  32. } else {
  33. // 如果日期不可选,清空选择状态
  34. this.setData({
  35. selectedDate: null,
  36. selectedTime: ''
  37. });
  38. }
  39. },
  40. // 格式化日期
  41. formatDate(date) {
  42. const year = date.getFullYear();
  43. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  44. const day = date.getDate().toString().padStart(2, '0');
  45. return `${year}-${month}-${day}`;
  46. },
  47. // 调用API获取指定日期的时段信息
  48. getSlotsByDate(dateString) {
  49. console.log('调用API获取时段信息,日期:', dateString);
  50. museumApi.getSlotsByDate(dateString)
  51. .then(res => {
  52. console.log('API返回数据:', res);
  53. if (res) {
  54. this.updateAvailability(res);
  55. } else {
  56. // API调用失败,使用默认值
  57. this.setData({
  58. morningAvailability: '余票充足',
  59. afternoonAvailability: '余票充足'
  60. });
  61. }
  62. })
  63. .catch(error => {
  64. console.error('API调用失败:', error);
  65. // 使用默认值
  66. this.setData({
  67. morningAvailability: '余票充足',
  68. afternoonAvailability: '余票充足'
  69. });
  70. });
  71. },
  72. // 更新余票显示
  73. updateAvailability(data) {
  74. // 根据API返回的数据更新余票显示
  75. // 只取前两项数据,第一项为上午,第二项为下午
  76. let morningText = '余票充足';
  77. let afternoonText = '余票充足';
  78. let morningTime = '10:00-14:00';
  79. let afternoonTime = '14:00-18:00';
  80. let morningId = null;
  81. let afternoonId = null;
  82. console.log('更新余票显示,数据:', data);
  83. if (data && Array.isArray(data) && data.length >= 2) {
  84. // 第一项为上午
  85. const morningData = data[0];
  86. morningText = morningData.pcs > 0 ? `余票${morningData.pcs}张` : '已约满';
  87. morningTime = morningData.time;
  88. morningId = morningData.id;
  89. // 第二项为下午
  90. const afternoonData = data[1];
  91. afternoonText = afternoonData.pcs > 0 ? `余票${afternoonData.pcs}张` : '已约满';
  92. afternoonTime = afternoonData.time;
  93. afternoonId = afternoonData.id;
  94. }
  95. this.setData({
  96. morningAvailability: morningText,
  97. afternoonAvailability: afternoonText,
  98. morningTime: morningTime,
  99. afternoonTime: afternoonTime,
  100. morningId: morningId,
  101. afternoonId: afternoonId
  102. });
  103. },
  104. // 选择时间段
  105. selectTime(e) {
  106. const time = e.currentTarget.dataset.time;
  107. this.setData({
  108. selectedTime: time
  109. });
  110. console.log('选择的时间段:', time);
  111. },
  112. // 下一步
  113. goNext() {
  114. if (this.data.selectedTime && this.data.selectedDate) {
  115. console.log('选择的时间段:', this.data.selectedTime);
  116. console.log('选择的日期:', this.data.selectedDate);
  117. // 根据选择的时间段获取对应的id和时间
  118. let appointmentSlotsId = null;
  119. let actualTime = '';
  120. if (this.data.selectedTime === 'morning') {
  121. appointmentSlotsId = this.data.morningId;
  122. actualTime = this.data.morningTime;
  123. } else if (this.data.selectedTime === 'afternoon') {
  124. appointmentSlotsId = this.data.afternoonId;
  125. actualTime = this.data.afternoonTime;
  126. }
  127. wx.navigateTo({
  128. url: `/pages/index/visit-people/visit-people?date=${this.data.selectedDate}&time=${actualTime}&appointmentSlotsId=${appointmentSlotsId}&type=1`
  129. });
  130. } else {
  131. wx.showToast({
  132. title: '请选择日期和时间段',
  133. icon: 'none'
  134. });
  135. }
  136. },
  137. onShareAppMessage() {
  138. return {
  139. title: '克拉玛依博物馆 - 开始预约',
  140. path: '/pages/index/start-preview/start-preview'
  141. };
  142. }
  143. });