index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // nova-tourism/pages/homestay/homestay-order2/index.js
  2. let Parse = getApp().Parse;
  3. const company = getApp().globalData.company
  4. import dataSource from '../../../service/data';
  5. import dateServ from '../../../service/date';
  6. const rechText = require('../../../../utils/rech-text')
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. //屏幕高度
  13. statusBarHeight: 0, // 状态栏高度
  14. screenHeight: 0, // 屏幕高度
  15. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  16. bottomNavHeight: 0, // 底部导航栏高度
  17. contentHeight: 0, // 可用内容高度
  18. roomList: [],
  19. startTime: null, // 入住开始时间
  20. endTime: null, // 入住结束时间
  21. roomId: null,
  22. count: 1, // 入住几晚
  23. date: '' //入住区间
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. const systemInfo = wx.getSystemInfoSync();
  30. const statusBarHeight = systemInfo.statusBarHeight || 0;
  31. const screenHeight = systemInfo.screenHeight || 0;
  32. const custom = wx.getMenuButtonBoundingClientRect();
  33. const customHeight = custom.height + 10 + 2 || 0;
  34. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  35. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  36. this.setData({
  37. contentHeight
  38. });
  39. const {
  40. objectId,
  41. date_start,
  42. date_end,
  43. } = options;
  44. const date_start1 = decodeURIComponent(date_start);
  45. const date_end1 = decodeURIComponent(date_end);
  46. this.setData({
  47. startTime: date_start1,
  48. endTime: date_end1,
  49. roomId: objectId
  50. })
  51. console.log('123', date_start1, date_end1);
  52. this.getroom()
  53. this.getdate()
  54. },
  55. //计算用户选择的入住和离店日期,并生成相应的日期范围和价格信息。
  56. getdate() {
  57. // let {
  58. // startTime,
  59. // endTime
  60. // } = this.formatTimeArea(this.data.start, this.data.end)
  61. // console.log('开始时间', this.data.start, '结束时间', this.data.end);
  62. // console.log('开始时间', startTime, '结束时间', endTime);
  63. var dataAll = dateServ.getDayAll(this.data.startTime, this.data.endTime);
  64. dataAll.pop(); // 最后一天为退房,不算在内
  65. console.log('dataAll',dataAll);
  66. // console.log(this.formatDate(startTime), this.formatDate(endTime))
  67. let dateStr = this.formatDate(this.data.startTime) + '-' + this.formatDate(this.data.endTime)
  68. console.log('dateStr',dateStr)
  69. this.setData({
  70. // showDate: false,
  71. date: `${this.formatDate(this.data.startTime)} - ${this.formatDate(this.data.endTime)}`,
  72. // priceInfoArr: dataAll,
  73. count: dataAll.length
  74. })
  75. console.log('天数',this.data.count);
  76. },
  77. //修改时间格式
  78. formatDate(date) {
  79. date = new Date(date);
  80. // 获取当前日期
  81. const today = new Date();
  82. // 格式化日期为 "月/日"
  83. const month = date.getMonth() + 1; // 月份从0开始,所以要加1
  84. const day = date.getDate();
  85. // 获取星期几
  86. const weekDays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  87. const weekDay = weekDays[date.getDay()]; // 获取星期几的中文
  88. // 判断是今天还是明天
  89. let dateDescription = '';
  90. if (date.toDateString() === today.toDateString()) {
  91. dateDescription = '今天';
  92. } else if (date.toDateString() === new Date(today.getTime() + 86400000).toDateString()) {
  93. dateDescription = '明天';
  94. } else {
  95. dateDescription = weekDay; // 其他情况返回星期几
  96. }
  97. // 返回格式化的字符串
  98. return `${month}月${day}日 ${dateDescription}`;
  99. },
  100. //获取房间信息
  101. async getroom() {
  102. let room = new Parse.Query('ShopRoom');
  103. room.equalTo('company', company);
  104. room.equalTo('objectId', this.data.roomId);
  105. room.equalTo('isEnabled', 'true');
  106. room.include('shop');
  107. room.notEqualTo('isDeleted', 'true');
  108. let room2 = await room.find();
  109. let roomList = room2.map(item => item.toJSON());
  110. this.setData({
  111. roomList
  112. })
  113. console.log('房间', this.data.roomList);
  114. },
  115. //计算选择日期的房间数量
  116. async checkOrderCount() {
  117. // start1 end1 现在选择
  118. // start2 end2 已有订单时间
  119. // start1 < start2 end1 < end2
  120. // start1 > start2 end1 > end2
  121. // start1 = start2 end1 = end2
  122. let now = dateServ.changeDateTime(new Date(), '14:00:00')
  123. let Order = new Parse.Query("RoomOrder")
  124. Order.equalTo("room", this.data.roomId)
  125. Order.equalTo("company", company)
  126. Order.exists("status")
  127. Order.notEqualTo("status", 100)
  128. Order.notEqualTo("status", 400)
  129. Order.notEqualTo("status", 601)
  130. Order.notEqualTo("status", 700)
  131. Order.greaterThanOrEqualTo("startTime", this.data.endTime) //20 23 21 22
  132. Order.lessThanOrEqualTo("endTime", this.data.startTime)
  133. Order.select("startTime", "endTime")
  134. let count = await Order.count()
  135. console.log(count);
  136. return count
  137. },
  138. //点击支付
  139. async submit() {
  140. if (!this.data.startTime || !this.data.endTime) {
  141. wx.showToast({
  142. title: '请选择入住时间',
  143. icon: 'none',
  144. })
  145. return
  146. }
  147. if (this.data.customers.length == 0) {
  148. wx.showToast({
  149. title: '请选择入住人',
  150. icon: 'none',
  151. })
  152. return
  153. }
  154. let count = await this.checkOrderCount()
  155. if (count >= this.data.room.total) {
  156. console.log(count);
  157. wx.showToast({
  158. title: '当前日期,已无剩余房间,请重新选择',
  159. icon: 'none',
  160. })
  161. return
  162. }
  163. let tradeNo = this.getTradeNo()
  164. let order = await this.setOrder(tradeNo)
  165. if (order) {
  166. this.setData({
  167. activeOrder: order,
  168. tradeNo,
  169. showPayment: true,
  170. })
  171. }
  172. },
  173. /**
  174. * 生命周期函数--监听页面初次渲染完成
  175. */
  176. onReady: function () {
  177. },
  178. /**
  179. * 生命周期函数--监听页面显示
  180. */
  181. onShow: function () {
  182. },
  183. /**
  184. * 生命周期函数--监听页面隐藏
  185. */
  186. onHide: function () {
  187. },
  188. /**
  189. * 生命周期函数--监听页面卸载
  190. */
  191. onUnload: function () {
  192. },
  193. /**
  194. * 页面相关事件处理函数--监听用户下拉动作
  195. */
  196. onPullDownRefresh: function () {
  197. },
  198. /**
  199. * 页面上拉触底事件的处理函数
  200. */
  201. onReachBottom: function () {
  202. },
  203. /**
  204. * 用户点击右上角分享
  205. */
  206. onShareAppMessage: function () {
  207. }
  208. })