start-preview.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. afternoonDisabled: false,
  14. morningDisabled: false,
  15. showMorning: true,
  16. showAfternoon: true,
  17. isNextButtonDisabled: false
  18. },
  19. onLoad(options) {
  20. // 页面加载时的逻辑
  21. },
  22. onShow() {
  23. // 页面显示时的逻辑
  24. console.log(22222)
  25. },
  26. // 日期选择回调
  27. onDateChange(e) {
  28. console.log('选择的日期:', e.detail);
  29. const selectedDate = e.detail.dateString;
  30. // 只有当日期可选时才设置selectedDate和selectedTime
  31. if (selectedDate) {
  32. this.setData({
  33. selectedDate: selectedDate,
  34. selectedTime: 'morning' // 默认选中上午
  35. });
  36. // 调用API获取当日时段信息
  37. this.getSlotsByDate(selectedDate);
  38. } else {
  39. // 如果日期不可选,清空选择状态
  40. this.setData({
  41. selectedDate: null,
  42. selectedTime: ''
  43. });
  44. }
  45. },
  46. // 格式化日期
  47. formatDate(date) {
  48. const year = date.getFullYear();
  49. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  50. const day = date.getDate().toString().padStart(2, '0');
  51. return `${year}-${month}-${day}`;
  52. },
  53. // 调用API获取指定日期的时段信息
  54. getSlotsByDate(dateString) {
  55. console.log('调用API获取时段信息,日期:', dateString);
  56. museumApi.getSlotsByDate(dateString)
  57. .then(res => {
  58. console.log('API返回数据:', res);
  59. if (res) {
  60. this.updateAvailability(res);
  61. } else {
  62. // API调用失败,使用默认值
  63. this.setData({
  64. morningAvailability: '余票充足',
  65. afternoonAvailability: '余票充足'
  66. });
  67. }
  68. })
  69. .catch(error => {
  70. console.error('API调用失败:', error);
  71. // 使用默认值
  72. this.setData({
  73. morningAvailability: '余票充足',
  74. afternoonAvailability: '余票充足'
  75. });
  76. });
  77. },
  78. // 检查时间段是否已过期
  79. isTimeSlotExpired(timeRange, selectedDate) {
  80. if (!timeRange || !selectedDate) return false;
  81. const now = new Date();
  82. const today = this.formatDate(now);
  83. // 如果选择的不是今天,则不需要检查过期
  84. if (selectedDate !== today) return false;
  85. // 解析时间段,获取结束时间
  86. const timeMatch = timeRange.match(/(\d{1,2}):(\d{2})-(\d{1,2}):(\d{2})/);
  87. if (!timeMatch) return false;
  88. const endHour = parseInt(timeMatch[3]);
  89. const endMinute = parseInt(timeMatch[4]);
  90. // 创建今天的结束时间
  91. const endTime = new Date();
  92. endTime.setHours(endHour, endMinute, 0, 0);
  93. // 如果当前时间已经超过结束时间,则过期
  94. return now > endTime;
  95. },
  96. // 更新余票显示
  97. updateAvailability(data) {
  98. // 根据API返回的数据更新余票显示
  99. let morningText = '余票充足';
  100. let afternoonText = '余票充足';
  101. let morningTime = '10:00-14:00';
  102. let afternoonTime = '14:00-18:00';
  103. let morningId = null;
  104. let afternoonId = null;
  105. let afternoonDisabled = false;
  106. let morningDisabled = false;
  107. let showMorning = true;
  108. let showAfternoon = true;
  109. console.log('更新余票显示,数据:', data);
  110. if (data && Array.isArray(data) && data.length > 0) {
  111. if (data.length >= 2) {
  112. // 有两项数据,根据时间段判断上午下午
  113. let morningData = null;
  114. let afternoonData = null;
  115. // 遍历数据,根据时间判断是上午还是下午
  116. data.forEach(item => {
  117. if (this.isAfternoonTime(item.time)) {
  118. afternoonData = item;
  119. } else {
  120. morningData = item;
  121. }
  122. });
  123. // 处理上午数据
  124. if (morningData) {
  125. if (morningData.pcs <= -1) {
  126. morningText = '闭馆';
  127. morningDisabled = true;
  128. } else if (morningData.pcs > 0) {
  129. morningText = `余票${morningData.pcs}张`;
  130. } else {
  131. morningText = '已约满';
  132. morningDisabled = true;
  133. }
  134. morningTime = morningData.time;
  135. morningId = morningData.id;
  136. } else {
  137. showMorning = false;
  138. morningDisabled = true;
  139. }
  140. // 处理下午数据
  141. if (afternoonData) {
  142. if (afternoonData.pcs <= -1) {
  143. afternoonText = '闭馆';
  144. afternoonDisabled = true;
  145. } else if (afternoonData.pcs > 0) {
  146. afternoonText = `余票${afternoonData.pcs}张`;
  147. } else {
  148. afternoonText = '已约满';
  149. afternoonDisabled = true;
  150. }
  151. afternoonTime = afternoonData.time;
  152. afternoonId = afternoonData.id;
  153. } else {
  154. showAfternoon = false;
  155. afternoonDisabled = true;
  156. }
  157. // 检查时间段是否已过期(只有未被禁用且不是闭馆状态的时间段才检查过期)
  158. if (morningData && !morningDisabled && morningText !== '闭馆') {
  159. const isExpired = this.isTimeSlotExpired(morningTime, this.data.selectedDate);
  160. if (isExpired) {
  161. morningText = '已过期';
  162. morningDisabled = true;
  163. }
  164. }
  165. if (afternoonData && !afternoonDisabled && afternoonText !== '闭馆') {
  166. const isExpired = this.isTimeSlotExpired(afternoonTime, this.data.selectedDate);
  167. if (isExpired) {
  168. afternoonText = '已过期';
  169. afternoonDisabled = true;
  170. }
  171. }
  172. } else {
  173. // 只有一项数据,根据时间段判断显示上午还是下午
  174. const singleData = data[0];
  175. const timeStr = singleData.time;
  176. // 解析时间段,判断是否大于14:00
  177. const isAfternoon = this.isAfternoonTime(timeStr);
  178. if (isAfternoon) {
  179. // 显示下午,隐藏上午
  180. if (singleData.pcs <= -1) {
  181. afternoonText = '闭馆';
  182. afternoonDisabled = true;
  183. } else if (singleData.pcs > 0) {
  184. afternoonText = `余票${singleData.pcs}张`;
  185. } else {
  186. afternoonText = '已约满';
  187. afternoonDisabled = true;
  188. }
  189. afternoonTime = singleData.time;
  190. afternoonId = singleData.id;
  191. if (!afternoonDisabled) {
  192. const isExpired = this.isTimeSlotExpired(afternoonTime, this.data.selectedDate);
  193. if (isExpired) {
  194. afternoonText = '已过期';
  195. afternoonDisabled = true;
  196. }
  197. }
  198. showMorning = false;
  199. morningDisabled = true;
  200. } else {
  201. // 显示上午,隐藏下午
  202. if (singleData.pcs <= -1) {
  203. morningText = '闭馆';
  204. morningDisabled = true;
  205. } else if (singleData.pcs > 0) {
  206. morningText = `余票${singleData.pcs}张`;
  207. } else {
  208. morningText = '已约满';
  209. morningDisabled = true;
  210. }
  211. morningTime = singleData.time;
  212. morningId = singleData.id;
  213. if (!morningDisabled) {
  214. const isExpired = this.isTimeSlotExpired(morningTime, this.data.selectedDate);
  215. if (isExpired) {
  216. morningText = '已过期';
  217. morningDisabled = true;
  218. }
  219. }
  220. showAfternoon = false;
  221. afternoonDisabled = true;
  222. }
  223. }
  224. // 如果当前选中的时间段已过期或被禁用,自动切换到可用的时间段
  225. if (this.data.selectedTime === 'morning' && (morningDisabled || !showMorning)) {
  226. if (!afternoonDisabled && showAfternoon) {
  227. this.setData({ selectedTime: 'afternoon' });
  228. } else {
  229. this.setData({ selectedTime: '' });
  230. }
  231. } else if (this.data.selectedTime === 'afternoon' && (afternoonDisabled || !showAfternoon)) {
  232. if (!morningDisabled && showMorning) {
  233. this.setData({ selectedTime: 'morning' });
  234. } else {
  235. this.setData({ selectedTime: '' });
  236. }
  237. }
  238. }
  239. // 计算下一步按钮是否应该禁用
  240. let isNextButtonDisabled = false;
  241. // 计算有几个时间段显示
  242. const visibleSlots = [];
  243. if (showMorning) visibleSlots.push({ disabled: morningDisabled, name: 'morning' });
  244. if (showAfternoon) visibleSlots.push({ disabled: afternoonDisabled, name: 'afternoon' });
  245. if (visibleSlots.length === 1) {
  246. // 只有一个时间段,如果该时间段被禁用则置灰按钮
  247. isNextButtonDisabled = visibleSlots[0].disabled;
  248. } else if (visibleSlots.length === 2) {
  249. // 有两个时间段,如果两个时间段都被禁用则置灰按钮
  250. isNextButtonDisabled = visibleSlots.every(slot => slot.disabled);
  251. }
  252. this.setData({
  253. morningAvailability: morningText,
  254. afternoonAvailability: afternoonText,
  255. morningTime: morningTime,
  256. afternoonTime: afternoonTime,
  257. morningId: morningId,
  258. afternoonId: afternoonId,
  259. afternoonDisabled: afternoonDisabled,
  260. morningDisabled: morningDisabled,
  261. showMorning: showMorning,
  262. showAfternoon: showAfternoon,
  263. isNextButtonDisabled: isNextButtonDisabled
  264. });
  265. },
  266. // 判断时间段是否为下午(大于14:00)
  267. isAfternoonTime(timeStr) {
  268. // 时间格式如:"10:00-14:00" 或 "14:00-18:00"
  269. const startTime = timeStr.split('-')[0].trim();
  270. const hour = parseInt(startTime.split(':')[0]);
  271. return hour >= 14;
  272. },
  273. // 选择时间段
  274. selectTime(e) {
  275. const time = e.currentTarget.dataset.time;
  276. // 如果点击的是上午且上午被禁用,则不允许选择
  277. if (time === 'morning' && this.data.morningDisabled) {
  278. return;
  279. }
  280. // 如果点击的是下午且下午被禁用,则不允许选择
  281. if (time === 'afternoon' && this.data.afternoonDisabled) {
  282. return;
  283. }
  284. this.setData({
  285. selectedTime: time
  286. });
  287. console.log('选择的时间段:', time);
  288. },
  289. // 下一步
  290. goNext() {
  291. // 如果按钮被禁用,不允许进入下一步
  292. if (this.data.isNextButtonDisabled) {
  293. // wx.showToast({
  294. // title: '当前时间段不可预约',
  295. // icon: 'none'
  296. // });
  297. return;
  298. }
  299. if (this.data.selectedTime && this.data.selectedDate) {
  300. console.log('选择的时间段:', this.data.selectedTime);
  301. console.log('选择的日期:', this.data.selectedDate);
  302. // 根据选择的时间段获取对应的id和时间
  303. let appointmentSlotsId = null;
  304. let actualTime = '';
  305. if (this.data.selectedTime === 'morning') {
  306. appointmentSlotsId = this.data.morningId;
  307. actualTime = this.data.morningTime;
  308. } else if (this.data.selectedTime === 'afternoon') {
  309. appointmentSlotsId = this.data.afternoonId;
  310. actualTime = this.data.afternoonTime;
  311. }
  312. wx.navigateTo({
  313. url: `/pages/index/visit-people/visit-people?date=${this.data.selectedDate}&time=${actualTime}&appointmentSlotsId=${appointmentSlotsId}&type=1`
  314. });
  315. } else {
  316. // wx.showToast({
  317. // title: '请选择日期和时间段',
  318. // icon: 'none'
  319. // });
  320. }
  321. },
  322. onShareAppMessage() {
  323. return {
  324. title: '克拉玛依博物馆 - 开始预约',
  325. path: '/pages/index/start-preview/start-preview'
  326. };
  327. }
  328. });