city.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <view>
  3. <!-- 左边内容 -->
  4. <!-- <scroll-view class="scroller" :scroll-into-view="toView" scroll-y="true" scroll-with-animation="true">
  5. <view :id="item" class="left" v-for="item in list">{{item.toUpperCase()}}</view>
  6. </scroll-view> -->
  7. <!-- 右边索引 -->
  8. <!-- <view>
  9. <view :data-id="item" v-for="item in list" @tap="bindToView">{{item}}</view>
  10. </view> -->
  11. <!-- 中间主体内容 -->
  12. <view class="content">
  13. <scroll-view scroll-y="true" style="width: 100%;height: 100%;" :scroll-into-view="toView"
  14. scroll-with-animation="true">
  15. <view class="content-title">
  16. 选择城市
  17. </view>
  18. <view class="content-search">
  19. <u-search placeholder="搜索城市名" v-model="keyword" @change="getCitysList"
  20. :show-action="false"></u-search>
  21. </view>
  22. <view class="content-map">
  23. <view class="flex align-center justify-between" @click="getLatOrLng">
  24. 定位城市
  25. <view class="" style="font-size: 24rpx;font-weight: 500;color: #999999;">
  26. <u-icon name="map" color="#999999" style="margin-right: 10rpx;" size="24"></u-icon>重新定位
  27. </view>
  28. </view>
  29. <view class="content-map-address flex justify-center align-center" @click="selectCity(city)">
  30. {{city}}
  31. </view>
  32. </view>
  33. <view class="content-map flex justify-between flex-wrap" v-if="rmCity.length>0">
  34. <view class="" style="width: 100%;">
  35. 热门城市
  36. </view>
  37. <view class="content-map-address flex justify-center align-center" @click="selectCity(item)"
  38. v-for="(item,index) in rmCity" :key="index">
  39. {{item}}
  40. </view>
  41. <view class="content-map-address flex justify-center align-center" style="height: 0;">
  42. </view>
  43. </view>
  44. <view :id="item.letter" class="content-map flex justify-between flex-wrap"
  45. v-for="(item,index) in cityList" v-if="item.city.length>0" :key="index">
  46. <view class="" style="width: 100%;">
  47. {{item.letter.toUpperCase()}}
  48. </view>
  49. <view class="content-map-address flex justify-center align-center" @click="selectCity(ite)"
  50. v-for="(ite,ind) in item.city" :key="ind">
  51. {{ite}}
  52. </view>
  53. <view class="content-map-address flex justify-center align-center" style="height: 0;">
  54. </view>
  55. </view>
  56. </scroll-view>
  57. </view>
  58. <view class="left flex justify-center flex-wrap">
  59. <view :data-id="item" class="left-item" v-for="(item,index) in list" @tap="bindToView">
  60. {{item.toUpperCase()}}
  61. </view>
  62. </view>
  63. </view>
  64. </template>
  65. <script>
  66. export default {
  67. data() {
  68. return {
  69. searchList: [],
  70. cityList: [],
  71. rmCity: [],
  72. keyword: '',
  73. list: [],
  74. toView: '',
  75. city: '',
  76. latitude: '',
  77. longitude: '',
  78. }
  79. },
  80. onLoad() {
  81. this.getLatOrLng()
  82. this.getRmCityList();
  83. this.getCitysList()
  84. },
  85. methods: {
  86. /**
  87. * 热门城市列表
  88. */
  89. getRmCityList() {
  90. let data = {
  91. limitCount: 10,
  92. }
  93. this.$Request.get('/app/postPush/getHotCity', data).then(res => {
  94. if (res.code == 0) {
  95. this.rmCity = res.data
  96. }
  97. })
  98. },
  99. /**
  100. * 获取城市列表
  101. */
  102. getCitysList() {
  103. let data = {
  104. city: this.keyword
  105. }
  106. this.$Request.get('/app/postPush/getCityList', data).then(res => {
  107. if (res.code == 0) {
  108. this.cityList = res.data
  109. let arr = res.data
  110. let rightList = []
  111. arr.map(item => {
  112. rightList.push(item.letter)
  113. })
  114. this.list = rightList
  115. }
  116. });
  117. },
  118. /**
  119. * 定位获取经纬度
  120. */
  121. getLatOrLng() {
  122. let that = this
  123. uni.getLocation({
  124. type: 'wgs84', //wgs84 gcj02
  125. success: function(res) {
  126. console.log(res, '地理位置')
  127. that.latitude = res.latitude
  128. that.longitude = res.longitude
  129. // #ifdef APP-PLUS
  130. if (res.address) {
  131. that.city = res.address.city
  132. } else {
  133. that.getSelectCity(that.longitude, that.latitude);
  134. }
  135. // #endif
  136. // #ifndef APP
  137. that.getSelectCity(that.longitude, that.latitude);
  138. // #endif
  139. },
  140. fail: function(err) {
  141. console.log('获取地址失败', err)
  142. }
  143. })
  144. },
  145. /**
  146. * @param {Object} longitude
  147. * @param {Object} latitude
  148. * 使用经纬度获取城市
  149. */
  150. getSelectCity(longitude, latitude) {
  151. console.log(longitude + '' + latitude, 'app请求经纬度');
  152. this.$Request.get('/app/Login/selectCity?lat=' + latitude + '&lng=' + longitude).then(res => {
  153. if (res.code == 0) {
  154. console.log(res, 'app定位请求')
  155. this.city = res.data.city ? res.data.city : '未知'
  156. }
  157. });
  158. },
  159. /**
  160. * @param {Object} city
  161. * 选择城市
  162. */
  163. selectCity(city) {
  164. uni.$emit('city', {
  165. city: city
  166. })
  167. uni.navigateBack()
  168. },
  169. bindToView(event) {
  170. var id = event.currentTarget.dataset.id;
  171. console.log(id)
  172. this.toView = id;
  173. }
  174. }
  175. }
  176. </script>
  177. <style lang="scss">
  178. .content {
  179. width: 686rpx;
  180. height: 100vh;
  181. position: fixed;
  182. right: 40rpx;
  183. /* #ifdef MP-WEIXIN */
  184. top: 0;
  185. /* #endif */
  186. /* #ifndef MP-WEIXIN */
  187. top: 76;
  188. /* #endif */
  189. .content-title {
  190. width: 100%;
  191. font-size: 38rpx;
  192. font-weight: bold;
  193. margin-top: 30rpx;
  194. }
  195. .content-search {
  196. width: 100%;
  197. height: 60rpx;
  198. border-radius: 24rpx;
  199. background-color: #F2F2F7;
  200. margin-top: 30rpx;
  201. }
  202. .content-map {
  203. width: 100%;
  204. margin-top: 30rpx;
  205. font-size: 30rpx;
  206. font-weight: bold;
  207. .content-map-address {
  208. margin-top: 20rpx;
  209. width: 200rpx;
  210. height: 70rpx;
  211. background-color: #F2F2F7;
  212. font-size: 26rpx;
  213. font-weight: 500;
  214. }
  215. }
  216. }
  217. .left {
  218. position: fixed;
  219. right: 0;
  220. top: 50%;
  221. transform: translate(0, -50%);
  222. width: 30rpx;
  223. .left-item {
  224. margin-top: 10rpx;
  225. padding-left: 10rpx;
  226. padding-right: 10rpx;
  227. color: #00B88F;
  228. }
  229. }
  230. </style>