| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- <template>
- <view class="container">
- <!-- 搜索框区域 -->
- <uv-row custom-style="margin: 10px 0px" gutter="10">
- <picker @change="bindPickerChange" range-key="name" :value="placeIndex" :range="placeList">
- <view class="address">
- <view class="address_text">{{ placeList[placeIndex].name }}</view>
- <img src="../../static/index/bottom.png" />
- </view>
- </picker>
- <view class="search">
- <view class="add">
- <image class="img" src="../../static/index/search.png" mode="aspectFit"></image>
- </view>
- <input class="inp" type="text" v-model="keywords" placeholder="请输入关键字搜索" />
- <view class="btnSearch" @click="searchHandler">搜索</view>
- </view>
- </uv-row>
- <!-- 名宿列表区域 -->
- <view class="body" v-if="hotelList.length">
- <!-- 每一个名宿区域 -->
- <view class="item" v-for="item in hotelList" :key="item.id" @click="goPageDetail(item)">
- <image class="item-img" :src="item.coverImg" mode="scaleToFill"></image>
- <view class="descrition">
- <text class="title">{{ item.hname }}</text>
- <text class="type">{{ item.hTypeName }}</text>
- <text class="distance" v-if="showdDistance">距您直线{{ item.distance }}公里</text>
- <view class="detail">
- <img class="img" src="../../static/index/hotel.png" />
- <view class="price">
- <text class="txt1">¥{{ item.min_price }}</text>
- <text class="txt2">起</text>
- </view>
- </view>
- </view>
- </view>
- </view>
- <!-- 没有数据时展示的页面 -->
- <view class="noData" v-else>
- <img src="../../static/images/noData.png" />
- 暂无数据
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- // 是否显示距离差
- showdDistance: false,
- // 搜索框绑定数据
- keywords: '',
- // 民宿列表数组
- hotelList: [],
- // 地区数组
- placeList: [
- {
- name: '靖安县'
- }
- ],
- // 当前选择地区索引
- placeIndex: 0,
- // 当前页
- page: 1,
- // 每页多少条
- rows: 6,
- // 总条数
- total: null,
- // 用户定位经度
- myLng: 0,
- // 用户定位纬度
- myLat: 0
- }
- },
- onLoad() {
- this.getLocation()
- this.getTownList()
- },
- // 页面下拉回调
- onPullDownRefresh() {
- setTimeout(() => {
- this.hotelList = []
- this.page = 1
- this.getLocation()
- uni.stopPullDownRefresh()
- }, 2000)
- },
- // 页面下拉到底部回调
- onReachBottom() {
- if (this.hotelList.length < this.total) {
- this.page++
- this.getHotelList()
- } else {
- uni.showToast({
- title: '没有更多数据了',
- icon: 'none'
- })
- }
- },
- methods: {
- // 获取乡镇集合
- async getTownList() {
- const res = await this.$myRequest({
- url: '/mhotel/hotelqueryList.action',
- method: 'get',
- data: {
- code: 10
- }
- })
- // console.log(res)
- if (res.code === 200) {
- this.placeList = [...this.placeList, ...res.data]
- }
- },
- // 获取用户当前位置
- getLocation() {
- uni.getSetting({
- success: (res) => {
- if (!res.authSetting['scope.userLocation']) {
- uni.authorize({
- scope: 'scope.userLocation',
- success: (res) => {
- // 授权成功
- uni.getLocation({
- type: 'gcj02',
- success: (res) => {
- this.myLat = res.latitude
- this.myLng = res.longitude
- this.showdDistance = true
- this.getHotelList()
- }
- })
- },
- fail: () => {
- uni.showModal({
- content: '获取定位权限失败将会影响使用部分功能,是否去设置打开?',
- confirmText: '确认',
- cancelText: '取消',
- success: (res) => {
- if (res.confirm) {
- uni.openSetting({
- success: (res) => {
- console.log(res)
- this.getLocation()
- }
- })
- } else {
- this.showdDistance = false
- this.getHotelList()
- uni.showToast({
- title: '获取定位权限失败',
- icon: 'none'
- })
- }
- }
- })
- }
- })
- } else {
- uni.getLocation({
- type: 'gcj02',
- success: (res) => {
- this.myLat = res.latitude
- this.myLng = res.longitude
- this.showdDistance = true
- this.getHotelList()
- }
- })
- }
- }
- })
- },
- // 获取民宿列表
- async getHotelList() {
- const res = await this.$myRequest({
- url: '/mhotel/ahphomePage.action',
- data: {
- queryValue: this.keywords,
- page: this.page,
- rows: this.rows,
- hotel_township: this.placeList[this.placeIndex].id || ''
- }
- })
- // console.log(res)
- if (res.code === 200) {
- this.hotelList = [...this.hotelList, ...res.data.pageList]
- this.total = res.data.total
- // 如果定位成功则获取和民宿之间的距离
- if (this.showdDistance && this.hotelList.length) {
- this.hotelList.forEach((ele) => {
- let lat = ele.hpositionWens.split(',')[0]
- let lng = ele.hpositionWens.split(',')[1]
- ele.distance = this.calculateDistance(lat, lng)
- })
- }
- }
- },
- // 计算两个点之间的距离
- calculateDistance(lat, lng) {
- let centerLat = lat
- let centerLng = lng
- let red1 = (this.myLat * Math.PI) / 180.0
- let red2 = (centerLat * Math.PI) / 180.0
- let a = red1 - red2
- let b = (this.myLng * Math.PI) / 180.0 - (centerLng * Math.PI) / 180.0
- let R = 6378137
- 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)))
- let res = (distance / 1000).toFixed(2) * 1
- return res
- },
- // 搜索按钮点击回调
- searchHandler() {
- this.hotelList = []
- this.page = 1
- this.getHotelList()
- },
- // 点击每一个名宿卡片回调
- goPageDetail(item) {
- uni.navigateTo({
- url: `/pages/detail/detail?id=${item.id}&distance=${item.distance}`
- })
- },
- // 选择地区时的回调
- bindPickerChange(e) {
- this.placeIndex = e.detail.value
- this.hotelList = []
- this.page = 1
- this.getHotelList()
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .container {
- display: flex;
- flex-direction: column;
- width: 750rpx;
- min-height: 100vh;
- padding: 0 30rpx;
- box-sizing: border-box;
- background-color: #f7f7f7;
- .address {
- display: flex;
- width: 152rpx;
- font-size: 28rpx;
- .address_text {
- width: 104rpx;
- text-align: center;
- overflow: hidden;
- white-space: nowrap;
- text-overflow: ellipsis;
- }
- img {
- width: 48rpx;
- height: 48rpx;
- }
- }
- .search {
- display: flex;
- justify-content: space-between;
- align-items: center;
- width: 538rpx;
- height: 80rpx;
- opacity: 1;
- border-radius: 70px;
- background-color: #fff;
- .add {
- display: flex;
- justify-content: center;
- align-items: center;
- margin-left: 10rpx;
- width: 60rpx;
- font-size: 50rpx;
- height: 60rpx;
- line-height: 60rpx;
- color: rgba(30, 125, 251, 1);
- .img {
- width: 30rpx;
- height: 30rpx;
- }
- }
- .inp {
- height: 60rpx;
- line-height: 60rpx;
- flex-grow: 1;
- font-size: 28rpx;
- }
- .btnSearch {
- width: 100rpx;
- text-align: center;
- margin-right: 10rpx;
- height: 60rpx;
- line-height: 60rpx;
- opacity: 1;
- font-size: 28rpx;
- font-weight: 400;
- height: 2rem;
- color: #096562;
- }
- }
- .body {
- display: flex;
- justify-content: space-between;
- flex-wrap: wrap;
- .item {
- width: 335rpx;
- box-sizing: border-box;
- margin-bottom: 20rpx;
- .item-img {
- width: 100%;
- height: 223rpx;
- border-radius: 18rpx 18rpx 0 0;
- box-sizing: border-box;
- }
- .descrition {
- display: flex;
- flex-direction: column;
- width: 100%;
- border-radius: 0 0 18rpx 18rpx;
- box-sizing: border-box;
- background: rgba(255, 255, 255, 1);
- margin-top: -10rpx;
- .title {
- font-size: 28rpx;
- font-weight: 600;
- padding: 20rpx 20rpx 10rpx;
- color: rgba(0, 0, 0, 1);
- overflow: hidden;
- white-space: nowrap;
- text-overflow: ellipsis;
- }
- .type {
- padding: 5rpx 20rpx;
- height: 40rpx;
- font-size: 24rpx;
- color: #a6a6a6;
- }
- .distance {
- padding: 10rpx 20rpx;
- font-size: 24rpx;
- color: #a6a6a6;
- }
- .detail {
- display: flex;
- flex-direction: row;
- justify-content: space-between;
- align-items: center;
- padding: 0 20rpx 20rpx 20rpx;
- color: rgba(0, 0, 0, 1);
- .img {
- width: 40rpx;
- height: 40rpx;
- }
- .price {
- .txt1 {
- font-size: 36rpx;
- font-weight: 600;
- color: rgba(255, 87, 51, 1);
- }
- .txt2 {
- font-size: 24rpx;
- font-weight: 400;
- color: #a6a6a6;
- }
- }
- .score {
- font-size: 24rpx;
- font-weight: 400;
- padding-top: 10rpx;
- color: rgba(166, 166, 166, 1);
- }
- }
- }
- }
- }
- .noData {
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- img {
- margin-top: 150rpx;
- width: 600rpx;
- height: 600rpx;
- }
- }
- }
- </style>
|