cityList.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. }else{
  82. uni.reLaunch({
  83. url:`${this.backUrl}?cityName=${city}&key=${this.key}&checkInDate=${this.checkInDate}&checkOutDate=${this.checkOutDate}`
  84. })
  85. }
  86. }else{
  87. uni.reLaunch({
  88. url:'/hotel/index?cityName='+city
  89. })
  90. }
  91. },
  92. loadMore(){
  93. this.getCityList()
  94. },
  95. getCityList() {
  96. this.status = 'loading'
  97. getCityList(this.query).then(res=>{
  98. if(res.state == 'Success'){
  99. this.list = this.list.concat(res.content.content)
  100. if(this.list.length == res.content.totalElements){
  101. this.status = 'noMore'
  102. }else{
  103. this.status = 'more'
  104. this.query.page++
  105. }
  106. }
  107. })
  108. },
  109. // 获取当前城市
  110. getCity(){
  111. let that = this
  112. that.city = '定位中...'
  113. return new Promise((resolve,reject)=>{
  114. uni.getLocation({
  115. type: 'gcj02',
  116. success: (res) => {
  117. // 解析地址
  118. // that.query['location.lat'] = res.latitude
  119. // that.query['location.lon'] = res.longitude
  120. // 存储经纬度
  121. uni.setStorageSync('location',JSON.stringify({latitude:res.latitude,longitude:res.longitude}))
  122. queryFromLocation({
  123. coordType:'gcj02ll',
  124. lat: res.latitude,
  125. lng: res.longitude
  126. }).then(res=>{
  127. console.log('解析结果',res);
  128. if(res.state == 'Success'){
  129. that.city = res.content.geoAddressComponent.city.replace('市','')
  130. }
  131. })
  132. },
  133. fail: () => {
  134. console.log("获取经纬度失败");
  135. },
  136. })
  137. })
  138. },
  139. },
  140. onLoad(options) {
  141. this.backUrl = options.backUrl
  142. this.checkInDate=options.checkInDate,
  143. this.checkOutDate=options.checkOutDate,
  144. this.key=options.key
  145. this.getCityList()
  146. this.getCity()
  147. }
  148. }
  149. </script>
  150. <style lang="scss" >
  151. .cityList{
  152. background: #fff;
  153. .search-box{
  154. position: fixed;
  155. top: 0%;
  156. left: 0%;
  157. padding: 0 24rpx ;
  158. background: #fff;
  159. .search{
  160. width: 702rpx;
  161. height: 72rpx;
  162. text-indent: 20rpx;
  163. background: #F6F6F6;
  164. border-radius: 36rpx;
  165. box-sizing: border-box;
  166. padding: 0 40rpx;
  167. }
  168. }
  169. .current-address{
  170. display: flex;
  171. align-items: center;
  172. border-radius: 8rpx 8rpx 8rpx 8rpx;
  173. border: 2rpx solid #F0F0F0;
  174. margin-top: 90rpx;
  175. line-height: 52rpx;
  176. padding: 0 20rpx;
  177. width: fit-content;
  178. margin-left: 24rpx;
  179. .icon{
  180. width: 30rpx;
  181. height: 30rpx;
  182. }
  183. .city{
  184. font-size: 28rpx;
  185. color: #222222;
  186. margin-left: 6rpx;
  187. }
  188. }
  189. .zs-list{
  190. .item{
  191. padding: 28rpx 0;
  192. font-size: 24rpx;
  193. color: #222222;
  194. border-bottom: 1rpx solid #F0F0F0;
  195. margin: 0 24rpx;
  196. }
  197. }
  198. }
  199. </style>