switchcity.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 @click="back" 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. back(){
  152. if (!this.$isEmpty(this.county)) {
  153. uni.navigateBack({
  154. delta:1
  155. })
  156. }
  157. },
  158. goTop (e) {
  159. //视图会发生重新渲染
  160. this.scrollTop = this.oldScrollTop
  161. //当视图渲染结束 重新设置为0
  162. this.$nextTick(() =>{
  163. this.scrollTop = 0
  164. });
  165. },
  166. clickLetter: function (e) {
  167. // console.log(e);
  168. console.log(e.currentTarget.dataset.letter);
  169. const showLetter = e.currentTarget.dataset.letter;
  170. this.setData({
  171. toastShowLetter: showLetter,
  172. isShowLetter: true,
  173. scrollTopId: showLetter
  174. }); // const that = this;
  175. // wx.showToast({
  176. // title: showLetter,
  177. // disabled: true,
  178. // duration: 500,
  179. // complete: function() {
  180. // that.setData({
  181. // scrollTopId: showLetter,
  182. // })
  183. // }
  184. // })
  185. const that = this;
  186. setTimeout(function () {
  187. that.setData({
  188. isShowLetter: false
  189. });
  190. }, 500);
  191. },
  192. reGetLocation: function () {
  193. appInstance.globalData.defaultCity = this.city;
  194. appInstance.globalData.defaultCounty = this.county;
  195. console.log(appInstance.globalData.defaultCity); //返回首页
  196. uni.navigateTo({
  197. url: "/pages/choosePlot/choosePlot"
  198. });
  199. },
  200. //选择城市
  201. bindCity: function (e) {
  202. this.goTop()
  203. this.setData({
  204. condition: true,
  205. city: e.currentTarget.dataset.city,
  206. currentCityCode: e.currentTarget.dataset.code,
  207. completeList: []
  208. });
  209. this.selectCounty();
  210. appInstance.globalData.defaultCity = this.city;
  211. appInstance.globalData.defaultCounty = '';
  212. this.setData({
  213. county: "",
  214. currentCityCode: ""
  215. });
  216. console.log(appInstance.globalData.defaultCity);
  217. },
  218. bindCounty: function (e) {
  219. console.log(e); //this.setData({ county: e.currentTarget.dataset.city })
  220. //appInstance.defaultCounty = this.data.county
  221. console.info("////" + this.city);
  222. let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息.
  223. let prevPage = pages[pages.length - 2].$vm; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面,-3是上上个页面以此类推。
  224. if(!this.$isEmpty(this.city)){
  225. app.globalData.city=this.city
  226. }
  227. if(!this.$isEmpty(e.currentTarget.dataset.city)){
  228. app.globalData.county=e.currentTarget.dataset.city
  229. }
  230. if(!this.$isEmpty(e.currentTarget.dataset.code)){
  231. app.globalData.currentCityCode=e.currentTarget.dataset.code
  232. }
  233. prevPage.setData({
  234. // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
  235. city: this.city,
  236. //广州市
  237. county: e.currentTarget.dataset.city,
  238. //页面标签定义的是city--天河区
  239. currentCityCode: e.currentTarget.dataset.code //天河区的code
  240. }); //从新获取楼栋信息
  241. // prevPage.getCommunity();
  242. uni.navigateBack(); //console.log(appInstance.defaultCounty);
  243. // wx.navigateBack({
  244. // url: "/pages/choosePlot/choosePlot",
  245. // })
  246. },
  247. //点击热门城市回到顶部
  248. hotCity: function () {
  249. console.log("hotCity");
  250. this.setData({
  251. scrollTop: 0
  252. });
  253. },
  254. bindScroll: function (e) {// console.log(e.detail)
  255. this.oldScrollTop = e.detail.scrollTop
  256. },
  257. selectCounty: function () {
  258. console.log("正在定位区县");
  259. let code = this.currentCityCode; // console.log(code);
  260. const that = this;
  261. let params={
  262. id:code,
  263. key:config.key
  264. }
  265. // let operation = 'miniprogram/ws/district/v1/getchildren';
  266. that.$http.getDistrictChildren(params).then (res=>{
  267. let countyList=res.data.result.result[0]
  268. that.setData({
  269. countyList: countyList
  270. });
  271. });
  272. },
  273. getLocation: function () {
  274. console.log("正在定位城市");
  275. this.setData({
  276. county: ''
  277. });
  278. const that = this;
  279. uni.getLocation({
  280. type: 'wgs84',
  281. success: function (res) {
  282. let latitude = res.latitude;
  283. let longitude = res.longitude;
  284. let params={
  285. location:latitude+","+longitude,
  286. key:config.key,
  287. }
  288. // let operation = 'miniprogram/ws/geocoder/v1/location';
  289. that.$http.getLocation(params).then(res =>{
  290. let info=res.data.result.result.ad_info
  291. console.log(info.city + info.adcode);
  292. that.setData({
  293. city: info.city,
  294. currentCityCode: info.adcode,
  295. county: info.district
  296. });
  297. });
  298. }
  299. });
  300. },
  301. bindBlur: function (e) {
  302. this.setData({
  303. inputName: ''
  304. });
  305. },
  306. bindKeyInput: function (e) {
  307. this.setData({
  308. inputName: e.detail.value
  309. });
  310. this.auto();
  311. this.goTop()
  312. },
  313. auto: function () {
  314. let inputSd = this.inputName.trim();
  315. let sd = inputSd.toLowerCase();
  316. let num = sd.length;
  317. const cityList = cityObjs.cityObjs; // console.log(cityList.length)
  318. let finalCityList = [];
  319. let temp = cityList.filter(item => {
  320. let text = item.short.slice(0, num).toLowerCase();
  321. return text && text == sd;
  322. }); //在城市数据中,添加简拼到“shorter”属性,就可以实现简拼搜索
  323. let tempShorter = cityList.filter(itemShorter => {
  324. if (itemShorter.shorter) {
  325. let textShorter = itemShorter.shorter.slice(0, num).toLowerCase();
  326. return textShorter && textShorter == sd;
  327. }
  328. return;
  329. });
  330. let tempChinese = cityList.filter(itemChinese => {
  331. let textChinese = itemChinese.city.slice(0, num);
  332. return textChinese && textChinese == sd;
  333. });
  334. if (temp[0]) {
  335. temp.map(item => {
  336. let testObj = {};
  337. testObj.city = item.city;
  338. testObj.code = item.code;
  339. finalCityList.push(testObj);
  340. });
  341. this.setData({
  342. completeList: finalCityList
  343. });
  344. } else if (tempShorter[0]) {
  345. tempShorter.map(item => {
  346. let testObj = {};
  347. testObj.city = item.city;
  348. testObj.code = item.code;
  349. finalCityList.push(testObj);
  350. });
  351. this.setData({
  352. completeList: finalCityList
  353. });
  354. } else if (tempChinese[0]) {
  355. tempChinese.map(item => {
  356. let testObj = {};
  357. testObj.city = item.city;
  358. testObj.code = item.code;
  359. finalCityList.push(testObj);
  360. });
  361. this.setData({
  362. completeList: finalCityList
  363. });
  364. } else {
  365. return;
  366. }
  367. }
  368. }
  369. };
  370. </script>
  371. <style lang="scss">
  372. .container-inner {
  373. display: flex;
  374. flex-direction: row-reverse;
  375. }
  376. .container {
  377. flex-grow: 1;
  378. display: flex;
  379. flex-direction: column;
  380. padding: 0rpx;
  381. }
  382. input {
  383. text-align: center;
  384. font-size: 32rpx;
  385. padding: 5px;
  386. }
  387. .searchLetter {
  388. flex-shrink: 0;
  389. width: 80rpx;
  390. text-align: center;
  391. display: flex;
  392. flex-direction: column;
  393. color: #666;
  394. }
  395. .searchLetter view {
  396. margin-top: 20rpx;
  397. }
  398. .touchClass {
  399. background-color: #fff;
  400. color: #fff;
  401. padding-top: 16rpx;
  402. padding-bottom: 16rpx;
  403. }
  404. .showSlectedLetter {
  405. background-color: rgba(0, 0, 0, 0.5);
  406. color: #fff;
  407. display: flex;
  408. justify-content: center;
  409. align-items: center;
  410. position: fixed;
  411. top: 50%;
  412. left: 50%;
  413. margin: -100rpx;
  414. width: 200rpx;
  415. height: 200rpx;
  416. border-radius: 20rpx;
  417. font-size: 52rpx;
  418. z-index: 1;
  419. }
  420. .selection {
  421. display: flex;
  422. width: 100%;
  423. flex-direction: column;
  424. margin-top: 10rpx;
  425. }
  426. .selectCity {
  427. padding: 16rpx;
  428. background-color: #FFFFFF;
  429. margin-bottom: -10rpx;
  430. }
  431. .item_letter {
  432. color: $base-btn-color;
  433. display: flex;
  434. background-color: #f5f5f5;
  435. height: 40rpx;
  436. padding-left: 34rpx;
  437. align-items: center;
  438. font-size: 24rpx;
  439. }
  440. .item_city {
  441. display: flex;
  442. background-color: #fff;
  443. height: 100rpx;
  444. padding-left: 34rpx;
  445. align-items: center;
  446. border-bottom: 1rpx solid #ededed;
  447. font-size: 24rpx;
  448. }
  449. .hotcity-common {
  450. font-size: 24rpx;
  451. color: #666;
  452. padding-bottom: 0;
  453. margin: 8rpx 0;
  454. margin-left: 16rpx;
  455. }
  456. .hotCity {
  457. padding-right: 50rpx;
  458. margin: auto;
  459. }
  460. .thisCityName {
  461. display: inline-block;
  462. border: 1rpx solid $base-btn-color;
  463. border-radius: 8rpx;
  464. padding: 16rpx 20rpx;
  465. font-size: 24rpx;
  466. color: $base-btn-color;
  467. text-align: center;
  468. min-width: 149.5rpx;
  469. margin: 16rpx 0;
  470. }
  471. .thishotText {
  472. color: $base-btn-color;
  473. font-size: 20rpx;
  474. margin: 0 !important;
  475. }
  476. .slectCity {
  477. border-color: $base-btn-color !important;
  478. }
  479. .slectCity view {
  480. color: $base-btn-color !important;
  481. }
  482. .weui-grid {
  483. padding: 18rpx 0;
  484. width: 200rpx;
  485. box-sizing: border-box;
  486. border: 1rpx solid #c8c8c8;
  487. border-radius: 8rpx;
  488. background-color: white;
  489. margin: 8rpx 0;
  490. }
  491. .weui-grids {
  492. display: flex;
  493. flex-direction: row;
  494. justify-content: space-between;
  495. }
  496. .weui-grid__label {
  497. display: block;
  498. text-align: center;
  499. color: #333;
  500. font-size: 24rpx;
  501. white-space: nowrap;
  502. text-overflow: ellipsis;
  503. overflow: hidden;
  504. }
  505. .ul {
  506. display: block;
  507. color: grey;
  508. margin-left: 20rpx;
  509. }
  510. .li {
  511. display: block;
  512. font-weight: 100;
  513. font-size: 28rpx;
  514. padding: 20rpx 0;
  515. border-bottom: 1rpx solid #efefef;
  516. }
  517. input {
  518. background-color: #fff;
  519. }
  520. .input {
  521. padding: 16rpx;
  522. border-bottom: 1rpx solid #f1f1f1;
  523. }
  524. .county {
  525. padding: 10rpx 0 0 20rpx;
  526. background-color: #FFFFFF;
  527. display: flex;
  528. flex-wrap: wrap;
  529. }
  530. </style>