index.js 13 KB

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