index.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /**
  32. * 生命周期函数--监听页面加载
  33. */
  34. onLoad: function (options) {
  35. // 计算
  36. const systemInfo = wx.getSystemInfoSync();
  37. const statusBarHeight = systemInfo.statusBarHeight || 0;
  38. const screenHeight = systemInfo.screenHeight || 0;
  39. const custom = wx.getMenuButtonBoundingClientRect();
  40. const customHeight = custom.height + 10 + 2 || 0;
  41. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  42. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  43. if (bottomNavHeight) {
  44. const padding_bottom = bottomNavHeight * 750 / systemInfo.windowWidth
  45. this.setData({
  46. bottomNavHeight: padding_bottom
  47. })
  48. console.log(this.data.bottomNavHeight);
  49. } else {
  50. this.setData({
  51. bottomNavHeight: 40
  52. })
  53. }
  54. this.setData({
  55. statusBarHeight,
  56. screenHeight,
  57. customHeight,
  58. contentHeight
  59. });
  60. const {
  61. objectId,
  62. date_start,
  63. date_end,
  64. daysBetween,
  65. istoday
  66. } = options;
  67. // 解码接收到的参数
  68. const decodedDateStart = decodeURIComponent(date_start);
  69. const decodedDateEnd = decodeURIComponent(date_end);
  70. this.setData({
  71. decodedDateStart,
  72. decodedDateEnd,
  73. objectId,
  74. daysBetween,
  75. istoday
  76. })
  77. console.log('istoday', this.data.istoday);
  78. this.gethomestay()
  79. this.getroom()
  80. this.getpic()
  81. },
  82. /**
  83. * 生命周期函数--监听页面初次渲染完成
  84. */
  85. onReady: function() {
  86. },
  87. /**
  88. * 生命周期函数--监听页面显示
  89. */
  90. onShow: function () {
  91. },
  92. /**
  93. * 生命周期函数--监听页面隐藏
  94. */
  95. onHide: function () {
  96. },
  97. /**
  98. * 生命周期函数--监听页面卸载
  99. */
  100. onUnload: function () {
  101. },
  102. /**
  103. * 页面相关事件处理函数--监听用户下拉动作
  104. */
  105. onPullDownRefresh: function () {
  106. },
  107. /**
  108. * 页面上拉触底事件的处理函数
  109. */
  110. onReachBottom: function () {
  111. },
  112. /**
  113. * 用户点击右上角分享
  114. */
  115. onShareAppMessage: function () {
  116. },
  117. //随轮播图变化而变化
  118. onSwiperChange: function (event) {
  119. const currentIndex = event.detail.current; // 获取当前索引
  120. this.setData({
  121. index: currentIndex + 1
  122. })
  123. },
  124. //获取名宿信息
  125. async gethomestay() {
  126. let ShopStore = new Parse.Query('ShopStore');
  127. ShopStore.equalTo('company', company);
  128. ShopStore.equalTo('type', "stay");
  129. ShopStore.equalTo('objectId', this.data.objectId);
  130. ShopStore.notEqualTo('isDeleted', "true");
  131. ShopStore.include('location');
  132. let store = await ShopStore.find();
  133. let storeListPromises = store.map(async item => {
  134. let storeItem = item.toJSON();
  135. storeItem.iscollect = await this.iscollect(storeItem.objectId); // 等待iscollect的结果
  136. return storeItem;
  137. });
  138. let storeList = await Promise.all(storeListPromises); // 等待所有的Promise完成
  139. this.setData({
  140. storeList
  141. });
  142. this.Getlocation()
  143. console.log(this.data.storeList);
  144. },
  145. //获取房间信息
  146. async getroom() {
  147. let room = new Parse.Query('ShopRoom');
  148. room.equalTo('company', company);
  149. room.equalTo('shop', this.data.objectId);
  150. room.equalTo('isEnabled', 'true');
  151. room.include('benefitMap')
  152. room.notEqualTo('isDeleted', 'true');
  153. let room2 = await room.find();
  154. let roomList = room2.map(item => item.toJSON());
  155. // 对 roomList 进行排序
  156. roomList.sort((a, b) => {
  157. // 先比较 remaining,remaining 为 0 的排后面
  158. if (a.remaining === 0 && b.remaining !== 0) {
  159. return 1; // a 排后面
  160. }
  161. if (a.remaining !== 0 && b.remaining === 0) {
  162. return -1; // a 排前面
  163. }
  164. // 如果两个房间的 remaining 都不为 0,按数量升序排列
  165. return a.remaining - b.remaining;
  166. });
  167. this.setData({
  168. roomList
  169. })
  170. console.log('房间', this.data.roomList);
  171. },
  172. //收藏功能
  173. async iscollect(object) {
  174. const currentUser = Parse.User.current();
  175. let Collect = new Parse.Query('DramaShopCollect');
  176. Collect.equalTo('company', company);
  177. Collect.equalTo('user', currentUser.id);
  178. Collect.equalTo('homestayStore', object);
  179. Collect.equalTo('isCollect', 'true');
  180. Collect.notEqualTo('isDeleted', "true");
  181. let collect = await Collect.first();
  182. if (collect) {
  183. return true
  184. } else {
  185. return false
  186. }
  187. },
  188. //获取轮播图
  189. async getpic() {
  190. let Banner = new Parse.Query('Banner');
  191. Banner.equalTo('company', company);
  192. Banner.equalTo('store', this.data.objectId);
  193. Banner.equalTo('isEnabled', 'true');
  194. Banner.notEqualTo('isDeleted', 'true');
  195. Banner.select('image');
  196. let Banner2 = await Banner.find();
  197. // 提取 image 属性并存储到 imageUrls 中
  198. let imageUrls = Banner2.map(item => item.get('image')); // 使用 get() 方法获取 image 属性
  199. this.setData({
  200. imageUrls // 将提取的 imageUrls 存储到组件状态中
  201. });
  202. console.log(this.data.imageUrls); // 输出 imageUrls
  203. },
  204. //获取当前位置信息
  205. Getlocation() {
  206. // 假设 this.storeList[0].location 是一个 Parse.GeoPoint 对象
  207. const storeLocation = this.data.storeList[0].location;
  208. // 从 GeoPoint 对象中获取经纬度
  209. const latitude = storeLocation.latitude; // 纬度
  210. const longitude = storeLocation.longitude; // 经度
  211. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  212. // 调用 API 解析地址
  213. wx.request({
  214. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  215. data: {},
  216. header: {
  217. 'Content-Type': 'application/json'
  218. },
  219. success: (ops) => { // 使用箭头函数
  220. console.log(ops);
  221. const address = ops.data.result.formatted_address;
  222. this.setData({
  223. address: address,
  224. latitude: latitude, // 保证 latitude 被设置
  225. longitude: longitude, // 保证 longitude 被设置
  226. markers: [{ // 设置 markers
  227. id: 1,
  228. latitude: latitude,
  229. longitude: longitude,
  230. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  231. width: 30,
  232. height: 30,
  233. callout: {
  234. content: address, // 可以显示解析出的地址
  235. color: '#ffffff',
  236. bgColor: '#7F56B2',
  237. padding: 10,
  238. borderRadius: 5,
  239. display: 'ALWAYS'
  240. }
  241. }]
  242. });
  243. console.log(this.data.address);
  244. },
  245. fail: function (resq) {
  246. wx.showModal({
  247. title: '信息提示',
  248. content: '请求失败',
  249. showCancel: false,
  250. confirmColor: '#f37938'
  251. });
  252. },
  253. complete: function () {}
  254. });
  255. },
  256. })