city.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <!-- 选择城市页面 -->
  2. <template>
  3. <view>
  4. <view class="input">
  5. <input placeholder="输入城市名或拼音查询" placeholder-style="font-size: 13px" :value="inputName" @input="bindKeyInput"
  6. @blur="bindBlur" />
  7. </view>
  8. <view class="container-inner">
  9. <view class="searchLetter touchClass">
  10. <view class="thishotText" @click="hotCity">
  11. <view style="margin-top:0;">当前</view>
  12. <view style="margin-top:0;">热门</view>
  13. </view>
  14. <view v-for="(item, idx) in searchLetter" :key="idx" style="color:#8BC34A;font-size:20upx;"
  15. :data-letter="item.name" @click="clickLetter">
  16. {{ item.name }}
  17. </view>
  18. </view>
  19. <view class="container">
  20. <view v-if="isShowLetter">
  21. <view class="showSlectedLetter">
  22. {{ toastShowLetter }}
  23. </view>
  24. </view>
  25. <scroll-view scroll-y="true" v-bind:style="{height: winHeight + 'px'}" :scroll-into-view="scrollTopId">
  26. <view class="ul" id="completelist">
  27. <view class="li" v-for="(item, idx) in completeList" :key="idx" :data-city="item.city"
  28. :data-code="item.code" @click="bindCity">
  29. {{ item.city }}
  30. </view>
  31. </view>
  32. <view class="selectCity">
  33. <view class="hotcity-common" @click="reGetLocation" id="currentcity">定位城市</view>
  34. <view class="thisCityName" @click="bindCity" :data-code="currentCityCode"
  35. :data-city="currentCity">{{ currentCity }}</view>
  36. <view class="hotcity-common">热门城市</view>
  37. <view class="weui-grids">
  38. <view class="weui-grid" :data-code="110100" data-city="北京市" @click="bindCity">
  39. <view class="weui-grid__label">北京市</view>
  40. </view>
  41. <view class="weui-grid" :data-code="310100" data-city="上海市" @click="bindCity">
  42. <view class="weui-grid__label">上海市</view>
  43. </view>
  44. <view class="weui-grid" :data-code="440100" data-city="广州市" @click="bindCity">
  45. <view class="weui-grid__label">广州市</view>
  46. </view>
  47. </view>
  48. <view class="weui-grids">
  49. <view class="weui-grid" :data-code="440300" data-city="深圳市" @click="bindCity">
  50. <view class="weui-grid__label">深圳市</view>
  51. </view>
  52. <view class="weui-grid" :data-code="330100" data-city="杭州市" @click="bindCity">
  53. <view class="weui-grid__label">杭州市</view>
  54. </view>
  55. <view class="weui-grid" :data-code="320100" data-city="南京市" @click="bindCity">
  56. <view class="weui-grid__label">南京市</view>
  57. </view>
  58. </view>
  59. <view class="weui-grids">
  60. <view class="weui-grid" :data-code="420100" data-city="武汉市" @click="bindCity">
  61. <view class="weui-grid__label">武汉市</view>
  62. </view>
  63. <view class="weui-grid" :data-code="120100" data-city="天津市" @click="bindCity">
  64. <view class="weui-grid__label">天津市</view>
  65. </view>
  66. <view class="weui-grid" :data-code="610100" data-city="西安市" @click="bindCity">
  67. <view class="weui-grid__label">西安市</view>
  68. </view>
  69. </view>
  70. </view>
  71. <view class="selection" v-for="(item, idx) in cityList" :key="idx">
  72. <view class="item_letter" :id="item.initial">{{ item.initial }}</view>
  73. <view class="item_city" v-for="(cityItem, index) in item.cityInfo" :key="index"
  74. :data-code="cityItem.code" :data-city="cityItem.city" @click="bindCity">
  75. {{cityItem.city}}
  76. </view>
  77. </view>
  78. </scroll-view>
  79. </view>
  80. </view>
  81. </view>
  82. </template>
  83. <script>
  84. import commonData from '../../commonData.js'
  85. import commonFun from '../../commonFun.js'
  86. import city from '../../common/city.js';
  87. import district from '../../common/district.js';
  88. // console.log(district)
  89. export default {
  90. data() {
  91. return {
  92. currentCityCode: '120100',
  93. searchLetter: [],
  94. showLetter: '',
  95. winHeight: 0,
  96. cityList: [],
  97. isShowLetter: false,
  98. // 置顶id
  99. scrollTopId: '',
  100. hotcityList: [],
  101. inputName: '',
  102. completeList: [],
  103. condition: false,
  104. detailAddress: '',
  105. toastShowLetter: '',
  106. currentCity: '北京市'
  107. };
  108. },
  109. onLoad() {
  110. const searchLetter = city.searchLetter;
  111. const cityList = city.cityList();
  112. const sysInfo = uni.getSystemInfoSync();
  113. // console.log(sysInfo);
  114. const winHeight = sysInfo.windowHeight;
  115. const itemH = winHeight / searchLetter.length;
  116. let tempArr = [];
  117. searchLetter.map(
  118. (item, index) => {
  119. let temp = {};
  120. temp.name = item;
  121. temp.tHeight = index * itemH;
  122. temp.bHeight = (index + 1) * itemH;
  123. tempArr.push(temp);
  124. }
  125. );
  126. this.winHeight = winHeight;
  127. this.itemH = itemH;
  128. this.searchLetter = tempArr;
  129. this.cityList = cityList;
  130. this.getLocation();
  131. },
  132. methods: {
  133. getLocation() {
  134. commonFun.getPositionLocal(res => {
  135. this.currentCity = res.city || '北京市'
  136. })
  137. },
  138. clickLetter(e) {
  139. const showLetter = e.currentTarget.dataset.letter;
  140. this.toastShowLetter = showLetter;
  141. this.isShowLetter = true;
  142. this.scrollTopId = showLetter;
  143. const self = this;
  144. setTimeout(() => {
  145. self.isShowLetter = false;
  146. }, 500);
  147. },
  148. reGetLocation() {
  149. // this.getLocation();
  150. },
  151. // 选择城市触发
  152. bindCity(e) {
  153. this.condition = true;
  154. //缓存选择的城市
  155. uni.setStorageSync('currentSelectCity', e.currentTarget.dataset.city);
  156. // let code = e.currentTarget.dataset.code
  157. let city = e.currentTarget.dataset.city
  158. let code = ''
  159. console.log(city)
  160. for (let i in district) {
  161. if (i.length == 8) {
  162. for (let j in district[i]) {
  163. if (district[i][j] == city) {
  164. code = j
  165. }
  166. }
  167. }
  168. }
  169. for (let i in district) {
  170. if (i.indexOf(code) != -1 && i.indexOf(0) == 0) {
  171. uni.setStorageSync('currentSelectDistrict', JSON.stringify(district[i]));
  172. uni.reLaunch({
  173. url: './index?city = ' + city
  174. })
  175. // uni.navigateBack({
  176. // delta: 1
  177. // });
  178. return
  179. }
  180. }
  181. uni.navigateBack({
  182. delta: 1
  183. });
  184. return
  185. },
  186. hotCity() {
  187. this.scrollTopId = 'currentcity';
  188. },
  189. bindScroll(e) {
  190. // console.log(e.detail);
  191. },
  192. bindBlur(e) {
  193. this.inputName = '';
  194. },
  195. bindKeyInput(e) {
  196. this.inputName = e.mp.detail.value;
  197. // 空搜索框时 取消匹配显示
  198. if (this.inputName.length < 1) {
  199. this.completeList = [];
  200. }
  201. this.scrollTopId = 'completelist';
  202. this.auto();
  203. },
  204. auto() {
  205. let inputSd = this.inputName.trim();
  206. let sd = inputSd.toLowerCase();
  207. let num = sd.length;
  208. const cityList = city.cityObjs;
  209. let finalCityList = [];
  210. let temp = cityList.filter(
  211. item => {
  212. let text = item.short.slice(0, num).toLowerCase();
  213. // eslint-disable-next-line
  214. return (text && text == sd);
  215. }
  216. );
  217. // 在城市数据中,添加简拼到“shorter”属性,就可以实现简拼搜索
  218. let tempShorter = cityList.filter(
  219. itemShorter => {
  220. if (itemShorter.shorter) {
  221. let textShorter = itemShorter.shorter.slice(0, num).toLowerCase();
  222. // eslint-disable-next-line
  223. return (textShorter && textShorter == sd);
  224. }
  225. }
  226. );
  227. let tempChinese = cityList.filter(
  228. itemChinese => {
  229. let textChinese = itemChinese.city.slice(0, num);
  230. // eslint-disable-next-line
  231. return (textChinese && textChinese == sd);
  232. }
  233. );
  234. if (temp[0]) {
  235. temp.map(
  236. item => {
  237. let testObj = {};
  238. testObj.city = item.city;
  239. testObj.code = item.code;
  240. finalCityList.push(testObj);
  241. }
  242. );
  243. this.completeList = finalCityList;
  244. } else if (tempShorter[0]) {
  245. tempShorter.map(
  246. item => {
  247. let testObj = {};
  248. testObj.city = item.city;
  249. testObj.code = item.code;
  250. finalCityList.push(testObj);
  251. }
  252. );
  253. this.completeList = finalCityList;
  254. } else if (tempChinese[0]) {
  255. tempChinese.map(
  256. item => {
  257. let testObj = {};
  258. testObj.city = item.city;
  259. testObj.code = item.code;
  260. finalCityList.push(testObj);
  261. }
  262. );
  263. this.completeList = finalCityList;
  264. }
  265. }
  266. }
  267. };
  268. </script>
  269. <style>
  270. @import url("city.css");
  271. </style>