index.js 13 KB

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