index.js 14 KB

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