index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. let Parse = getApp().Parse;
  2. const company = getApp().globalData.company
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {},
  8. /**
  9. * 组件的初始数据
  10. */
  11. data: {
  12. statusBarHeight: 0,
  13. screenHeight: 0,
  14. customHeight: 0,
  15. bottomNavHeight: 0,
  16. contentHeight: 0,
  17. date_start: '',
  18. date_end: '',
  19. date_start1: '',
  20. date_end1: '',
  21. istoday:true,
  22. daysBetween:1,
  23. show: false,
  24. //店铺数据
  25. storeList:[],
  26. //价格
  27. price:211
  28. },
  29. lifetimes: {
  30. detached: function () {},
  31. attached: async function () {
  32. const systemInfo = wx.getSystemInfoSync();
  33. const statusBarHeight = systemInfo.statusBarHeight || 0;
  34. const screenHeight = systemInfo.screenHeight || 0;
  35. const custom = wx.getMenuButtonBoundingClientRect();
  36. const customHeight = custom.height + 10 + 2 || 0;
  37. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  38. const contentHeight = (screenHeight - bottomNavHeight - 50 - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  39. this.setData({
  40. statusBarHeight,
  41. screenHeight,
  42. customHeight,
  43. bottomNavHeight,
  44. contentHeight
  45. });
  46. this.getcurrentdate();
  47. this.gethomestar()
  48. },
  49. },
  50. /**
  51. * 组件的方法列表
  52. */
  53. methods: {
  54. //获取今日明日日期
  55. getcurrentdate() {
  56. const today = new Date();
  57. const tomorrow = new Date();
  58. tomorrow.setDate(today.getDate() + 1);
  59. this.setData({
  60. date_start: this.formatDate(today),
  61. date_end: this.formatDate(tomorrow),
  62. date_start1: this.formatDate(today),
  63. date_end1: this.formatDate(tomorrow),
  64. });
  65. console.log(this.data.date_start, this.data.date_end);
  66. },
  67. // 计算两个日期之间的天数
  68. calculateDaysBetween(startDate, endDate) {
  69. const start = new Date(startDate);
  70. const end = new Date(endDate);
  71. const timeDifference = end - start; // 计算时间差(毫秒)
  72. const daysDifference = timeDifference / (1000 * 3600 * 24); // 转换为天数
  73. return daysDifference; // 返回天数差
  74. },
  75. //开日历
  76. onDisplay() {
  77. this.setData({
  78. show: true
  79. });
  80. },
  81. //关日历
  82. onClose() {
  83. this.setData({
  84. show: false
  85. });
  86. },
  87. //转换日期
  88. formatDate(date) {
  89. date = new Date(date);
  90. return `${date.getMonth() + 1}月${date.getDate()}日`;
  91. },
  92. //选好日期点击完成后
  93. onConfirm(event) {
  94. const [start, end] = event.detail;
  95. const daysBetween = this.calculateDaysBetween(start, end); // 计算天数差
  96. this.setData({
  97. show: false,
  98. date_start: `${this.formatDate(start)} `,
  99. date_end: `${this.formatDate(end)}`,
  100. daysBetween
  101. });
  102. if(this.data.date_start.trim()==this.data.date_start1.trim()&&this.data.date_end.trim()==this.data.date_end1.trim()){
  103. this.setData({
  104. istoday:true
  105. })
  106. console.log(this.data.istoday);
  107. }else{
  108. this.setData({
  109. istoday:false
  110. })
  111. console.log(this.data.istoday);
  112. }
  113. this.gethomestar()
  114. console.log(`入住日期: ${this.data.date_start}, 离店日期: ${this.data.date_end}, 天数差: ${daysBetween}天`);
  115. },
  116. gourl(e) {
  117. const url = e.currentTarget.dataset.url;
  118. const id = e.currentTarget.dataset.id;
  119. // 构造要传递的信息
  120. const info = {
  121. objectId: id,
  122. date_start: this.data.date_start,
  123. date_end: this.data.date_end,
  124. daysBetween: this.data.daysBetween,
  125. istoday:this.data.istoday,
  126. };
  127. // 将信息转为查询字符串
  128. const queryString = Object.keys(info)
  129. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(info[key])}`)
  130. .join('&');
  131. // 使用查询字符串跳转
  132. wx.navigateTo({
  133. url: `${url}?${queryString}`,
  134. });
  135. },
  136. //获取店铺消息
  137. async gethomestar() {
  138. let ShopStore = new Parse.Query('ShopStore');
  139. ShopStore.equalTo('company', company);
  140. ShopStore.equalTo('type', "stay");
  141. ShopStore.notEqualTo('isDeleted', "true");
  142. let store = await ShopStore.find();
  143. let storeList = store.map(item => {
  144. let storeItem = item.toJSON();
  145. // 为每一项添加价格属性
  146. storeItem.price = storeItem.perCapita*this.data.daysBetween;
  147. return storeItem;
  148. });
  149. this.setData({
  150. storeList
  151. });
  152. console.log(this.data.storeList);
  153. }
  154. }
  155. });