index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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(this.compareFunction);
  186. this.setData({
  187. roomList
  188. });
  189. console.log('房间', this.data.roomList);
  190. },
  191. //排序
  192. compareFunction(a, b) {
  193. // 先比较 isroom 的值
  194. if (a.isroom !== b.isroom) {
  195. return a.isroom ? -1 : 1;
  196. }
  197. // 如果 isroom 相同,再比较 index 是否有值
  198. const hasIndexA = a.index !== undefined;
  199. const hasIndexB = b.index !== undefined;
  200. if (hasIndexA !== hasIndexB) {
  201. return hasIndexA ? -1 : 1;
  202. }
  203. // 如果 index 都有值或者都没值,按 index 大小排序(都没值时顺序其实不变)
  204. return (a.index || 0) - (b.index || 0);
  205. },
  206. async checkOrderCount(roomId, start, end) {
  207. const startTime = new Date(start);
  208. const endTime = new Date(end);
  209. let startTime2 = dateServ.changeDateTime(startTime, '14:00:00')
  210. let endTime2 = dateServ.changeDateTime(endTime, '12:00:00')
  211. let Order = new Parse.Query("RoomOrder")
  212. Order.equalTo("room", roomId)
  213. Order.equalTo("company", company)
  214. Order.exists("status")
  215. Order.notContainedIn('status', [400, 601, 700,800,103])
  216. Order.lessThan("startTime", endTime2);
  217. Order.greaterThan("endTime", startTime2);
  218. Order.select("startTime", "endTime")
  219. let count = await Order.count()
  220. return count
  221. },
  222. //收藏功能
  223. async iscollect(object) {
  224. const currentUser = Parse.User.current();
  225. let Collect = new Parse.Query('DramaShopCollect');
  226. Collect.equalTo('company', company);
  227. Collect.equalTo('user', currentUser.id);
  228. Collect.equalTo('homestayStore', object);
  229. Collect.equalTo('isCollect', 'true');
  230. Collect.notEqualTo('isDeleted', "true");
  231. let collect = await Collect.first();
  232. if (collect) {
  233. return true
  234. } else {
  235. return false
  236. }
  237. },
  238. //获取轮播图
  239. // async getpic() {
  240. // let Banner = new Parse.Query('Banner');
  241. // Banner.equalTo('company', company);
  242. // Banner.equalTo('store', this.data.objectId);
  243. // Banner.equalTo('isEnabled', 'true');
  244. // Banner.notEqualTo('isDeleted', 'true');
  245. // Banner.select('image');
  246. // let Banner2 = await Banner.find();
  247. // // 提取 image 属性并存储到 imageUrls 中
  248. // let imageUrls = Banner2.map(item => item.get('image')); // 使用 get() 方法获取 image 属性
  249. // this.setData({
  250. // imageUrls // 将提取的 imageUrls 存储到组件状态中
  251. // });
  252. // console.log(this.data.imageUrls); // 输出 imageUrls
  253. // },
  254. //获取当前位置信息
  255. Getlocation() {
  256. // 假设 this.storeList[0].location 是一个 Parse.GeoPoint 对象
  257. if (this.data.storeList[0].location) {
  258. const storeLocation = this.data.storeList[0].location;
  259. console.log('地址存在');
  260. // 从 GeoPoint 对象中获取经纬度
  261. const latitude = storeLocation.latitude; // 纬度
  262. const longitude = storeLocation.longitude; // 经度
  263. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  264. // 调用 API 解析地址
  265. wx.request({
  266. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  267. data: {},
  268. header: {
  269. 'Content-Type': 'application/json'
  270. },
  271. success: (ops) => { // 使用箭头函数
  272. console.log(ops);
  273. const address = ops.data.result.formatted_address;
  274. this.setData({
  275. address: address,
  276. latitude: latitude, // 保证 latitude 被设置
  277. longitude: longitude, // 保证 longitude 被设置
  278. markers: [{ // 设置 markers
  279. id: 1,
  280. latitude: latitude,
  281. longitude: longitude,
  282. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  283. width: 20,
  284. height: 20,
  285. // callout: {
  286. // content: this.data.storeList[0].storeName, // 可以显示解析出的地址
  287. // color: '#ffffff',
  288. // bgColor: '#7F56B2',
  289. // padding: 4,
  290. // fontSize: 8,
  291. // borderRadius: 3,
  292. // display: 'ALWAYS'
  293. // }
  294. }]
  295. });
  296. // console.log(this.data.address);
  297. },
  298. fail: function (resq) {
  299. wx.showModal({
  300. title: '信息提示',
  301. content: '请求失败',
  302. showCancel: false,
  303. confirmColor: '#f37938'
  304. });
  305. },
  306. complete: function () {}
  307. });
  308. }
  309. },
  310. //点击预定
  311. navigate(e) {
  312. const objectId = e.currentTarget.dataset.id;
  313. // let currentUser = Parse.User.current()
  314. // currentUser = currentUser.toJSON()
  315. // console.log(currentUser, currentUser.idcard);
  316. // if (currentUser.idcard) {
  317. // // 构造要传递的信息
  318. // const info = {
  319. // objectId: objectId,
  320. // date_start: this.data.start,
  321. // date_end: this.data.end,
  322. // };
  323. // // console.log('info',info);
  324. // // 将信息转为查询字符串
  325. // var queryString = Object.keys(info)
  326. // .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(info[key])}`)
  327. // .join('&');
  328. // console.log(queryString);
  329. // wx.navigateTo({
  330. // url: `../homestay-order/index?${queryString}`
  331. // })
  332. // } else {
  333. // wx.showToast({
  334. // title: '请先进行实名认证',
  335. // icon: 'none'
  336. // })
  337. //实名
  338. // wx.navigateTo({
  339. // url: `/common-page/pages/info/cauth/cauth?themeColor=#FFE300`
  340. // })
  341. // 构造要传递的信息
  342. const info = {
  343. objectId: objectId,
  344. date_start: this.data.start,
  345. date_end: this.data.end,
  346. };
  347. console.log('info', info);
  348. // 将信息转为查询字符串
  349. var queryString = Object.keys(info)
  350. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(info[key])}`)
  351. .join('&');
  352. console.log(queryString);
  353. wx.navigateTo({
  354. url: `../homestay-order2/index?${queryString}`
  355. })
  356. // }
  357. },
  358. //获取今日明日日期
  359. getcurrentdate() {
  360. const today = new Date();
  361. const tomorrow = new Date();
  362. tomorrow.setDate(today.getDate() + 1);
  363. this.setData({
  364. date_start1: this.formatDate(today),
  365. date_end1: this.formatDate(tomorrow),
  366. });
  367. console.log(this.data.start, this.data.end);
  368. },
  369. //转换日期
  370. formatDate(date) {
  371. date = new Date(date);
  372. return `${date.getMonth() + 1}月${date.getDate()}日`;
  373. },
  374. // 计算两个日期之间的天数
  375. calculateDaysBetween(startDate, endDate) {
  376. const start = new Date(startDate);
  377. const end = new Date(endDate);
  378. const timeDifference = end - start; // 计算时间差(毫秒)
  379. const daysDifference = timeDifference / (1000 * 3600 * 24); // 转换为天数
  380. return daysDifference; // 返回天数差
  381. },
  382. //开日历
  383. onDisplay() {
  384. this.setData({
  385. show: true
  386. });
  387. },
  388. //关日历
  389. onClose() {
  390. this.setData({
  391. show: false
  392. });
  393. },
  394. //选好日期点击完成后
  395. onConfirm(event) {
  396. const [start, end] = event.detail;
  397. const daysBetween = this.calculateDaysBetween(start, end); // 计算天数差
  398. this.setData({
  399. show: false,
  400. start,
  401. end,
  402. decodedDateStartart: `${this.formatDate(start)} `,
  403. decodedDateEnd: `${this.formatDate(end)}`,
  404. daysBetween
  405. });
  406. if (this.data.decodedDateStartart.trim() == this.data.date_start1.trim() && this.data.decodedDateEnd.trim() == this.data.date_end1.trim()) {
  407. this.setData({
  408. istoday: 'true'
  409. })
  410. console.log(this.data.istoday);
  411. } else {
  412. this.setData({
  413. istoday: 'false'
  414. })
  415. console.log(this.data.istoday);
  416. }
  417. this.getroom()
  418. console.log(`入住日期: ${this.data.decodedDateStartart}, 离店日期: ${this.data.decodedDateEnd}, 天数差: ${daysBetween}天`);
  419. },
  420. //拨打电话
  421. phone() {
  422. let phone = this.data.storeList[0].mobile
  423. console.log(phone);
  424. wx.makePhoneCall({
  425. phoneNumber: phone
  426. })
  427. },
  428. callMap() {
  429. if (this.data.storeList[0].location) {
  430. const latitude = this.data.storeList[0].location.latitude
  431. const longitude = this.data.storeList[0].location.longitude
  432. wx.openLocation({
  433. name: this.data.storeList[0].storeName,
  434. latitude,
  435. longitude,
  436. scale: 18
  437. })
  438. }
  439. }
  440. })