city.vue 7.3 KB

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