switchcity.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <template>
  2. <view>
  3. <view class="">
  4. <view class="cu-bar bg-white search " >
  5. <view class="search-form round">
  6. <text class="cuIcon-search"></text>
  7. <input type="text" confirm-type="search" @input="bindKeyInput" @blur="bindBlur" placeholder="输入城市名或拼音查询" placeholder-style="font-size: 28rpx" :value="inputName"></input>
  8. </view>
  9. </view>
  10. </view>
  11. <view class="container-inner">
  12. <view class="searchLetter touchClass">
  13. <view class="thishotText" @tap="hotCity">
  14. <view style="margin-top:0;">当前</view>
  15. <view style="margin-top:0;">热门</view>
  16. </view>
  17. <view v-for="(item, index) in searchLetter" :key="index" class="text-red-btn" style="font-size:20rpx;" :data-letter="item.name" @click="clickLetter">{{item.name}}</view>
  18. </view>
  19. <view class="container">
  20. <block v-if="isShowLetter">
  21. <view class="showSlectedLetter">
  22. {{toastShowLetter}}
  23. </view>
  24. </block>
  25. <scroll-view scroll-y="true" :style="'height:' + winHeight + 'px'" @scroll="bindScroll" :scroll-into-view="scrollTopId" :scroll-top="scrollTop">
  26. <view class="bg-white">
  27. <view class="solid-bottom li" v-for="(item, index) in completeList" :key="index" @tap="bindCity" :data-city="item.city" :data-code="item.code" >
  28. <text class="padding-left-50">{{item.city}}</text>
  29. </view>
  30. </view>
  31. <view v-if="condition" class="hotcity-common" style="margin-bottom: 10rpx;" >选择区县</view>
  32. <view v-if="condition" class="county">
  33. <block v-for="(item, index) in countyList" :key="index" class="hotCity">
  34. <view class="weui-grid" style="margin-right: 16rpx;" :data-code="item.id" :data-city="item.fullname" @tap="bindCounty">
  35. <view class="weui-grid__label">{{item.fullname}}</view>
  36. </view>
  37. </block>
  38. </view>
  39. <view class="selectCity">
  40. <view class="hotcity-common" @tap="reGetLocation">重新定位城区</view>
  41. <view class="thisCityName" :data-city="city" :data-code="currentCityCode">{{city}}{{county}}</view>
  42. <view class="hotcity-common">热门城市</view>
  43. <view class="weui-grids">
  44. <view class="weui-grid" data-code="110000" data-city="北京市" @tap="bindCity">
  45. <view class="weui-grid__label">北京市</view>
  46. </view>
  47. <view class="weui-grid" data-code="310000" data-city="上海市" @tap="bindCity">
  48. <view class="weui-grid__label">上海市</view>
  49. </view>
  50. <view class="weui-grid" data-code="440100" data-city="广州市" @tap="bindCity">
  51. <view class="weui-grid__label">广州市</view>
  52. </view>
  53. </view>
  54. <view class="weui-grids">
  55. <view class="weui-grid" data-code="440300" data-city="深圳市" @tap="bindCity">
  56. <view class="weui-grid__label">深圳市</view>
  57. </view>
  58. <view class="weui-grid" data-code="330100" data-city="杭州市" @tap="bindCity">
  59. <view class="weui-grid__label">杭州市</view>
  60. </view>
  61. <view class="weui-grid" data-code="320100" data-city="南京市" @tap="bindCity">
  62. <view class="weui-grid__label">南京市</view>
  63. </view>
  64. </view>
  65. <view class="weui-grids">
  66. <view class="weui-grid" data-code="420100" data-city="武汉市" @tap="bindCity">
  67. <view class="weui-grid__label">武汉市</view>
  68. </view>
  69. <view class="weui-grid" data-code="120000" data-city="天津市" @tap="bindCity">
  70. <view class="weui-grid__label">天津市</view>
  71. </view>
  72. <view class="weui-grid" data-code="610100" data-city="西安市" @tap="bindCity">
  73. <view class="weui-grid__label">西安市</view>
  74. </view>
  75. </view>
  76. </view>
  77. <view v-for="(item, index) in cityList" :key="index" class="selection">
  78. <view class="item_letter" :id="item.initial">{{item.initial}}</view>
  79. <view v-for="(ct, index2) in item.cityInfo" :key="index2" class="item_city" :data-code="ct.code" :data-city="ct.city" @tap="bindCity">
  80. {{ct.city}}
  81. </view>
  82. </view>
  83. </scroll-view>
  84. </view>
  85. </view>
  86. </view>
  87. </template>
  88. <script>
  89. var app=getApp()
  90. const city = require("../../utils/city.js");
  91. const cityObjs = require("../../utils/city.js");
  92. const config = require("../../utils/config.js");
  93. const appInstance = getApp();
  94. export default {
  95. data() {
  96. return {
  97. searchLetter: [],
  98. showLetter: "",
  99. winHeight: 0,
  100. cityList: [],
  101. isShowLetter: false,
  102. scrollTop: 0,
  103. oldScrollTop:0,
  104. //置顶高度
  105. scrollTopId: '',
  106. //置顶id
  107. city: "定位中...",
  108. currentCityCode: '',
  109. inputName: '',
  110. completeList: [],
  111. county: '',
  112. //do
  113. condition: false,
  114. itemH: "",
  115. toastShowLetter: "",
  116. countyList: ""
  117. };
  118. },
  119. components: {},
  120. props: {},
  121. onLoad: function () {
  122. // 生命周期函数--监听页面加载
  123. const searchLetter = city.searchLetter;
  124. const cityList = city.cityList();
  125. const sysInfo = uni.getSystemInfoSync();
  126. console.log(sysInfo);
  127. const winHeight = sysInfo.windowHeight;
  128. const itemH = winHeight / searchLetter.length;
  129. let tempArr = [];
  130. searchLetter.map((item, index) => {
  131. // console.log(item);
  132. // console.log(index);
  133. let temp = {};
  134. temp.name = item;
  135. temp.tHeight = index * itemH;
  136. temp.bHeight = (index + 1) * itemH;
  137. tempArr.push(temp);
  138. }); // console.log(tempArr);
  139. this.setData({
  140. winHeight: winHeight,
  141. itemH: itemH,
  142. searchLetter: tempArr,
  143. cityList: cityList,
  144. //兼容地图插件
  145. city: appInstance.globalData.city,
  146. currentCityCode: appInstance.globalData.currentCityCode,
  147. county: appInstance.globalData.county
  148. }); //this.getLocation();
  149. },
  150. methods: {
  151. goTop (e) {
  152. //视图会发生重新渲染
  153. this.scrollTop = this.oldScrollTop
  154. //当视图渲染结束 重新设置为0
  155. this.$nextTick(() =>{
  156. this.scrollTop = 0
  157. });
  158. },
  159. clickLetter: function (e) {
  160. // console.log(e);
  161. console.log(e.currentTarget.dataset.letter);
  162. const showLetter = e.currentTarget.dataset.letter;
  163. this.setData({
  164. toastShowLetter: showLetter,
  165. isShowLetter: true,
  166. scrollTopId: showLetter
  167. }); // const that = this;
  168. // wx.showToast({
  169. // title: showLetter,
  170. // disabled: true,
  171. // duration: 500,
  172. // complete: function() {
  173. // that.setData({
  174. // scrollTopId: showLetter,
  175. // })
  176. // }
  177. // })
  178. const that = this;
  179. setTimeout(function () {
  180. that.setData({
  181. isShowLetter: false
  182. });
  183. }, 500);
  184. },
  185. reGetLocation: function () {
  186. appInstance.globalData.defaultCity = this.city;
  187. appInstance.globalData.defaultCounty = this.county;
  188. console.log(appInstance.globalData.defaultCity); //返回首页
  189. uni.navigateTo({
  190. url: "/pages/choosePlot/choosePlot"
  191. });
  192. },
  193. //选择城市
  194. bindCity: function (e) {
  195. this.goTop()
  196. this.setData({
  197. condition: true,
  198. city: e.currentTarget.dataset.city,
  199. currentCityCode: e.currentTarget.dataset.code,
  200. completeList: []
  201. });
  202. this.selectCounty();
  203. appInstance.globalData.defaultCity = this.city;
  204. appInstance.globalData.defaultCounty = '';
  205. this.setData({
  206. county: "",
  207. currentCityCode: ""
  208. });
  209. console.log(appInstance.globalData.defaultCity);
  210. },
  211. bindCounty: function (e) {
  212. console.log(e); //this.setData({ county: e.currentTarget.dataset.city })
  213. //appInstance.defaultCounty = this.data.county
  214. console.info("////" + this.city);
  215. let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息.
  216. let prevPage = pages[pages.length - 2].$vm; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面,-3是上上个页面以此类推。
  217. if(!this.$isEmpty(this.city)){
  218. app.globalData.city=this.city
  219. }
  220. if(!this.$isEmpty(e.currentTarget.dataset.city)){
  221. app.globalData.county=e.currentTarget.dataset.city
  222. }
  223. if(!this.$isEmpty(e.currentTarget.dataset.code)){
  224. app.globalData.currentCityCode=e.currentTarget.dataset.code
  225. }
  226. prevPage.setData({
  227. // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
  228. city: this.city,
  229. //广州市
  230. county: e.currentTarget.dataset.city,
  231. //页面标签定义的是city--天河区
  232. currentCityCode: e.currentTarget.dataset.code //天河区的code
  233. }); //从新获取楼栋信息
  234. prevPage.getCommunity(); //上一个页面内执行setData操作,将我们想要的信息保存住。当我们返回去的时候,页面已经处理完毕。
  235. //最后就是返回上一个页面。
  236. uni.navigateBack(); //console.log(appInstance.defaultCounty);
  237. // wx.navigateBack({
  238. // url: "/pages/choosePlot/choosePlot",
  239. // })
  240. },
  241. //点击热门城市回到顶部
  242. hotCity: function () {
  243. console.log("hotCity");
  244. this.setData({
  245. scrollTop: 0
  246. });
  247. },
  248. bindScroll: function (e) {// console.log(e.detail)
  249. this.oldScrollTop = e.detail.scrollTop
  250. },
  251. selectCounty: function () {
  252. console.log("正在定位区县");
  253. let code = this.currentCityCode; // console.log(code);
  254. const that = this;
  255. uni.request({
  256. url: `https://apis.map.qq.com/ws/district/v1/getchildren?&id=${code}&key=${config.key}`,
  257. success: function (res) {
  258. // console.log(res.data)
  259. // console.log(res.data.result[0]);
  260. that.setData({
  261. countyList: res.data.result[0]
  262. }); // console.log(that.data.countyList);
  263. console.log("请求区县成功" + `https://apis.map.qq.com/ws/district/v1/getchildren?&id=${code}&key=${config.key}`); // console.log(res)
  264. },
  265. fail: function () {
  266. console.log("请求区县失败,请重试");
  267. }
  268. });
  269. },
  270. getLocation: function () {
  271. console.log("正在定位城市");
  272. this.setData({
  273. county: ''
  274. });
  275. const that = this;
  276. uni.getLocation({
  277. type: 'wgs84',
  278. success: function (res) {
  279. let latitude = res.latitude;
  280. let longitude = res.longitude;
  281. uni.request({
  282. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}`,
  283. success: res => {
  284. // console.log(res)
  285. console.log(res.data.result.ad_info.city + res.data.result.ad_info.adcode);
  286. that.setData({
  287. city: res.data.result.ad_info.city,
  288. currentCityCode: res.data.result.ad_info.adcode,
  289. county: res.data.result.ad_info.district
  290. });
  291. that.selectCounty();
  292. }
  293. });
  294. }
  295. });
  296. },
  297. bindBlur: function (e) {
  298. this.setData({
  299. inputName: ''
  300. });
  301. },
  302. bindKeyInput: function (e) {
  303. this.setData({
  304. inputName: e.detail.value
  305. });
  306. this.auto();
  307. this.goTop()
  308. },
  309. auto: function () {
  310. let inputSd = this.inputName.trim();
  311. let sd = inputSd.toLowerCase();
  312. let num = sd.length;
  313. const cityList = cityObjs.cityObjs; // console.log(cityList.length)
  314. let finalCityList = [];
  315. let temp = cityList.filter(item => {
  316. let text = item.short.slice(0, num).toLowerCase();
  317. return text && text == sd;
  318. }); //在城市数据中,添加简拼到“shorter”属性,就可以实现简拼搜索
  319. let tempShorter = cityList.filter(itemShorter => {
  320. if (itemShorter.shorter) {
  321. let textShorter = itemShorter.shorter.slice(0, num).toLowerCase();
  322. return textShorter && textShorter == sd;
  323. }
  324. return;
  325. });
  326. let tempChinese = cityList.filter(itemChinese => {
  327. let textChinese = itemChinese.city.slice(0, num);
  328. return textChinese && textChinese == sd;
  329. });
  330. if (temp[0]) {
  331. temp.map(item => {
  332. let testObj = {};
  333. testObj.city = item.city;
  334. testObj.code = item.code;
  335. finalCityList.push(testObj);
  336. });
  337. this.setData({
  338. completeList: finalCityList
  339. });
  340. } else if (tempShorter[0]) {
  341. tempShorter.map(item => {
  342. let testObj = {};
  343. testObj.city = item.city;
  344. testObj.code = item.code;
  345. finalCityList.push(testObj);
  346. });
  347. this.setData({
  348. completeList: finalCityList
  349. });
  350. } else if (tempChinese[0]) {
  351. tempChinese.map(item => {
  352. let testObj = {};
  353. testObj.city = item.city;
  354. testObj.code = item.code;
  355. finalCityList.push(testObj);
  356. });
  357. this.setData({
  358. completeList: finalCityList
  359. });
  360. } else {
  361. return;
  362. }
  363. }
  364. }
  365. };
  366. </script>
  367. <style lang="scss">
  368. .container-inner {
  369. display: flex;
  370. flex-direction: row-reverse;
  371. }
  372. .container {
  373. flex-grow: 1;
  374. display: flex;
  375. flex-direction: column;
  376. padding: 0rpx;
  377. }
  378. input {
  379. text-align: center;
  380. font-size: 32rpx;
  381. padding: 5px;
  382. }
  383. .searchLetter {
  384. flex-shrink: 0;
  385. width: 80rpx;
  386. text-align: center;
  387. display: flex;
  388. flex-direction: column;
  389. color: #666;
  390. }
  391. .searchLetter view {
  392. margin-top: 20rpx;
  393. }
  394. .touchClass {
  395. background-color: #fff;
  396. color: #fff;
  397. padding-top: 16rpx;
  398. padding-bottom: 16rpx;
  399. }
  400. .showSlectedLetter {
  401. background-color: rgba(0, 0, 0, 0.5);
  402. color: #fff;
  403. display: flex;
  404. justify-content: center;
  405. align-items: center;
  406. position: fixed;
  407. top: 50%;
  408. left: 50%;
  409. margin: -100rpx;
  410. width: 200rpx;
  411. height: 200rpx;
  412. border-radius: 20rpx;
  413. font-size: 52rpx;
  414. z-index: 1;
  415. }
  416. .selection {
  417. display: flex;
  418. width: 100%;
  419. flex-direction: column;
  420. margin-top: 10rpx;
  421. }
  422. .selectCity {
  423. padding: 16rpx;
  424. background-color: #FFFFFF;
  425. margin-bottom: -10rpx;
  426. }
  427. .item_letter {
  428. color: $base-btn-color;
  429. display: flex;
  430. background-color: #f5f5f5;
  431. height: 40rpx;
  432. padding-left: 34rpx;
  433. align-items: center;
  434. font-size: 24rpx;
  435. }
  436. .item_city {
  437. display: flex;
  438. background-color: #fff;
  439. height: 100rpx;
  440. padding-left: 34rpx;
  441. align-items: center;
  442. border-bottom: 1rpx solid #ededed;
  443. font-size: 24rpx;
  444. }
  445. .hotcity-common {
  446. font-size: 24rpx;
  447. color: #666;
  448. padding-bottom: 0;
  449. margin: 8rpx 0;
  450. margin-left: 16rpx;
  451. }
  452. .hotCity {
  453. padding-right: 50rpx;
  454. margin: auto;
  455. }
  456. .thisCityName {
  457. display: inline-block;
  458. border: 1rpx solid $base-btn-color;
  459. border-radius: 8rpx;
  460. padding: 16rpx 20rpx;
  461. font-size: 24rpx;
  462. color: $base-btn-color;
  463. text-align: center;
  464. min-width: 149.5rpx;
  465. margin: 16rpx 0;
  466. }
  467. .thishotText {
  468. color: $base-btn-color;
  469. font-size: 20rpx;
  470. margin: 0 !important;
  471. }
  472. .slectCity {
  473. border-color: $base-btn-color !important;
  474. }
  475. .slectCity view {
  476. color: $base-btn-color !important;
  477. }
  478. .weui-grid {
  479. padding: 18rpx 0;
  480. width: 200rpx;
  481. box-sizing: border-box;
  482. border: 1rpx solid #c8c8c8;
  483. border-radius: 8rpx;
  484. background-color: white;
  485. margin: 8rpx 0;
  486. }
  487. .weui-grids {
  488. display: flex;
  489. flex-direction: row;
  490. justify-content: space-between;
  491. }
  492. .weui-grid__label {
  493. display: block;
  494. text-align: center;
  495. color: #333;
  496. font-size: 24rpx;
  497. white-space: nowrap;
  498. text-overflow: ellipsis;
  499. overflow: hidden;
  500. }
  501. .ul {
  502. display: block;
  503. color: grey;
  504. margin-left: 20rpx;
  505. }
  506. .li {
  507. display: block;
  508. font-weight: 100;
  509. font-size: 28rpx;
  510. padding: 20rpx 0;
  511. border-bottom: 1rpx solid #efefef;
  512. }
  513. input {
  514. background-color: #fff;
  515. }
  516. .input {
  517. padding: 16rpx;
  518. border-bottom: 1rpx solid #f1f1f1;
  519. }
  520. .county {
  521. padding: 10rpx 0 0 20rpx;
  522. background-color: #FFFFFF;
  523. display: flex;
  524. flex-wrap: wrap;
  525. }
  526. </style>