city.vue 7.2 KB

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