index.js 13 KB

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