cityList.vue 4.9 KB

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