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