city-select.vue 9.5 KB

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