chooseCity.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.city" @click="selectCity(locationCity)" class="dingweiCity">
  17. {{locationCity.city}}
  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>
  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. city: '',
  78. code: '',
  79. },
  80. location: {
  81. lng: '',
  82. lat: '',
  83. },
  84. }
  85. },
  86. onLoad() {
  87. this.getData();
  88. },
  89. onShow() {
  90. var nowCity = uni.getStorageSync("locationCity");
  91. if (nowCity) {
  92. this.locationCity = nowCity
  93. } else {
  94. this.getLocation()
  95. }
  96. this.getData();
  97. },
  98. methods: {
  99. tap(item) {
  100. console.log('字母点击', item);
  101. this.toIndex = item
  102. },
  103. againDingWei() {
  104. console.log('重新定位');
  105. this.getLocation();
  106. },
  107. getLocation() {
  108. const that = this
  109. uni.getLocation({
  110. type: 'gcj02',
  111. success: function(res) {
  112. console.log('定位', res)
  113. that.location.lng = res.longitude
  114. that.location.lat = res.latitude
  115. uni.setStorage({
  116. key: 'location',
  117. data: that.location,
  118. success: function() {
  119. console.log('定位,保存成功');
  120. }
  121. })
  122. that.getAdress();
  123. },
  124. fail(err) {
  125. console.log(err)
  126. }
  127. });
  128. },
  129. getAdress() {
  130. // 根据经纬度 逆城市地理编码 获取城市信息
  131. var location = this.location.lng + ',' + this.location.lat
  132. uni.request({
  133. url: 'https://restapi.amap.com/v3/place/around',
  134. data: {
  135. key: '064b6a4a8ade55656edcde2f528876de',
  136. location: location,
  137. types: "190000",
  138. extensions: "all",
  139. radius: 100
  140. },
  141. dataType: "json",
  142. success: (res) => {
  143. console.log('定位城市', res);
  144. let cityname = res.data.pois[0].cityname;
  145. var cityCode = res.data.pois[0].adcode
  146. cityCode = cityCode.slice(0, -2)
  147. cityCode = cityCode + '00'
  148. this.locationCity.city = cityname
  149. this.locationCity.code = cityCode
  150. uni.setStorage({
  151. key: 'locationCity',
  152. data: this.locationCity,
  153. success: function() {
  154. console.log('定位城市,保存成功');
  155. }
  156. })
  157. }
  158. });
  159. },
  160. search(val) {
  161. // console.log(val);
  162. this.searchValue = val
  163. this.getData()
  164. },
  165. getData() {
  166. uni.showLoading({
  167. title: '加载中'
  168. })
  169. let url = 'worldKeepCar/worldHome/queryCityRole',
  170. params = {
  171. city: this.searchValue ? this.searchValue : '',
  172. }
  173. this.$http(url, params, 'GET').then(res => {
  174. uni.hideLoading()
  175. if (res.code == 0) {
  176. this.cityData = res.data
  177. var arr = []
  178. this.cityData.forEach((item, index) => {
  179. arr.push(item.首字母)
  180. });
  181. }
  182. this.alphabet = arr
  183. console.log(this.alphabet);
  184. console.log(this.cityData);
  185. })
  186. },
  187. selectCity(city) {
  188. console.log('选择的城市:', city);
  189. uni.setStorage({
  190. key: 'selectCity',
  191. data: city,
  192. success: function() {
  193. console.log('选择的城市,保存成功');
  194. }
  195. })
  196. uni.navigateBack({
  197. })
  198. },
  199. },
  200. onPullDownRefresh() {
  201. this.getData()
  202. setTimeout(function() {
  203. uni.stopPullDownRefresh();
  204. }, 1000);
  205. },
  206. }
  207. </script>
  208. <style>
  209. .content {
  210. background: #FFFFFF;
  211. min-height: 100vh;
  212. }
  213. .topView {
  214. background: #FFFFFF;
  215. position: fixed;
  216. width: 100%;
  217. height: 120rpx;
  218. z-index: 99;
  219. }
  220. .searchBoxBg {
  221. width: 100%;
  222. background-color: #FFFFFF;
  223. border-top: 1rpx solid #EEEEEE;
  224. }
  225. .searchBox {
  226. display: flex;
  227. height: 72rpx;
  228. margin: 24rpx;
  229. background-color: #F4F5F7;
  230. border-radius: 36rpx;
  231. }
  232. .scroll-view {
  233. width: 100%;
  234. height: calc(100vh - 120rpx);
  235. box-sizing: border-box;
  236. padding: 120rpx 24rpx 20rpx;
  237. }
  238. .nowArea {
  239. width: 100%;
  240. height: 147rpx;
  241. }
  242. .area {
  243. color: #999999;
  244. font-size: 24rpx;
  245. margin-bottom: 20rpx;
  246. }
  247. .dingwBox {
  248. display: flex;
  249. justify-content: space-between;
  250. }
  251. .dingweiCity {
  252. background-color: #F4F5F7;
  253. border-radius: 49rpx;
  254. width: 140rpx;
  255. height: 64rpx;
  256. text-align: center;
  257. line-height: 64rpx;
  258. font-size: 26rpx;
  259. }
  260. .dingBg {
  261. display: flex;
  262. align-items: center;
  263. color: #3F90F7;
  264. font-size: 26rpx;
  265. }
  266. .icon {
  267. width: 33rpx;
  268. height: 33rpx;
  269. margin-right: 5rpx;
  270. }
  271. .listContent {
  272. display: flex;
  273. justify-content: space-between;
  274. }
  275. .city-list {
  276. display: flex;
  277. flex-direction: column;
  278. width: 95%;
  279. }
  280. .c-title {
  281. color: #999999;
  282. font-size: 24rpx;
  283. height: 33rpx;
  284. line-height: 33rpx;
  285. padding-top: 30rpx;
  286. }
  287. .item {
  288. width: 100%;
  289. height: 46rpx;
  290. padding: 30rpx 13rpx;
  291. color: #3C3C3C;
  292. font-size: 28rpx;
  293. border-bottom: 1rpx solid #EEEEEE;
  294. }
  295. .alphabet {
  296. font-size: 22rpx;
  297. font-weight: bold;
  298. color: #999999;
  299. width: 4%;
  300. margin: 20rpx 0;
  301. text-align: center;
  302. }
  303. .select {
  304. color: #FF4F00;
  305. }
  306. .reach-content{
  307. width: 100%;
  308. height: calc(100vh - 120rpx);
  309. box-sizing: border-box;
  310. padding: 120rpx 24rpx 20rpx;
  311. }
  312. /* 空白页css */
  313. .nodataBox {
  314. text-align: center;
  315. }
  316. .nodataImg {
  317. width: 400rpx;
  318. padding-top: 300rpx;
  319. }
  320. .noTxt {
  321. font-size: 30rpx;
  322. color: #999999;
  323. padding-top: 50rpx;
  324. }
  325. </style>