city.vue 6.9 KB

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