index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. const storeLocation = this.data.storeList[0].location;
  257. console.log();
  258. // 从 GeoPoint 对象中获取经纬度
  259. const latitude = storeLocation.latitude; // 纬度
  260. const longitude = storeLocation.longitude; // 经度
  261. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  262. // 调用 API 解析地址
  263. wx.request({
  264. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  265. data: {},
  266. header: {
  267. 'Content-Type': 'application/json'
  268. },
  269. success: (ops) => { // 使用箭头函数
  270. console.log(ops);
  271. const address = ops.data.result.formatted_address;
  272. this.setData({
  273. address: address,
  274. latitude: latitude, // 保证 latitude 被设置
  275. longitude: longitude, // 保证 longitude 被设置
  276. markers: [{ // 设置 markers
  277. id: 1,
  278. latitude: latitude,
  279. longitude: longitude,
  280. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  281. width: 30,
  282. height: 30,
  283. callout: {
  284. content: this.data.storeList[0].storeName, // 可以显示解析出的地址
  285. color: '#ffffff',
  286. bgColor: '#7F56B2',
  287. padding: 10,
  288. borderRadius: 5,
  289. display: 'ALWAYS'
  290. }
  291. }]
  292. });
  293. console.log(this.data.address);
  294. },
  295. fail: function (resq) {
  296. wx.showModal({
  297. title: '信息提示',
  298. content: '请求失败',
  299. showCancel: false,
  300. confirmColor: '#f37938'
  301. });
  302. },
  303. complete: function () {}
  304. });
  305. },
  306. //点击预定
  307. navigate(e) {
  308. const objectId = e.currentTarget.dataset.id;
  309. // let currentUser = Parse.User.current()
  310. // currentUser = currentUser.toJSON()
  311. // console.log(currentUser, currentUser.idcard);
  312. // if (currentUser.idcard) {
  313. // // 构造要传递的信息
  314. // const info = {
  315. // objectId: objectId,
  316. // date_start: this.data.start,
  317. // date_end: this.data.end,
  318. // };
  319. // // console.log('info',info);
  320. // // 将信息转为查询字符串
  321. // var queryString = Object.keys(info)
  322. // .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(info[key])}`)
  323. // .join('&');
  324. // console.log(queryString);
  325. // wx.navigateTo({
  326. // url: `../homestay-order/index?${queryString}`
  327. // })
  328. // } else {
  329. // wx.showToast({
  330. // title: '请先进行实名认证',
  331. // icon: 'none'
  332. // })
  333. //实名
  334. // wx.navigateTo({
  335. // url: `/common-page/pages/info/cauth/cauth?themeColor=#FFE300`
  336. // })
  337. // 构造要传递的信息
  338. const info = {
  339. objectId: objectId,
  340. date_start: this.data.start,
  341. date_end: this.data.end,
  342. };
  343. console.log('info', info);
  344. // 将信息转为查询字符串
  345. var queryString = Object.keys(info)
  346. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(info[key])}`)
  347. .join('&');
  348. console.log(queryString);
  349. wx.navigateTo({
  350. url: `../homestay-order2/index?${queryString}`
  351. })
  352. // }
  353. },
  354. //获取今日明日日期
  355. getcurrentdate() {
  356. const today = new Date();
  357. const tomorrow = new Date();
  358. tomorrow.setDate(today.getDate() + 1);
  359. this.setData({
  360. date_start1: this.formatDate(today),
  361. date_end1: this.formatDate(tomorrow),
  362. });
  363. console.log(this.data.start, this.data.end);
  364. },
  365. //转换日期
  366. formatDate(date) {
  367. date = new Date(date);
  368. return `${date.getMonth() + 1}月${date.getDate()}日`;
  369. },
  370. // 计算两个日期之间的天数
  371. calculateDaysBetween(startDate, endDate) {
  372. const start = new Date(startDate);
  373. const end = new Date(endDate);
  374. const timeDifference = end - start; // 计算时间差(毫秒)
  375. const daysDifference = timeDifference / (1000 * 3600 * 24); // 转换为天数
  376. return daysDifference; // 返回天数差
  377. },
  378. //开日历
  379. onDisplay() {
  380. this.setData({
  381. show: true
  382. });
  383. },
  384. //关日历
  385. onClose() {
  386. this.setData({
  387. show: false
  388. });
  389. },
  390. //选好日期点击完成后
  391. onConfirm(event) {
  392. const [start, end] = event.detail;
  393. const daysBetween = this.calculateDaysBetween(start, end); // 计算天数差
  394. this.setData({
  395. show: false,
  396. start,
  397. end,
  398. decodedDateStartart: `${this.formatDate(start)} `,
  399. decodedDateEnd: `${this.formatDate(end)}`,
  400. daysBetween
  401. });
  402. if (this.data.decodedDateStartart.trim() == this.data.date_start1.trim() && this.data.decodedDateEnd.trim() == this.data.date_end1.trim()) {
  403. this.setData({
  404. istoday: 'true'
  405. })
  406. console.log(this.data.istoday);
  407. } else {
  408. this.setData({
  409. istoday: 'false'
  410. })
  411. console.log(this.data.istoday);
  412. }
  413. console.log(`入住日期: ${this.data.decodedDateStartart}, 离店日期: ${this.data.decodedDateEnd}, 天数差: ${daysBetween}天`);
  414. },
  415. //拨打电话
  416. phone() {
  417. let phone = this.data.storeList[0].mobile
  418. console.log(phone);
  419. wx.makePhoneCall({
  420. phoneNumber: phone
  421. })
  422. },
  423. callMap(){
  424. const latitude = this.data.storeList[0].location.latitude
  425. const longitude = this.data.storeList[0].location.longitude
  426. wx.openLocation({
  427. latitude,
  428. longitude,
  429. scale: 18
  430. })
  431. }
  432. })