cityList.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <view class="cityList">
  3. <view class="search-box">
  4. <input class="search" v-model="query.name" type="text" placeholder="搜索城市名称" />
  5. </view>
  6. <view class="current-address" @click="handleCurrent">
  7. <image class="icon" src="@/static/blue-position.png" mode=""></image>
  8. <view class="city">
  9. {{city}}
  10. </view>
  11. </view>
  12. <zs-list class="list" mt="0" @load="loadMore" :status="status">
  13. <view class="item" v-for="item in list" :key="item.code" @click="handleCity(item.name,item.code)">
  14. {{item.name}}
  15. </view>
  16. </zs-list>
  17. </view>
  18. </template>
  19. <script>
  20. import {debounce} from '@/utils/tool.js'
  21. import {getCityList} from '@/api/common'
  22. // 腾讯地图
  23. var QQMapWX = require('../libs/qqmap-wx-jssdk.min.js');
  24. var qqmapsdk = new QQMapWX({key:'KX5BZ-B64RC-RO62W-AMWAZ-VVTC3-YAFXF'});
  25. export default {
  26. data() {
  27. return {
  28. query:{
  29. name:'',
  30. page:0,
  31. size:30,
  32. },
  33. list:[],
  34. status:'more',
  35. backUrl:'',
  36. checkInDate:'',
  37. checkOutDate:'',
  38. key:'',
  39. address:'定位中',
  40. city:'定位中',
  41. }
  42. },
  43. watch: {
  44. 'query.name':debounce(function(val) {
  45. this.status = 'more'
  46. this.query.page = 0
  47. this.list = []
  48. this.getCityList()
  49. })
  50. },
  51. methods: {
  52. handleCurrent(){
  53. let code
  54. this.list.map(item=>{
  55. if(item.name.indexOf(this.city) != -1){
  56. code = item.code
  57. }
  58. })
  59. if(this.backUrl){
  60. if(this.backUrl == '/pages/index/index'){
  61. uni.setStorageSync('HomeCity',this.city)
  62. uni.reLaunch({
  63. url:`${this.backUrl}?&regionCode=${code}`
  64. })
  65. }else{
  66. uni.reLaunch({
  67. url:`${this.backUrl}?cityName=${this.city}&key=${this.key}&checkInDate=${this.checkInDate}&checkOutDate=${this.checkOutDate}`
  68. })
  69. }
  70. }else{
  71. uni.reLaunch({
  72. url:'/hotel/index?cityName='+this.city
  73. })
  74. }
  75. },
  76. handleCity(name,code){
  77. let city = name.replace('市','')
  78. if(this.backUrl){
  79. if(this.backUrl == '/pages/index/index'){
  80. uni.setStorageSync('HomeCity',city)
  81. uni.reLaunch({
  82. url:`${this.backUrl}?&regionCode=${code}`
  83. })
  84. }else{
  85. uni.reLaunch({
  86. url:`${this.backUrl}?cityName=${city}&key=${this.key}&checkInDate=${this.checkInDate}&checkOutDate=${this.checkOutDate}`
  87. })
  88. }
  89. }else{
  90. uni.reLaunch({
  91. url:'/hotel/index?cityName='+city
  92. })
  93. }
  94. },
  95. loadMore(){
  96. this.getCityList()
  97. },
  98. getCityList() {
  99. this.status = 'loading'
  100. getCityList(this.query).then(res=>{
  101. if(res.state == 'Success'){
  102. this.list = this.list.concat(res.content.content)
  103. if(this.list.length == res.content.totalElements){
  104. this.status = 'noMore'
  105. }else{
  106. this.status = 'more'
  107. this.query.page++
  108. }
  109. }
  110. })
  111. },
  112. // 获取当前城市
  113. getCity(){
  114. let that = this
  115. that.city = '定位中...'
  116. return new Promise((resolve,reject)=>{
  117. uni.getLocation({
  118. type: 'gcj02',
  119. success: (res) => {
  120. // 解析地址
  121. // that.query['location.lat'] = res.latitude
  122. // that.query['location.lon'] = res.longitude
  123. // 存储经纬度
  124. uni.setStorageSync('location',JSON.stringify({latitude:res.latitude,longitude:res.longitude}))
  125. qqmapsdk.reverseGeocoder({
  126. location: {
  127. latitude: res.latitude,
  128. longitude: res.longitude
  129. },
  130. success: function(res) {
  131. // that.address = res.result.formatted_addresses.recommend
  132. that.city = res.result.address_component.city.replace('市','')
  133. console.log(9999,res);
  134. },
  135. fail: function(res) {
  136. that.city = '定位失败'
  137. },
  138. })
  139. },
  140. fail: () => {
  141. console.log("获取经纬度失败");
  142. },
  143. })
  144. })
  145. },
  146. },
  147. onLoad(options) {
  148. this.backUrl = options.backUrl
  149. this.checkInDate=options.checkInDate,
  150. this.checkOutDate=options.checkOutDate,
  151. this.key=options.key
  152. this.getCityList()
  153. this.getCity()
  154. }
  155. }
  156. </script>
  157. <style lang="scss" >
  158. .cityList{
  159. background: #fff;
  160. .search-box{
  161. position: fixed;
  162. top: 0%;
  163. left: 0%;
  164. padding: 0 24rpx ;
  165. background: #fff;
  166. .search{
  167. width: 702rpx;
  168. height: 72rpx;
  169. text-indent: 20rpx;
  170. background: #F6F6F6;
  171. border-radius: 36rpx;
  172. box-sizing: border-box;
  173. padding: 0 40rpx;
  174. }
  175. }
  176. .current-address{
  177. display: flex;
  178. align-items: center;
  179. border-radius: 8rpx 8rpx 8rpx 8rpx;
  180. border: 2rpx solid #F0F0F0;
  181. margin-top: 90rpx;
  182. line-height: 52rpx;
  183. padding: 0 20rpx;
  184. width: fit-content;
  185. margin-left: 24rpx;
  186. .icon{
  187. width: 30rpx;
  188. height: 30rpx;
  189. }
  190. .city{
  191. font-size: 28rpx;
  192. color: #222222;
  193. margin-left: 6rpx;
  194. }
  195. }
  196. .zs-list{
  197. .item{
  198. padding: 28rpx 0;
  199. font-size: 24rpx;
  200. color: #222222;
  201. border-bottom: 1rpx solid #F0F0F0;
  202. margin: 0 24rpx;
  203. }
  204. }
  205. }
  206. </style>