index.js 11 KB

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