index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // nova-tourism/pages/homestay/homestay-detail/index.js
  2. let Parse = getApp().Parse;
  3. const company = getApp().globalData.company
  4. import dateServ from '../../../service/date';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. //屏幕高度
  11. statusBarHeight: 0, // 状态栏高度
  12. screenHeight: 0, // 屏幕高度
  13. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  14. bottomNavHeight: 0, // 底部导航栏高度
  15. contentHeight: 0, // 可用内容高度
  16. // 轮播图数组
  17. imageUrls: [],
  18. index: 1,
  19. //
  20. decodedDateStart: '',
  21. decodedDateEnd: '',
  22. objectId: "",
  23. daysBetween: 0,
  24. istoday: null,
  25. storeList: [],
  26. roomList: [],
  27. //地图
  28. longitude: 0,
  29. latitude: 0,
  30. markers: [],
  31. //
  32. start: '',
  33. end: '',
  34. //
  35. show: false,
  36. date_start1: '',
  37. date_end1: '',
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: function (options) {
  43. // 计算
  44. const systemInfo = wx.getSystemInfoSync();
  45. const statusBarHeight = systemInfo.statusBarHeight || 0;
  46. const screenHeight = systemInfo.screenHeight || 0;
  47. const custom = wx.getMenuButtonBoundingClientRect();
  48. const customHeight = custom.height + 10 + 2 || 0;
  49. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  50. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  51. if (bottomNavHeight) {
  52. const padding_bottom = bottomNavHeight * 750 / systemInfo.windowWidth
  53. this.setData({
  54. bottomNavHeight: padding_bottom
  55. })
  56. console.log(this.data.bottomNavHeight);
  57. } else {
  58. this.setData({
  59. bottomNavHeight: 40
  60. })
  61. }
  62. this.setData({
  63. statusBarHeight,
  64. screenHeight,
  65. customHeight,
  66. contentHeight
  67. });
  68. const {
  69. objectId,
  70. date_start,
  71. date_end,
  72. daysBetween,
  73. istoday,
  74. start,
  75. end,
  76. } = options;
  77. console.log(options);
  78. // 解码接收到的参数
  79. const decodedDateStart = decodeURIComponent(date_start);
  80. const decodedDateEnd = decodeURIComponent(date_end);
  81. const Start = decodeURIComponent(start);
  82. const End = decodeURIComponent(end);
  83. this.setData({
  84. decodedDateStart,
  85. decodedDateEnd,
  86. objectId,
  87. daysBetween,
  88. istoday,
  89. start: Start,
  90. end: End,
  91. })
  92. console.log('istoday', this.data.istoday);
  93. this.hanshu()
  94. },
  95. async hanshu() {
  96. await this.gethomestay()
  97. await this.getroom()
  98. // await this.getpic()
  99. await this.getcurrentdate()
  100. },
  101. /**
  102. * 生命周期函数--监听页面初次渲染完成
  103. */
  104. onReady: function () {
  105. },
  106. /**
  107. * 生命周期函数--监听页面显示
  108. */
  109. onShow: function () {
  110. },
  111. /**
  112. * 生命周期函数--监听页面隐藏
  113. */
  114. onHide: function () {
  115. },
  116. /**
  117. * 生命周期函数--监听页面卸载
  118. */
  119. onUnload: function () {
  120. },
  121. /**
  122. * 页面相关事件处理函数--监听用户下拉动作
  123. */
  124. onPullDownRefresh: function () {
  125. },
  126. /**
  127. * 页面上拉触底事件的处理函数
  128. */
  129. onReachBottom: function () {
  130. },
  131. /**
  132. * 用户点击右上角分享
  133. */
  134. onShareAppMessage: function () {
  135. },
  136. //随轮播图变化而变化
  137. onSwiperChange: function (event) {
  138. const currentIndex = event.detail.current; // 获取当前索引
  139. this.setData({
  140. index: currentIndex + 1
  141. })
  142. },
  143. //获取名宿信息
  144. async gethomestay() {
  145. let ShopStore = new Parse.Query('ShopStore');
  146. ShopStore.equalTo('company', company);
  147. ShopStore.equalTo('type', "stay");
  148. ShopStore.equalTo('objectId', this.data.objectId);
  149. ShopStore.notEqualTo('isDeleted', "true");
  150. ShopStore.include('location');
  151. let store = await ShopStore.find();
  152. let storeListPromises = store.map(async item => {
  153. let storeItem = item.toJSON();
  154. storeItem.iscollect = await this.iscollect(storeItem.objectId); // 等待iscollect的结果
  155. return storeItem;
  156. });
  157. let storeList = await Promise.all(storeListPromises); // 等待所有的Promise完成
  158. this.setData({
  159. storeList
  160. });
  161. this.setData({
  162. imageUrls: storeList[0].image
  163. })
  164. this.Getlocation()
  165. console.log(this.data.storeList);
  166. },
  167. //获取房间信息
  168. async getroom() {
  169. let room = new Parse.Query('ShopRoom');
  170. room.equalTo('company', company);
  171. room.equalTo('shop', this.data.objectId);
  172. room.equalTo('isEnabled', 'true');
  173. room.include('benefitMap');
  174. room.notEqualTo('isDeleted', 'true');
  175. let room2 = await room.find();
  176. // 使用 Promise.all 来处理异步操作
  177. let roomList = await Promise.all(room2.map(async item => {
  178. let roomItem = item.toJSON();
  179. let count = await this.checkOrderCount(roomItem.objectId, this.data.start, this.data.end);
  180. console.log(count, roomItem.total);
  181. roomItem.isroom = count < roomItem.total; // 简化判断
  182. return roomItem;
  183. }));
  184. // 对 roomList 进行排序
  185. roomList.sort((a, b) => {
  186. // 首先比较 isroom,true 排在前面
  187. if (a.isroom === b.isroom) {
  188. // 如果 isroom 相同,按 remaining 排序
  189. if (a.remaining === 0 && b.remaining !== 0) {
  190. return 1; // a 排后面
  191. }
  192. if (a.remaining !== 0 && b.remaining === 0) {
  193. return -1; // a 排前面
  194. }
  195. // 如果两个房间的 remaining 都不为 0,按数量升序排列
  196. return a.remaining - b.remaining;
  197. }
  198. return a.isroom ? -1 : 1; // true 排在前面,false 排在后面
  199. });
  200. this.setData({
  201. roomList
  202. });
  203. console.log('房间', this.data.roomList);
  204. },
  205. async checkOrderCount(roomId, start, end) {
  206. const startTime = new Date(start);
  207. const endTime = new Date(end);
  208. let now = dateServ.changeDateTime(new Date(), '14:00:00')
  209. let Order = new Parse.Query("RoomOrder")
  210. Order.equalTo("room", roomId)
  211. Order.equalTo("company", company)
  212. Order.exists("status")
  213. Order.notContainedIn('status', [100, 400, 601, 700])
  214. Order.greaterThanOrEqualTo("startTime", endTime) //20 23 21 22
  215. Order.lessThanOrEqualTo("endTime", startTime)
  216. Order.select("startTime", "endTime")
  217. let count = await Order.count()
  218. console.log(count);
  219. return count
  220. },
  221. //收藏功能
  222. async iscollect(object) {
  223. const currentUser = Parse.User.current();
  224. let Collect = new Parse.Query('DramaShopCollect');
  225. Collect.equalTo('company', company);
  226. Collect.equalTo('user', currentUser.id);
  227. Collect.equalTo('homestayStore', object);
  228. Collect.equalTo('isCollect', 'true');
  229. Collect.notEqualTo('isDeleted', "true");
  230. let collect = await Collect.first();
  231. if (collect) {
  232. return true
  233. } else {
  234. return false
  235. }
  236. },
  237. //获取轮播图
  238. // async getpic() {
  239. // let Banner = new Parse.Query('Banner');
  240. // Banner.equalTo('company', company);
  241. // Banner.equalTo('store', this.data.objectId);
  242. // Banner.equalTo('isEnabled', 'true');
  243. // Banner.notEqualTo('isDeleted', 'true');
  244. // Banner.select('image');
  245. // let Banner2 = await Banner.find();
  246. // // 提取 image 属性并存储到 imageUrls 中
  247. // let imageUrls = Banner2.map(item => item.get('image')); // 使用 get() 方法获取 image 属性
  248. // this.setData({
  249. // imageUrls // 将提取的 imageUrls 存储到组件状态中
  250. // });
  251. // console.log(this.data.imageUrls); // 输出 imageUrls
  252. // },
  253. //获取当前位置信息
  254. Getlocation() {
  255. // 假设 this.storeList[0].location 是一个 Parse.GeoPoint 对象
  256. if (this.data.storeList[0].location) {
  257. const storeLocation = this.data.storeList[0].location;
  258. console.log('地址存在');
  259. // 从 GeoPoint 对象中获取经纬度
  260. const latitude = storeLocation.latitude; // 纬度
  261. const longitude = storeLocation.longitude; // 经度
  262. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  263. // 调用 API 解析地址
  264. wx.request({
  265. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  266. data: {},
  267. header: {
  268. 'Content-Type': 'application/json'
  269. },
  270. success: (ops) => { // 使用箭头函数
  271. console.log(ops);
  272. const address = ops.data.result.formatted_address;
  273. this.setData({
  274. address: address,
  275. latitude: latitude, // 保证 latitude 被设置
  276. longitude: longitude, // 保证 longitude 被设置
  277. markers: [{ // 设置 markers
  278. id: 1,
  279. latitude: latitude,
  280. longitude: longitude,
  281. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  282. width: 20,
  283. height: 20,
  284. callout: {
  285. content: this.data.storeList[0].storeName, // 可以显示解析出的地址
  286. color: '#ffffff',
  287. bgColor: '#7F56B2',
  288. padding: 2,
  289. fontSize:12,
  290. borderRadius: 5,
  291. display: 'ALWAYS'
  292. }
  293. }]
  294. });
  295. // console.log(this.data.address);
  296. },
  297. fail: function (resq) {
  298. wx.showModal({
  299. title: '信息提示',
  300. content: '请求失败',
  301. showCancel: false,
  302. confirmColor: '#f37938'
  303. });
  304. },
  305. complete: function () {}
  306. });
  307. }
  308. },
  309. //点击预定
  310. navigate(e) {
  311. const objectId = e.currentTarget.dataset.id;
  312. // let currentUser = Parse.User.current()
  313. // currentUser = currentUser.toJSON()
  314. // console.log(currentUser, currentUser.idcard);
  315. // if (currentUser.idcard) {
  316. // // 构造要传递的信息
  317. // const info = {
  318. // objectId: objectId,
  319. // date_start: this.data.start,
  320. // date_end: this.data.end,
  321. // };
  322. // // console.log('info',info);
  323. // // 将信息转为查询字符串
  324. // var queryString = Object.keys(info)
  325. // .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(info[key])}`)
  326. // .join('&');
  327. // console.log(queryString);
  328. // wx.navigateTo({
  329. // url: `../homestay-order/index?${queryString}`
  330. // })
  331. // } else {
  332. // wx.showToast({
  333. // title: '请先进行实名认证',
  334. // icon: 'none'
  335. // })
  336. //实名
  337. // wx.navigateTo({
  338. // url: `/common-page/pages/info/cauth/cauth?themeColor=#FFE300`
  339. // })
  340. // 构造要传递的信息
  341. const info = {
  342. objectId: objectId,
  343. date_start: this.data.start,
  344. date_end: this.data.end,
  345. };
  346. console.log('info', info);
  347. // 将信息转为查询字符串
  348. var queryString = Object.keys(info)
  349. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(info[key])}`)
  350. .join('&');
  351. console.log(queryString);
  352. wx.navigateTo({
  353. url: `../homestay-order2/index?${queryString}`
  354. })
  355. // }
  356. },
  357. //获取今日明日日期
  358. getcurrentdate() {
  359. const today = new Date();
  360. const tomorrow = new Date();
  361. tomorrow.setDate(today.getDate() + 1);
  362. this.setData({
  363. date_start1: this.formatDate(today),
  364. date_end1: this.formatDate(tomorrow),
  365. });
  366. console.log(this.data.start, this.data.end);
  367. },
  368. //转换日期
  369. formatDate(date) {
  370. date = new Date(date);
  371. return `${date.getMonth() + 1}月${date.getDate()}日`;
  372. },
  373. // 计算两个日期之间的天数
  374. calculateDaysBetween(startDate, endDate) {
  375. const start = new Date(startDate);
  376. const end = new Date(endDate);
  377. const timeDifference = end - start; // 计算时间差(毫秒)
  378. const daysDifference = timeDifference / (1000 * 3600 * 24); // 转换为天数
  379. return daysDifference; // 返回天数差
  380. },
  381. //开日历
  382. onDisplay() {
  383. this.setData({
  384. show: true
  385. });
  386. },
  387. //关日历
  388. onClose() {
  389. this.setData({
  390. show: false
  391. });
  392. },
  393. //选好日期点击完成后
  394. onConfirm(event) {
  395. const [start, end] = event.detail;
  396. const daysBetween = this.calculateDaysBetween(start, end); // 计算天数差
  397. this.setData({
  398. show: false,
  399. start,
  400. end,
  401. decodedDateStartart: `${this.formatDate(start)} `,
  402. decodedDateEnd: `${this.formatDate(end)}`,
  403. daysBetween
  404. });
  405. if (this.data.decodedDateStartart.trim() == this.data.date_start1.trim() && this.data.decodedDateEnd.trim() == this.data.date_end1.trim()) {
  406. this.setData({
  407. istoday: 'true'
  408. })
  409. console.log(this.data.istoday);
  410. } else {
  411. this.setData({
  412. istoday: 'false'
  413. })
  414. console.log(this.data.istoday);
  415. }
  416. console.log(`入住日期: ${this.data.decodedDateStartart}, 离店日期: ${this.data.decodedDateEnd}, 天数差: ${daysBetween}天`);
  417. },
  418. //拨打电话
  419. phone() {
  420. let phone = this.data.storeList[0].mobile
  421. console.log(phone);
  422. wx.makePhoneCall({
  423. phoneNumber: phone
  424. })
  425. },
  426. callMap() {
  427. if (this.data.storeList[0].location) {
  428. const latitude = this.data.storeList[0].location.latitude
  429. const longitude = this.data.storeList[0].location.longitude
  430. wx.openLocation({
  431. name: this.data.storeList[0].storeName,
  432. latitude,
  433. longitude,
  434. scale: 18
  435. })
  436. }
  437. }
  438. })