index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 storeListPromises = store.map(async item => {
  144. let storeItem = item.toJSON();
  145. // 为每一项添加价格属性
  146. storeItem.price = storeItem.perCapita * this.data.daysBetween;
  147. storeItem.iscollect = await this.iscollect(storeItem.objectId); // 等待iscollect的结果
  148. return storeItem;
  149. });
  150. let storeList = await Promise.all(storeListPromises); // 等待所有的Promise完成
  151. this.setData({
  152. storeList
  153. });
  154. console.log(this.data.storeList);
  155. },
  156. //收藏功能
  157. async iscollect(object) {
  158. const currentUser = Parse.User.current();
  159. let Collect = new Parse.Query('DramaShopCollect');
  160. Collect.equalTo('company', company);
  161. Collect.equalTo('user', currentUser.id);
  162. Collect.equalTo('homestayStore', object);
  163. Collect.equalTo('isCollect', 'true');
  164. Collect.notEqualTo('isDeleted', "true");
  165. let collect = await Collect.first();
  166. if (collect) {
  167. return true
  168. } else {
  169. return false
  170. }
  171. },
  172. //点击收藏
  173. async submit(e) {
  174. const object = e.currentTarget.dataset.id
  175. console.log(object);
  176. const currentUser = Parse.User.current();
  177. let Collect = new Parse.Query('DramaShopCollect');
  178. Collect.equalTo('company', company);
  179. Collect.equalTo('user', currentUser.id);
  180. Collect.equalTo('homestayStore', object);
  181. Collect.notEqualTo('isDeleted', "true");
  182. let collect = await Collect.first();
  183. await this.changeiscollect(object)
  184. if (collect) {
  185. collect.set('isCollect', true)
  186. try {
  187. let saveDate = await collect.save();
  188. console.log(saveDate);
  189. console.log("收藏成功1");
  190. } catch (error) {
  191. console.error("保存数据时出现错误:", error);
  192. }
  193. } else {
  194. //user
  195. const currentUser = Parse.User.current();
  196. let userquery2 = new Parse.Query('_User');
  197. userquery2.equalTo('company', company);
  198. userquery2.equalTo('objectId', currentUser.id);
  199. userquery2.notEqualTo('isDeleted', true)
  200. let user2 = await userquery2.first();
  201. //店铺
  202. let Collect3 = new Parse.Query('ShopStore');
  203. Collect3.equalTo('company', company);
  204. Collect3.equalTo('objectId', object);
  205. Collect3.notEqualTo('isDeleted', "true");
  206. let collect3 = await Collect3.first();
  207. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  208. let Comment = new Parse.Object('DramaShopCollect');
  209. Comment.set('company', companyPointer);
  210. Comment.set('homestayStore', collect3.toPointer());
  211. Comment.set('user', user2.toPointer());
  212. Comment.set('isCollect', true);
  213. try {
  214. let saveDate2 = await Comment.save();
  215. console.log(saveDate2);
  216. console.log("收藏成功");
  217. } catch (error) {
  218. console.error("保存数据时出现错误:", error);
  219. }
  220. }
  221. },
  222. // 点击收藏后把storeList中对应的isCollect变成true
  223. changeiscollect(objectId) {
  224. // 创建一个新的 storeList 数组,以确保视图更新
  225. const updatedStoreList = this.data.storeList.map(item => {
  226. if (item.objectId === objectId) {
  227. return {
  228. ...item,
  229. iscollect: !item.iscollect // 切换 iscollect 的值
  230. };
  231. }
  232. return item; // 返回未修改的项
  233. });
  234. // 更新 storeList
  235. this.setData({
  236. storeList: updatedStoreList
  237. });
  238. console.log('修改成功', objectId);
  239. },
  240. //点击取消
  241. async cancle(e) {
  242. const object = e.currentTarget.dataset.id
  243. const currentUser = Parse.User.current();
  244. let Collect = new Parse.Query('DramaShopCollect');
  245. Collect.equalTo('company', company);
  246. Collect.equalTo('user', currentUser.id);
  247. Collect.equalTo('isCollect', true);
  248. Collect.equalTo('homestayStore', object);
  249. Collect.notEqualTo('isDeleted', "true");
  250. let collect = await Collect.first();
  251. await this.changeiscollect(object)
  252. if (collect) {
  253. collect.set('isCollect', false)
  254. try {
  255. let saveDate = await collect.save();
  256. console.log(saveDate);
  257. console.log("取消成功");
  258. } catch (error) {
  259. console.error("保存数据时出现错误:", error);
  260. }
  261. }
  262. },
  263. }
  264. });