chooseCity.vue 8.2 KB

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