city.vue 7.0 KB

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