home.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <template>
  2. <view class="container">
  3. <!-- 搜索框区域 -->
  4. <view class="content">
  5. <uv-row custom-style="margin: 10px 0px" gutter="10">
  6. <picker @change="bindPickerChange" range-key="name" :value="placeIndex" :range="placeList">
  7. <view class="address">
  8. <view class="address_text">{{ placeList[placeIndex].name }}</view>
  9. <img src="../../static/index/bottom.png" />
  10. </view>
  11. </picker>
  12. <view class="search">
  13. <view class="add">
  14. <image class="img" src="../../static/index/search.png" mode="aspectFit"></image>
  15. </view>
  16. <input class="inp" type="text" v-model="keywords" placeholder="请输入关键字搜索" />
  17. <view class="btnSearch" @click="searchHandler">搜索</view>
  18. </view>
  19. </uv-row>
  20. </view>
  21. <view class="main">
  22. <!-- 添加一层view解决 css属性 sticky 不生效问题 -->
  23. <view>
  24. <!-- 分段器区域 -->
  25. <view class="control" :class="{ sticky: isSticky }">
  26. <uni-segmented-control :current="current" :values="items" style-type="text" active-color="#096562" @clickItem="onClickItem" />
  27. </view>
  28. <!-- 民宿列表 -->
  29. <view class="box" v-if="hotelList.length">
  30. <!-- 列表区域 -->
  31. <view class="box_body">
  32. <!-- 每一个民宿区域 -->
  33. <view class="box_item" v-for="item in hotelList" :key="item.id" @click="goPageDetail(item)">
  34. <img mode="aspectFill" :src="item.coverImg" />
  35. <view class="item_name">{{ item.hotel_name }}</view>
  36. <view class="item_rate">
  37. <view class="rate_left">{{ item.scoreHotel || '5.0' }}</view>
  38. </view>
  39. <view class="item_distance" v-if="item.distance">距您直线{{ item.distance }}公里</view>
  40. <view class="item_info">
  41. <view class="info_count">剩{{ item.roomNumber }}间</view>
  42. <view class="info_price">
  43. ¥{{ item.min_price ? item.min_price : 0 }}
  44. <text>起</text>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. <!-- 没有数据时展示的页面 -->
  51. <view class="noData" v-else>
  52. <img src="../../static/images/noData.png" />
  53. 暂无数据
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. export default {
  61. data() {
  62. return {
  63. // 分段器数组
  64. items: ['白金级', '金宿级', '银宿级'],
  65. // 当前索引
  66. current: 0,
  67. // 地区数组
  68. placeList: [
  69. {
  70. name: '靖安县'
  71. }
  72. ],
  73. // 当前选择地区索引
  74. placeIndex: 0,
  75. // 搜索框绑定数据
  76. keywords: '',
  77. // 是否显示距离差
  78. showdDistance: false,
  79. // 用户定位经度
  80. myLng: 0,
  81. // 用户定位纬度
  82. myLat: 0,
  83. // 当前页
  84. page: 1,
  85. // 每页多少条
  86. rows: 6,
  87. // 总条数
  88. total: null,
  89. // 民宿列表数组
  90. hotelList: [],
  91. // 当前乡镇
  92. town: '',
  93. // 分段器是否吸顶
  94. isSticky: false
  95. }
  96. },
  97. onLoad(options) {
  98. // console.log(options)
  99. this.keywords = options.keywords
  100. if (!options.level || options.level === '白金级') {
  101. this.current = 0
  102. } else if (options.level === '金宿级') {
  103. this.current = 1
  104. } else if (options.level === '银宿级') {
  105. this.current = 2
  106. }
  107. uni.setNavigationBarTitle({
  108. title: options.town || '靖安县'
  109. })
  110. this.getTownList(options.town)
  111. },
  112. onReachBottom() {
  113. if (this.hotelList.length < this.total) {
  114. this.page++
  115. this.getHotelList()
  116. } else {
  117. uni.showToast({
  118. title: '没有更多数据了',
  119. icon: 'none'
  120. })
  121. }
  122. },
  123. // 页面下拉刷新回调
  124. onPullDownRefresh() {
  125. setTimeout(() => {
  126. this.hotelList = []
  127. this.page = 1
  128. this.getLocation()
  129. uni.stopPullDownRefresh()
  130. }, 2000)
  131. },
  132. methods: {
  133. // 获取乡镇集合
  134. async getTownList(town) {
  135. const res = await this.$myRequest({
  136. url: '/mhotel/hotelqueryList.action',
  137. method: 'get',
  138. data: {
  139. code: 10
  140. }
  141. })
  142. // console.log(res)
  143. if (res.code === 200) {
  144. this.placeList = [...this.placeList, ...res.data]
  145. let temindex
  146. this.placeList.forEach((ele, index) => {
  147. if (ele.name === town) {
  148. temindex = index
  149. }
  150. })
  151. this.placeIndex = temindex || 0
  152. this.getLocation()
  153. // console.log(this.placeIndex)
  154. }
  155. },
  156. // 获取用户当前位置
  157. getLocation() {
  158. uni.getSetting({
  159. success: (res) => {
  160. if (!res.authSetting['scope.userLocation']) {
  161. uni.authorize({
  162. scope: 'scope.userLocation',
  163. success: (res) => {
  164. // 授权成功
  165. uni.getLocation({
  166. type: 'gcj02',
  167. success: (res) => {
  168. this.myLat = res.latitude
  169. this.myLng = res.longitude
  170. this.showdDistance = true
  171. this.getHotelList()
  172. }
  173. })
  174. },
  175. fail: () => {
  176. uni.showModal({
  177. content: '获取定位权限失败将会影响使用部分功能,是否去设置打开?',
  178. confirmText: '确认',
  179. cancelText: '取消',
  180. success: (res) => {
  181. if (res.confirm) {
  182. uni.openSetting({
  183. success: (res) => {
  184. console.log(res)
  185. this.getLocation()
  186. }
  187. })
  188. } else {
  189. this.showdDistance = false
  190. this.getHotelList()
  191. uni.showToast({
  192. title: '获取定位权限失败',
  193. icon: 'none'
  194. })
  195. }
  196. }
  197. })
  198. }
  199. })
  200. } else {
  201. uni.getLocation({
  202. type: 'gcj02',
  203. success: (res) => {
  204. this.myLat = res.latitude
  205. this.myLng = res.longitude
  206. this.showdDistance = true
  207. this.getHotelList()
  208. }
  209. })
  210. }
  211. }
  212. })
  213. },
  214. // 获取民宿列表
  215. async getHotelList() {
  216. let type
  217. if (this.current === 0) {
  218. type = 3
  219. } else if (this.current === 1) {
  220. type = 2
  221. } else if (this.current === 2) {
  222. type = 1
  223. }
  224. const res = await this.$myRequest({
  225. url: '/mhotel/ahphomePage.action',
  226. data: {
  227. queryValue: this.keywords,
  228. page: this.page,
  229. rows: this.rows,
  230. hotel_township: this.placeList[this.placeIndex].id || '',
  231. userId: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').id : '',
  232. type
  233. }
  234. })
  235. // console.log(res)
  236. if (res.code === 200) {
  237. this.hotelList = [...this.hotelList, ...res.data.pageList]
  238. this.total = res.data.total
  239. // 如果定位成功则获取和民宿之间的距离
  240. if (this.showdDistance && this.hotelList.length) {
  241. this.hotelList.forEach((ele) => {
  242. let lat = ele.hpositionWens.split(',')[0]
  243. let lng = ele.hpositionWens.split(',')[1]
  244. ele.distance = this.calculateDistance(lat, lng)
  245. })
  246. }
  247. }
  248. },
  249. // 点击每一个民宿卡片回调
  250. goPageDetail(item) {
  251. uni.navigateTo({
  252. url: `/pages/detail/detail?id=${item.id}&distance=${item.distance}&town=${item.hotelTownshipName}`
  253. })
  254. },
  255. // 选择地区时的回调
  256. bindPickerChange(e) {
  257. this.placeIndex = e.detail.value
  258. uni.setNavigationBarTitle({
  259. title: this.placeList[this.placeIndex].name
  260. })
  261. this.hotelList = []
  262. this.page = 1
  263. this.getHotelList()
  264. },
  265. // 搜索按钮点击回调
  266. searchHandler() {
  267. this.hotelList = []
  268. this.page = 1
  269. this.getHotelList()
  270. },
  271. onClickItem(e) {
  272. if (this.current !== e.currentIndex) {
  273. this.current = e.currentIndex
  274. this.hotelList = []
  275. this.page = 1
  276. this.getHotelList()
  277. }
  278. },
  279. // 计算两个点之间的距离
  280. calculateDistance(lat, lng) {
  281. let centerLat = lat
  282. let centerLng = lng
  283. let red1 = (this.myLat * Math.PI) / 180.0
  284. let red2 = (centerLat * Math.PI) / 180.0
  285. let a = red1 - red2
  286. let b = (this.myLng * Math.PI) / 180.0 - (centerLng * Math.PI) / 180.0
  287. let R = 6378137
  288. let distance = R * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(red1) * Math.cos(red2) * Math.pow(Math.sin(b / 2), 2)))
  289. let res = (distance / 1000).toFixed(2) * 1
  290. return res
  291. }
  292. }
  293. }
  294. </script>
  295. <style lang="scss" scoped>
  296. .container {
  297. display: flex;
  298. flex-direction: column;
  299. width: 750rpx;
  300. min-height: 100vh;
  301. padding: 0 30rpx;
  302. box-sizing: border-box;
  303. overflow-y: auto;
  304. background-color: #f7f7f7;
  305. .content {
  306. position: sticky;
  307. background-color: #f7f7f7;
  308. .address {
  309. display: flex;
  310. width: 152rpx;
  311. font-size: 28rpx;
  312. .address_text {
  313. width: 104rpx;
  314. text-align: center;
  315. overflow: hidden;
  316. white-space: nowrap;
  317. text-overflow: ellipsis;
  318. }
  319. img {
  320. width: 48rpx;
  321. height: 48rpx;
  322. }
  323. }
  324. .search {
  325. display: flex;
  326. justify-content: space-between;
  327. align-items: center;
  328. width: 538rpx;
  329. height: 80rpx;
  330. opacity: 1;
  331. border-radius: 70px;
  332. background-color: #fff;
  333. .add {
  334. display: flex;
  335. justify-content: center;
  336. align-items: center;
  337. margin-left: 10rpx;
  338. width: 60rpx;
  339. font-size: 50rpx;
  340. height: 60rpx;
  341. line-height: 60rpx;
  342. color: rgba(30, 125, 251, 1);
  343. .img {
  344. width: 30rpx;
  345. height: 30rpx;
  346. }
  347. }
  348. .inp {
  349. height: 60rpx;
  350. line-height: 60rpx;
  351. flex-grow: 1;
  352. font-size: 28rpx;
  353. }
  354. .btnSearch {
  355. width: 100rpx;
  356. text-align: center;
  357. margin-right: 10rpx;
  358. height: 60rpx;
  359. line-height: 60rpx;
  360. opacity: 1;
  361. font-size: 28rpx;
  362. font-weight: 400;
  363. height: 2rem;
  364. color: #096562;
  365. }
  366. }
  367. }
  368. .main {
  369. .icons {
  370. display: flex;
  371. justify-content: space-around;
  372. align-items: center;
  373. height: 158rpx;
  374. border-radius: 12rpx;
  375. background-color: #fff;
  376. .icon_item {
  377. display: flex;
  378. flex-direction: column;
  379. align-items: center;
  380. width: 20%;
  381. .item_top {
  382. display: flex;
  383. align-items: center;
  384. justify-content: center;
  385. width: 60rpx;
  386. height: 60rpx;
  387. border-radius: 50%;
  388. img {
  389. width: 28rpx;
  390. height: 27rpx;
  391. }
  392. .img2 {
  393. width: 40rpx;
  394. height: 25.5rpx;
  395. }
  396. .img3 {
  397. width: 31rpx;
  398. height: 21rpx;
  399. }
  400. }
  401. .color {
  402. background-color: #d8e8e8;
  403. }
  404. .color2 {
  405. background-color: #c2f2d5;
  406. }
  407. .color3 {
  408. background-color: #f7d4ba;
  409. }
  410. .color4 {
  411. background-color: #fae1e2;
  412. }
  413. .color5 {
  414. background-color: #d0e0f2;
  415. }
  416. .item_bottom {
  417. margin-top: 6rpx;
  418. font-size: 28rpx;
  419. }
  420. }
  421. }
  422. .control {
  423. margin: 20rpx 0;
  424. height: 90rpx;
  425. border-radius: 8rpx;
  426. background-color: #fff;
  427. }
  428. .sticky {
  429. position: sticky;
  430. top: 0;
  431. border-radius: 0;
  432. }
  433. .box {
  434. .box_body {
  435. display: flex;
  436. flex-wrap: wrap;
  437. justify-content: space-between;
  438. .box_item {
  439. margin-bottom: 20rpx;
  440. width: 335rpx;
  441. height: 446rpx;
  442. border-radius: 10rpx;
  443. background-color: #fff;
  444. img {
  445. width: 335rpx;
  446. height: 223rpx;
  447. border-radius: 10rpx 10rpx 0 0;
  448. }
  449. .item_name {
  450. margin-left: 22rpx;
  451. margin-top: 10rpx;
  452. line-height: 55rpx;
  453. font-size: 28rpx;
  454. font-weight: bold;
  455. overflow: hidden;
  456. text-overflow: ellipsis;
  457. white-space: nowrap;
  458. }
  459. .item_rate {
  460. display: flex;
  461. align-items: center;
  462. margin-left: 22rpx;
  463. font-size: 24rpx;
  464. .rate_left {
  465. padding: 0 12rpx;
  466. color: #fff;
  467. // border-radius: 32rpx 0 0 32rpx;
  468. border-radius: 32rpx;
  469. background-color: #096562;
  470. }
  471. .rate_right {
  472. padding: 0 16rpx 0 7rpx;
  473. color: #096562;
  474. border-radius: 0 32rpx 32rpx 0;
  475. background-color: #dff2f2;
  476. }
  477. }
  478. .item_distance {
  479. margin-top: 10rpx;
  480. margin-left: 22rpx;
  481. color: #a6a6a6;
  482. font-size: 24rpx;
  483. }
  484. .item_info {
  485. box-sizing: border-box;
  486. padding: 0 30rpx 0 25rpx;
  487. margin-top: 16rpx;
  488. display: flex;
  489. justify-content: space-between;
  490. color: #ff5733;
  491. .info_count {
  492. font-size: 24rpx;
  493. }
  494. .info_price {
  495. margin-top: -5rpx;
  496. font-size: 36rpx;
  497. font-weight: bold;
  498. text {
  499. color: #a6a6a6;
  500. font-size: 24rpx;
  501. font-weight: 400;
  502. }
  503. }
  504. }
  505. }
  506. }
  507. }
  508. .noData {
  509. display: flex;
  510. flex-direction: column;
  511. justify-content: center;
  512. align-items: center;
  513. img {
  514. margin-top: 150rpx;
  515. width: 400rpx;
  516. height: 400rpx;
  517. }
  518. }
  519. }
  520. }
  521. </style>