city-select.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <template>
  2. <!-- 城市选择-->
  3. <view class="city-select">
  4. <scroll-view :scroll-top="scrollTop" scroll-y="true" class="city-select-main" id="city-select-main" :scroll-into-view="toView">
  5. <!-- 预留搜索-->
  6. <view class="city-serach" v-if="isSearch"><input @input="keyInput" :placeholder="placeholder" class="city-serach-input" /></view>
  7. <!-- 当前定位城市 -->
  8. <view class="hot-title" v-if="activeCity && !serachCity">当前定位城市</view>
  9. <view class="hot-city" v-if="activeCity && !serachCity">
  10. <view class="hot-item" @click="cityTrigger(activeCity)">{{ activeCity[formatName] }}</view>
  11. </view>
  12. <!-- 热门城市 -->
  13. <view class="hot-title" v-if="hotCity.length > 0 && !serachCity">热门城市</view>
  14. <view class="hot-city" v-if="hotCity.length > 0 && !serachCity">
  15. <template v-for="(item, index) in hotCity">
  16. <view :key="index" @click="cityTrigger(item, 'hot')" class="hot-item">{{ item[formatName] }}</view>
  17. </template>
  18. </view>
  19. <!-- 城市列表(搜索前) -->
  20. <view class="citys" v-if="!serachCity">
  21. <view v-for="(city, index) in sortItems" :key="index" v-show="city.isCity" class="citys-row">
  22. <view class="citys-item-letter" :id="'city-letter-' + (city.name === '#' ? '0' : city.name)">{{ city.name }}</view>
  23. <view class="citys-item" v-for="(item, inx) in city.citys" :key="inx" @click="cityTrigger(item)">{{ item.cityName }}</view>
  24. </view>
  25. </view>
  26. <!-- 城市列表(搜索后) -->
  27. <view class="citys" v-if="serachCity">
  28. <view v-for="(item, index) in searchDatas" :key="index" class="citys-row">
  29. <view class="citys-item" :key="inx" @click="cityTrigger(item)">{{ item.name }}</view>
  30. </view>
  31. </view>
  32. </scroll-view>
  33. <!-- 城市选择索引-->
  34. <view class="city-indexs-view" v-if="!serachCity">
  35. <view class="city-indexs">
  36. <view v-for="(cityIns, index) in handleCity" class="city-indexs-text" v-show="cityIns.isCity" :key="index" @click="cityindex(cityIns.forName)">
  37. {{ cityIns.name }}
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import citySelect from './citySelect.js';
  45. export default {
  46. props: {
  47. //查询提示文字
  48. placeholder: {
  49. type: String,
  50. default: '请输入城市名称'
  51. },
  52. //传入要排序的名称
  53. formatName: {
  54. type: String,
  55. default: 'cityName'
  56. },
  57. //当前定位城市
  58. activeCity: {
  59. type: Object,
  60. default: () => null
  61. },
  62. //热门城市
  63. hotCity: {
  64. type: Array,
  65. default: () => []
  66. },
  67. //城市数据
  68. obtainCitys: {
  69. type: Array,
  70. default: () => []
  71. },
  72. //是否有搜索
  73. isSearch: {
  74. type: Boolean,
  75. default: true
  76. }
  77. },
  78. data() {
  79. return {
  80. toView: 'city-letter-Find', //锚链接 初始值
  81. scrollTop: 0, //scroll-view 滑动的距离
  82. cityindexs: [], // 城市索引
  83. activeCityIndex: '', // 当前所在的城市索引
  84. handleCity: [], // 处理后的城市数据
  85. serachCity: '', // 搜索的城市
  86. cityData: []
  87. };
  88. },
  89. computed: {
  90. /**
  91. * @desc 城市列表排序
  92. * @return Array
  93. */
  94. sortItems() {
  95. for (let index = 0; index < this.handleCity.length; index++) {
  96. if (this.handleCity[index].isCity) {
  97. let cityArr = this.handleCity[index].citys;
  98. cityArr = cityArr.sort(function(a, b) {
  99. var value1 = a.unicode;
  100. var value2 = b.unicode;
  101. return value1 - value2;
  102. });
  103. }
  104. }
  105. return this.handleCity;
  106. },
  107. /**
  108. * @desc 搜索后的城市列表
  109. * @return Array
  110. */
  111. searchDatas() {
  112. var searchData = [];
  113. for (let i = 0; i < this.cityData.length; i++) {
  114. if (this.cityData[i][this.formatName].indexOf(this.serachCity) !== -1) {
  115. searchData.push({
  116. oldData: this.cityData[i],
  117. name: this.cityData[i][this.formatName]
  118. });
  119. }
  120. }
  121. return searchData;
  122. }
  123. },
  124. created() {
  125. // 初始化城市数据
  126. this.cityData = this.obtainCitys;
  127. this.initializationCity();
  128. this.buildCityindexs();
  129. },
  130. watch: {
  131. obtainCitys(newData) {
  132. this.updateCitys(newData);
  133. }
  134. },
  135. methods: {
  136. /**
  137. * @desc 初始化
  138. */
  139. updateCitys(data) {
  140. if (data && data.length) {
  141. this.cityData = data;
  142. this.initializationCity();
  143. this.buildCityindexs();
  144. }
  145. },
  146. /**
  147. * @desc 监听输入框的值
  148. */
  149. keyInput(event) {
  150. this.serachCity = event.detail.value;
  151. },
  152. /**
  153. * @desc 初始化城市数据
  154. * @return undefind
  155. */
  156. initializationCity() {
  157. this.handleCity = [];
  158. const cityLetterArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '#'];
  159. for (let index = 0; index < cityLetterArr.length; index++) {
  160. this.handleCity.push({
  161. name: cityLetterArr[index],
  162. isCity: false, // 用于区分是否含有当前字母开头的城市
  163. citys: [], // 存放城市首字母含是此字母的数组
  164. forName: 'city-letter-' + (cityLetterArr[index] == '#' ? '0' : cityLetterArr[index]) //label的绑定
  165. });
  166. }
  167. },
  168. /**
  169. * @desc 得到城市的首字母
  170. * @param str String
  171. */
  172. getLetter(str) {
  173. return citySelect.getFirstLetter(str[0]);
  174. },
  175. /**
  176. * @desc 构建城市索引
  177. * @return undefind
  178. */
  179. buildCityindexs() {
  180. this.cityindexs = [];
  181. for (let i = 0; i < this.cityData.length; i++) {
  182. // 获取首字母
  183. const cityLetter = this.getLetter(this.cityData[i][this.formatName]).firstletter;
  184. // 获取当前城市首字母的unicode,用作后续排序
  185. const unicode = this.getLetter(this.cityData[i][this.formatName]).unicode;
  186. const index = this.cityIndexPosition(cityLetter);
  187. if (this.cityindexs.indexOf(cityLetter) === -1) {
  188. this.handleCity[index].isCity = true;
  189. this.cityindexs.push(cityLetter);
  190. }
  191. this.handleCity[index].citys.push({
  192. cityName: this.cityData[i][this.formatName],
  193. unicode: unicode,
  194. oldData: this.cityData[i]
  195. });
  196. }
  197. },
  198. /**
  199. * @desc 滑动到城市索引所在的地方
  200. * @param id String 城市索引
  201. */
  202. cityindex(id) {
  203. this.toView = id;
  204. // //创建节点查询器
  205. // const query = uni.createSelectorQuery().in(this)
  206. // var that = this
  207. // that.scrollTop = 0
  208. // //滑动到指定位置(解决方法:重置到顶部,重新计算,影响:页面会闪一下)
  209. // setTimeout(() => {
  210. // query
  211. // .select('#city-letter-' + (id === '#' ? '0' : id))
  212. // .boundingClientRect(data => {
  213. // // console.log("得到布局位置信息" + JSON.stringify(data));
  214. // // console.log("节点离页面顶部的距离为" + data.top);
  215. // data ? (that.scrollTop = data.top) : void 0
  216. // })
  217. // .exec()
  218. // }, 0)
  219. },
  220. /**
  221. * @desc 获取城市首字母的unicode
  222. * @param letter String 城市索引
  223. */
  224. cityIndexPosition(letter) {
  225. if (!letter) {
  226. return '';
  227. }
  228. const ACode = 65;
  229. return letter === '#' ? 26 : letter.charCodeAt(0) - ACode;
  230. },
  231. /** @desc 城市列表点击事件
  232. * @param Object
  233. */
  234. cityTrigger(item) {
  235. // 传值到父组件
  236. this.$emit('cityClick', item.oldData ? item.oldData : item);
  237. }
  238. }
  239. };
  240. </script>
  241. <style lang="scss">
  242. //宽度转换vw
  243. @function vww($number) {
  244. @return ($number / 375) * 750 + rpx;
  245. }
  246. view {
  247. box-sizing: border-box;
  248. }
  249. .city-serach {
  250. width: 100%;
  251. color: #4a4a4a;
  252. padding: 0 vww(10);
  253. &-input {
  254. margin: vww(10) 0;
  255. height: vww(40);
  256. line-height: vww(40);
  257. font-size: vww(14);
  258. padding: 0 vww(5);
  259. border: 1px solid #4d8cfd;
  260. border-radius: 3px;
  261. }
  262. }
  263. .city-select-main {
  264. position: relative;
  265. // overflow: scroll;
  266. // -webkit-overflow-scrolling: touch;
  267. width: 100%;
  268. height: 100%;
  269. background: #f6f5fa;
  270. // overflow-y: auto;
  271. }
  272. .city-select {
  273. position: relative;
  274. width: 100vw;
  275. height: 100vh;
  276. background: #f6f5fa;
  277. // 热门城市
  278. .hot-title {
  279. padding-left: vww(23);
  280. width: 100vw;
  281. font-size: 14px;
  282. line-height: vww(40);
  283. color: #9b9b9b;
  284. }
  285. .hot-city {
  286. padding-left: vww(23);
  287. padding-right: vww(20);
  288. overflow: hidden;
  289. width: 100vw;
  290. .hot-item {
  291. float: left;
  292. padding: 0 vww(5);
  293. margin-right: vww(16);
  294. margin-bottom: vww(6);
  295. overflow: hidden;
  296. width: vww(100);
  297. height: vww(31);
  298. font-size: 14px;
  299. text-align: center;
  300. display: -webkit-box;
  301. -webkit-box-orient: vertical;
  302. -webkit-line-clamp: 1;
  303. line-height: vww(31);
  304. color: #4a4a4a;
  305. background: #fff;
  306. border: 1px solid #ebebf0;
  307. &:nth-child(3n) {
  308. margin-right: 0;
  309. }
  310. }
  311. .hot-hidden {
  312. display: none;
  313. margin-right: 0;
  314. }
  315. }
  316. .citys {
  317. .citys-row {
  318. padding-left: vww(18);
  319. width: 100%;
  320. font-size: 14px;
  321. background: #fff;
  322. .citys-item-letter {
  323. margin-left: vww(-18);
  324. padding-left: vww(18);
  325. margin-top: -1px;
  326. width: 100vw;
  327. line-height: vww(30);
  328. color: #9b9b9b;
  329. background: #f6f5fa;
  330. border-top: none;
  331. }
  332. .citys-item {
  333. width: 100%;
  334. line-height: vww(50);
  335. color: #4a4a4a;
  336. border-bottom: 1px solid #ebebf0;
  337. &:last-child {
  338. border: none;
  339. }
  340. }
  341. }
  342. }
  343. .city-indexs-view {
  344. position: absolute;
  345. right: 0;
  346. top: 0;
  347. z-index: 999;
  348. display: flex;
  349. width: vww(20);
  350. height: 100%;
  351. text-align: center;
  352. .city-indexs {
  353. width: vww(20);
  354. text-align: center;
  355. vertical-align: middle;
  356. align-self: center;
  357. .city-indexs-text {
  358. margin-bottom: vww(10);
  359. width: vww(20);
  360. font-size: 12px;
  361. color: #4d8cfd;
  362. &:last-child {
  363. margin-bottom: 0;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. </style>