index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. value: ''
  30. },
  31. lifetimes: {
  32. detached: function () {},
  33. attached: async function () {
  34. const systemInfo = wx.getSystemInfoSync();
  35. const statusBarHeight = systemInfo.statusBarHeight || 0;
  36. const screenHeight = systemInfo.screenHeight || 0;
  37. const custom = wx.getMenuButtonBoundingClientRect();
  38. const customHeight = custom.height + 10 + 2 || 0;
  39. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  40. const contentHeight = (screenHeight - bottomNavHeight - 50 - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  41. this.setData({
  42. statusBarHeight,
  43. screenHeight,
  44. customHeight,
  45. bottomNavHeight,
  46. contentHeight
  47. });
  48. this.getcurrentdate();
  49. this.gethomestar()
  50. },
  51. },
  52. /**
  53. * 组件的方法列表
  54. */
  55. methods: {
  56. //获取今日明日日期
  57. getcurrentdate() {
  58. const today = new Date();
  59. const tomorrow = new Date();
  60. tomorrow.setDate(today.getDate() + 1);
  61. this.setData({
  62. date_start: this.formatDate(today),
  63. date_end: this.formatDate(tomorrow),
  64. date_start1: this.formatDate(today),
  65. date_end1: this.formatDate(tomorrow),
  66. });
  67. console.log(this.data.date_start, this.data.date_end);
  68. },
  69. // 计算两个日期之间的天数
  70. calculateDaysBetween(startDate, endDate) {
  71. const start = new Date(startDate);
  72. const end = new Date(endDate);
  73. const timeDifference = end - start; // 计算时间差(毫秒)
  74. const daysDifference = timeDifference / (1000 * 3600 * 24); // 转换为天数
  75. return daysDifference; // 返回天数差
  76. },
  77. //开日历
  78. onDisplay() {
  79. this.setData({
  80. show: true
  81. });
  82. },
  83. //关日历
  84. onClose() {
  85. this.setData({
  86. show: false
  87. });
  88. },
  89. //转换日期
  90. formatDate(date) {
  91. date = new Date(date);
  92. return `${date.getMonth() + 1}月${date.getDate()}日`;
  93. },
  94. //选好日期点击完成后
  95. onConfirm(event) {
  96. const [start, end] = event.detail;
  97. const daysBetween = this.calculateDaysBetween(start, end); // 计算天数差
  98. this.setData({
  99. show: false,
  100. date_start: `${this.formatDate(start)} `,
  101. date_end: `${this.formatDate(end)}`,
  102. daysBetween
  103. });
  104. if (this.data.date_start.trim() == this.data.date_start1.trim() && this.data.date_end.trim() == this.data.date_end1.trim()) {
  105. this.setData({
  106. istoday: true
  107. })
  108. console.log(this.data.istoday);
  109. } else {
  110. this.setData({
  111. istoday: false
  112. })
  113. console.log(this.data.istoday);
  114. }
  115. this.gethomestar()
  116. console.log(`入住日期: ${this.data.date_start}, 离店日期: ${this.data.date_end}, 天数差: ${daysBetween}天`);
  117. },
  118. gourl(e) {
  119. const url = e.currentTarget.dataset.url;
  120. const id = e.currentTarget.dataset.id;
  121. // 构造要传递的信息
  122. const info = {
  123. objectId: id,
  124. date_start: this.data.date_start,
  125. date_end: this.data.date_end,
  126. daysBetween: this.data.daysBetween,
  127. istoday: this.data.istoday,
  128. };
  129. // 将信息转为查询字符串
  130. const queryString = Object.keys(info)
  131. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(info[key])}`)
  132. .join('&');
  133. // 使用查询字符串跳转
  134. wx.navigateTo({
  135. url: `${url}?${queryString}`,
  136. });
  137. },
  138. //获取店铺消息
  139. async gethomestar() {
  140. let ShopStore = new Parse.Query('ShopStore');
  141. ShopStore.equalTo('company', company);
  142. ShopStore.equalTo('type', "stay");
  143. ShopStore.notEqualTo('isDeleted', "true");
  144. let store = await ShopStore.find();
  145. let storeListPromises = store.map(async item => {
  146. let storeItem = item.toJSON();
  147. // 为每一项添加价格属性
  148. storeItem.price = storeItem.perCapita * this.data.daysBetween;
  149. storeItem.iscollect = await this.iscollect(storeItem.objectId); // 等待iscollect的结果
  150. return storeItem;
  151. });
  152. let storeList = await Promise.all(storeListPromises); // 等待所有的Promise完成
  153. this.setData({
  154. storeList
  155. });
  156. console.log(this.data.storeList);
  157. },
  158. //收藏功能
  159. async iscollect(object) {
  160. const currentUser = Parse.User.current();
  161. let Collect = new Parse.Query('DramaShopCollect');
  162. Collect.equalTo('company', company);
  163. Collect.equalTo('user', currentUser.id);
  164. Collect.equalTo('homestayStore', object);
  165. Collect.equalTo('isCollect', 'true');
  166. Collect.notEqualTo('isDeleted', "true");
  167. let collect = await Collect.first();
  168. if (collect) {
  169. return true
  170. } else {
  171. return false
  172. }
  173. },
  174. //点击收藏
  175. async submit(e) {
  176. const object = e.currentTarget.dataset.id
  177. console.log(object);
  178. const currentUser = Parse.User.current();
  179. let Collect = new Parse.Query('DramaShopCollect');
  180. Collect.equalTo('company', company);
  181. Collect.equalTo('user', currentUser.id);
  182. Collect.equalTo('homestayStore', object);
  183. Collect.notEqualTo('isDeleted', "true");
  184. let collect = await Collect.first();
  185. await this.changeiscollect(object)
  186. if (collect) {
  187. collect.set('isCollect', true)
  188. try {
  189. let saveDate = await collect.save();
  190. console.log(saveDate);
  191. console.log("收藏成功1");
  192. } catch (error) {
  193. console.error("保存数据时出现错误:", error);
  194. }
  195. } else {
  196. //user
  197. const currentUser = Parse.User.current();
  198. let userquery2 = new Parse.Query('_User');
  199. userquery2.equalTo('company', company);
  200. userquery2.equalTo('objectId', currentUser.id);
  201. userquery2.notEqualTo('isDeleted', true)
  202. let user2 = await userquery2.first();
  203. //店铺
  204. let Collect3 = new Parse.Query('ShopStore');
  205. Collect3.equalTo('company', company);
  206. Collect3.equalTo('objectId', object);
  207. Collect3.notEqualTo('isDeleted', "true");
  208. let collect3 = await Collect3.first();
  209. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  210. let Comment = new Parse.Object('DramaShopCollect');
  211. Comment.set('company', companyPointer);
  212. Comment.set('homestayStore', collect3.toPointer());
  213. Comment.set('user', user2.toPointer());
  214. Comment.set('isCollect', true);
  215. try {
  216. let saveDate2 = await Comment.save();
  217. console.log(saveDate2);
  218. console.log("收藏成功");
  219. } catch (error) {
  220. console.error("保存数据时出现错误:", error);
  221. }
  222. }
  223. },
  224. // 点击收藏后把storeList中对应的isCollect变成true
  225. changeiscollect(objectId) {
  226. // 创建一个新的 storeList 数组,以确保视图更新
  227. const updatedStoreList = this.data.storeList.map(item => {
  228. if (item.objectId === objectId) {
  229. return {
  230. ...item,
  231. iscollect: !item.iscollect // 切换 iscollect 的值
  232. };
  233. }
  234. return item; // 返回未修改的项
  235. });
  236. // 更新 storeList
  237. this.setData({
  238. storeList: updatedStoreList
  239. });
  240. console.log('修改成功', objectId);
  241. },
  242. //点击取消
  243. async cancle(e) {
  244. const object = e.currentTarget.dataset.id
  245. const currentUser = Parse.User.current();
  246. let Collect = new Parse.Query('DramaShopCollect');
  247. Collect.equalTo('company', company);
  248. Collect.equalTo('user', currentUser.id);
  249. Collect.equalTo('isCollect', true);
  250. Collect.equalTo('homestayStore', object);
  251. Collect.notEqualTo('isDeleted', "true");
  252. let collect = await Collect.first();
  253. await this.changeiscollect(object)
  254. if (collect) {
  255. collect.set('isCollect', false)
  256. try {
  257. let saveDate = await collect.save();
  258. console.log(saveDate);
  259. console.log("取消成功");
  260. } catch (error) {
  261. console.error("保存数据时出现错误:", error);
  262. }
  263. }
  264. },
  265. //搜索功能
  266. search(e) {
  267. const value = e.detail;
  268. console.log(value);
  269. this.getRooms(value)
  270. },
  271. //清零后
  272. change(e) {
  273. const value = e.detail;
  274. if (!value) {
  275. console.log('运行');
  276. this.gethomestar()
  277. }
  278. },
  279. // 搜索函数
  280. async getRooms(value) {
  281. // 确保 value 不为空
  282. if (!value) {
  283. console.log('搜索值为空');
  284. return; // 如果没有输入值,直接返回
  285. }
  286. let queryParams = {
  287. where: {
  288. $or: [{
  289. storeName: {
  290. $regex: `.*${value}.*` // 使用 value 进行正则匹配
  291. }
  292. }, {
  293. storeAddress: {
  294. $regex: `.*${value}.*` // 使用 value 进行正则匹配
  295. }
  296. }]
  297. }
  298. };
  299. let ShopStore = Parse.Query.fromJSON('ShopStore', queryParams);
  300. ShopStore.equalTo('company', company);
  301. ShopStore.equalTo('type', "stay");
  302. ShopStore.notEqualTo('isDeleted', "true");
  303. try {
  304. let store = await ShopStore.find();
  305. let storeListPromises = store.map(async item => {
  306. let storeItem = item.toJSON();
  307. // 为每一项添加价格属性
  308. storeItem.price = storeItem.perCapita * this.data.daysBetween;
  309. storeItem.iscollect = await this.iscollect(storeItem.objectId); // 等待 iscollect 的结果
  310. return storeItem;
  311. });
  312. let storeList = await Promise.all(storeListPromises); // 等待所有的 Promise 完成
  313. this.setData({
  314. storeList // 更新 storeList
  315. });
  316. } catch (error) {
  317. console.error("查询商店时出现错误:", error);
  318. }
  319. }
  320. //添加后台经纬度
  321. // async setadd() {
  322. // let ShopStore = new Parse.Query('ShopStore');
  323. // ShopStore.equalTo('company', company);
  324. // ShopStore.equalTo('objectId', "GyeSIi3zwL");
  325. // let store = await ShopStore.first();
  326. // // 创建 GeoPoint 对象
  327. // let location = new Parse.GeoPoint(28.679224,117.818856); // 注意:GeoPoint 的参数是 (latitude, longitude)
  328. // // 设置 location 字段
  329. // store.set('location', location);
  330. // try {
  331. // let saveDate = await store.save();
  332. // console.log(saveDate);
  333. // console.log("上传地址成功");
  334. // } catch (error) {
  335. // console.error("保存数据时出现错误:", error);
  336. // }
  337. // }
  338. }
  339. });