index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // pages/user/my-preview/index.js
  2. const { museumApi } = require('../../../utils/api.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. reservations: [],
  9. pageNum: 1,
  10. pageSize: 10,
  11. hasMore: true,
  12. loading: false,
  13. type: 0 // 预约类型:0-全部,1-参观,2-活动
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad(options) {
  19. // 重置数据并加载第一页
  20. this.setData({
  21. reservations: [],
  22. pageNum: 1,
  23. hasMore: true
  24. });
  25. this.loadReservations();
  26. },
  27. /**
  28. * 生命周期函数--监听页面初次渲染完成
  29. */
  30. onReady() {
  31. },
  32. /**
  33. * 生命周期函数--监听页面显示
  34. */
  35. onShow() {
  36. },
  37. /**
  38. * 生命周期函数--监听页面隐藏
  39. */
  40. onHide() {
  41. },
  42. /**
  43. * 生命周期函数--监听页面卸载
  44. */
  45. onUnload() {
  46. },
  47. /**
  48. * 页面相关事件处理函数--监听用户下拉动作
  49. */
  50. onPullDownRefresh() {
  51. // 重置数据并重新加载
  52. this.setData({
  53. reservations: [],
  54. pageNum: 1,
  55. hasMore: true
  56. });
  57. this.loadReservations().finally(() => {
  58. wx.stopPullDownRefresh();
  59. });
  60. },
  61. /**
  62. * 页面上拉触底事件的处理函数
  63. */
  64. onReachBottom() {
  65. this.loadMoreReservations();
  66. },
  67. /**
  68. * 用户点击右上角分享
  69. */
  70. onShareAppMessage() {
  71. },
  72. /**
  73. * 加载预约数据
  74. */
  75. loadReservations() {
  76. if (this.data.loading) return Promise.resolve();
  77. this.setData({ loading: true });
  78. return museumApi.getMyReservations({
  79. pageNum: this.data.pageNum,
  80. pageSize: this.data.pageSize,
  81. type: this.data.type
  82. }).then(res => {
  83. console.log('预约列表数据:', res);
  84. if (res) {
  85. const newReservations = this.formatReservationData(res.records || []);
  86. const allReservations = this.data.pageNum === 1 ? newReservations : [...this.data.reservations, ...newReservations];
  87. this.setData({
  88. reservations: allReservations,
  89. hasMore: res.records && res.records.length === this.data.pageSize,
  90. loading: false
  91. });
  92. } else {
  93. wx.showToast({
  94. title: res.message || '获取预约数据失败',
  95. icon: 'none'
  96. });
  97. this.setData({ loading: false });
  98. }
  99. }).catch(error => {
  100. console.error('获取预约数据失败:', error);
  101. // wx.showToast({
  102. // title: '网络错误,请重试',
  103. // icon: 'none'
  104. // });
  105. this.setData({ loading: false });
  106. });
  107. },
  108. /**
  109. * 加载更多预约数据
  110. */
  111. loadMoreReservations() {
  112. if (!this.data.hasMore || this.data.loading) return;
  113. this.setData({
  114. pageNum: this.data.pageNum + 1
  115. });
  116. this.loadReservations();
  117. },
  118. /**
  119. * 格式化预约数据
  120. */
  121. formatReservationData(records) {
  122. const currentDate = new Date();
  123. currentDate.setHours(0, 0, 0, 0); // 设置为当天0点,便于日期比较
  124. return records.map(item => {
  125. const appointmentDate = new Date(item.appointmentTime);
  126. appointmentDate.setHours(0, 0, 0, 0);
  127. const isExpired = appointmentDate < currentDate;
  128. return {
  129. id: item.id,
  130. type: item.type === 1 ? 'normal' : 'activity', // 1-参观,2-活动
  131. date: this.formatDate(item.appointmentTime),
  132. time: item.time || '',
  133. activityName: item.activityTitle || '',
  134. status: item.status,
  135. visitors: item.visitors || [],
  136. isExpired: isExpired
  137. };
  138. });
  139. },
  140. /**
  141. * 格式化日期
  142. */
  143. formatDate(dateString) {
  144. if (!dateString) return '';
  145. const date = new Date(dateString);
  146. const year = date.getFullYear();
  147. const month = String(date.getMonth() + 1).padStart(2, '0');
  148. const day = String(date.getDate()).padStart(2, '0');
  149. return `${year}.${month}.${day}`;
  150. },
  151. /**
  152. * 取消预约
  153. */
  154. cancelReservation(e) {
  155. const reservationId = e.currentTarget.dataset.id;
  156. wx.showModal({
  157. title: '确认取消',
  158. content: '确定要取消这个预约吗?',
  159. success: (res) => {
  160. if (res.confirm) {
  161. // 这里调用取消预约的API
  162. this.performCancelReservation(reservationId);
  163. }
  164. }
  165. });
  166. },
  167. /**
  168. * 执行取消预约操作
  169. */
  170. performCancelReservation(reservationId) {
  171. wx.showLoading({
  172. title: '取消中...'
  173. });
  174. museumApi.cancelReservation(reservationId).then(res => {
  175. wx.hideLoading();
  176. console.log('取消预约结果:', res);
  177. if (res) {
  178. wx.showToast({
  179. title: '取消成功',
  180. icon: 'success'
  181. });
  182. // 重新获取列表数据
  183. this.setData({
  184. reservations: [],
  185. pageNum: 1,
  186. hasMore: true
  187. });
  188. this.loadReservations();
  189. } else {
  190. wx.showToast({
  191. title: res.message || '取消失败',
  192. icon: 'none'
  193. });
  194. }
  195. }).catch(error => {
  196. wx.hideLoading();
  197. console.error('取消预约失败:', error);
  198. wx.showToast({
  199. title: '网络错误,请重试',
  200. icon: 'none'
  201. });
  202. });
  203. }
  204. })