chooseCity.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <view class="content">
  3. <!-- 城市列表 -->
  4. <scroll-view class="scroll-view" scroll-y scroll-with-animation="true" enable-back-to-top="true"
  5. :scroll-into-view="toIndex" >
  6. <view class="listContent">
  7. <view class="city-list">
  8. <!-- 城市列表 -->
  9. <view v-for="(item, index) in cityData">
  10. <view class="c-title" :id="item['首字母']">{{item['首字母']}}</view>
  11. <view class="item" v-for="(city,index2) in item['城市列表']" @click="selectCity(city)">
  12. {{city.city}}
  13. </view>
  14. </view>
  15. </view>
  16. <!-- 字母列表 -->
  17. <view class="zhimubox">
  18. <view v-for="(item, index) in alphabet">
  19. <view class="alphabet" :class="{select:toIndex == item}" @click="tap(item)">
  20. {{item}}
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </scroll-view>
  26. </view>
  27. </template>
  28. <script>
  29. export default {
  30. data() {
  31. return {
  32. searchValue: '',
  33. cityData: [],
  34. alphabet: [],
  35. toIndex: '', //跳转的索引的字母
  36. searchData:[],
  37. locationCity: {
  38. cityName: '',
  39. cityCode: '',
  40. lng: '',
  41. lat: '',
  42. },
  43. }
  44. },
  45. onLoad() {
  46. this.getData();
  47. },
  48. onShow() {
  49. /* var nowCity = uni.getStorageSync("locationCity");
  50. if (nowCity) {
  51. this.locationCity = nowCity
  52. } else {
  53. this.getLocation()
  54. } */
  55. this.getData();
  56. },
  57. methods: {
  58. tap(item) {
  59. console.log('字母点击', item);
  60. this.toIndex = item
  61. },
  62. againDingWei() {
  63. console.log('重新定位');
  64. this.getLocation();
  65. },
  66. getLocation() {
  67. const that = this
  68. uni.getLocation({
  69. type: 'wgs84',
  70. success: function(res) {
  71. console.log('定位', res)
  72. that.locationCity.lng = res.longitude
  73. that.locationCity.lat = res.latitude
  74. that.getAdress();
  75. },
  76. fail(err) {
  77. console.log(err)
  78. }
  79. });
  80. },
  81. getAdress() {
  82. // 根据经纬度 逆城市地理编码 获取城市信息
  83. var location = this.locationCity.lng + ',' + this.locationCity.lat
  84. uni.request({
  85. url: 'https://restapi.amap.com/v3/geocode/regeo',
  86. data: {
  87. key: '389a059efa3f499d9145eb84b1c3248d',
  88. location: location,
  89. //location: '117.06533,36.68013',
  90. //types: "190000",
  91. //extensions: "all",
  92. //radius: 100
  93. },
  94. dataType: "json",
  95. success: (res) => {
  96. console.log('定位城市', res);
  97. if(res.data.regeocode){
  98. console.log("城市名称")
  99. console.log(res.data.regeocode.addressComponent.city)
  100. // console.log(res.data.pois[0].cityname)
  101. let cityname = res.data.regeocode.addressComponent.city;
  102. var cityCode = res.data.regeocode.addressComponent.adcode
  103. cityCode = cityCode.slice(0, -2)
  104. cityCode = cityCode + '00'
  105. this.locationCity.cityName = cityname
  106. this.locationCity.cityCode = cityCode
  107. uni.setStorage({
  108. key: 'locationCity',
  109. data: this.locationCity,
  110. success: function() {
  111. console.log('定位城市,保存成功');
  112. }
  113. })
  114. }else{
  115. console.log("接口获取失败")
  116. }
  117. }
  118. });
  119. },
  120. search(val) {
  121. // console.log(val);
  122. this.searchValue = val
  123. this.getData()
  124. },
  125. getData() {
  126. uni.showLoading({
  127. title: '加载中'
  128. })
  129. let url = 'opencarOwnerHome/shop-city-list',
  130. params = {
  131. }
  132. this.$http(url, params, 'GET').then(res => {
  133. uni.hideLoading()
  134. if (res.code == 0) {
  135. this.cityData = res.data
  136. var arr = []
  137. this.cityData.forEach((item, index) => {
  138. arr.push(item.首字母)
  139. });
  140. }
  141. this.alphabet = arr
  142. })
  143. },
  144. selectDingCity(locationCity){
  145. console.log('选择了定位城市:', locationCity);
  146. var city={}
  147. city.city = locationCity.cityName
  148. city.code = locationCity.cityCode
  149. uni.setStorage({
  150. key: 'selectCity',
  151. data: city,
  152. success: function() {
  153. console.log('选择了定位城市,保存成功');
  154. }
  155. })
  156. uni.navigateBack({
  157. })
  158. },
  159. selectCity(city) {
  160. console.log('选择的城市:', city);
  161. uni.setStorage({
  162. key: 'selectCity',
  163. data: city,
  164. success: function() {
  165. console.log('选择的城市,保存成功');
  166. }
  167. })
  168. uni.navigateBack({
  169. })
  170. },
  171. },
  172. onPullDownRefresh() {
  173. this.getData()
  174. setTimeout(function() {
  175. uni.stopPullDownRefresh();
  176. }, 1000);
  177. },
  178. }
  179. </script>
  180. <style scoped>
  181. .zhimubox{
  182. position: fixed;
  183. right: 30rpx;
  184. top: 200rpx;
  185. height: 80vh;
  186. overflow-y: scroll;
  187. }
  188. .content {
  189. background: #FFFFFF;
  190. min-height: 100vh;
  191. }
  192. .topView {
  193. background: #FFFFFF;
  194. position: fixed;
  195. width: 100%;
  196. height: 120rpx;
  197. z-index: 99;
  198. }
  199. .searchBoxBg {
  200. width: 100%;
  201. background-color: #FFFFFF;
  202. border-top: 1rpx solid #EEEEEE;
  203. }
  204. .searchBox {
  205. display: flex;
  206. height: 72rpx;
  207. margin: 24rpx;
  208. background-color: #F4F5F7;
  209. border-radius: 36rpx;
  210. }
  211. .scroll-view {
  212. width: 100%;
  213. /* height: calc(100vh - 80rpx); */
  214. height: 100vh;
  215. box-sizing: border-box;
  216. padding: 20rpx 24rpx 20rpx;
  217. }
  218. .nowArea {
  219. width: 100%;
  220. height: 147rpx;
  221. }
  222. .area {
  223. color: #999999;
  224. font-size: 24rpx;
  225. margin-bottom: 20rpx;
  226. }
  227. .dingwBox {
  228. display: flex;
  229. justify-content: space-between;
  230. }
  231. .dingweiCity {
  232. background-color: #F4F5F7;
  233. border-radius: 49rpx;
  234. width: 140rpx;
  235. height: 64rpx;
  236. text-align: center;
  237. line-height: 64rpx;
  238. font-size: 26rpx;
  239. }
  240. .dingBg {
  241. display: flex;
  242. align-items: center;
  243. color: #3F90F7;
  244. font-size: 26rpx;
  245. }
  246. .icon {
  247. width: 33rpx;
  248. height: 33rpx;
  249. margin-right: 5rpx;
  250. }
  251. .listContent {
  252. display: flex;
  253. justify-content: space-between;
  254. }
  255. .city-list {
  256. display: flex;
  257. flex-direction: column;
  258. width: 95%;
  259. }
  260. .c-title {
  261. color: #999999;
  262. font-size: 24rpx;
  263. height: 33rpx;
  264. line-height: 33rpx;
  265. padding-top: 30rpx;
  266. }
  267. .item {
  268. width: 100%;
  269. height: 46rpx;
  270. padding: 30rpx 13rpx;
  271. color: #3C3C3C;
  272. font-size: 28rpx;
  273. border-bottom: 1rpx solid #EEEEEE;
  274. }
  275. .alphabet {
  276. font-size: 22rpx;
  277. font-weight: bold;
  278. color: #999999;
  279. width: 4%;
  280. margin: 20rpx 0;
  281. text-align: center;
  282. }
  283. .select {
  284. color: #FF4F00;
  285. }
  286. .reach-content{
  287. width: 100%;
  288. height: calc(100vh - 120rpx);
  289. box-sizing: border-box;
  290. padding: 120rpx 24rpx 20rpx;
  291. }
  292. /* 空白页css */
  293. .nodataBox {
  294. text-align: center;
  295. }
  296. .nodataImg {
  297. width: 400rpx;
  298. padding-top: 300rpx;
  299. }
  300. .noTxt {
  301. font-size: 30rpx;
  302. color: #999999;
  303. padding-top: 50rpx;
  304. }
  305. </style>