lee-select-city.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <view class="lee-select-city" :style="{ height }">
  3. <!-- 选择提示 -->
  4. <view class="lee-select-display" v-if="currentSelected.length">
  5. <view class="lee-select-display-item"
  6. v-for="(v, k) of currentSelected"
  7. :key="k"
  8. :class="{ active: currentLevel === k }"
  9. @click="skipLevel(k)"
  10. >{{ v.name }}</view>
  11. <view class="lee-select-display-item"
  12. v-if="placeholder">{{ placeholder }}</view>
  13. </view>
  14. <!-- 选择提示END -->
  15. <view class="swiper">
  16. <view class="swiper-wrapper" :style="{
  17. transform: `translateX(-${100 * currentLevel/3}%)`
  18. }">
  19. <!-- 省级 -->
  20. <scroll-view class="swiper-item" scroll-y>
  21. <view class="swiper-content">
  22. <!-- 当前定位 -->
  23. <block v-if="currentPosition">
  24. <view class="caption">
  25. <text>当前定位</text>
  26. <text class="action" @click="getNewPosition">重新定位</text>
  27. </view>
  28. <view class="current-position"
  29. @click="selectCurrentPosition">
  30. {{ currentPosition }}
  31. </view>
  32. </block>
  33. <!-- 当前定位END -->
  34. <!-- 热门城市 -->
  35. <block v-if="hotCities.length">
  36. <view class="caption">热门城市</view>
  37. <view class="gird">
  38. <view class="gird-item"
  39. v-for="(v, k) of hotCities"
  40. :key="k"
  41. >
  42. <view @click="hotCitySelectHandler(v)">{{ v[0] }}</view>
  43. </view>
  44. </view>
  45. </block>
  46. <!-- 热门城市END -->
  47. <view class="caption">选择省份/地区</view>
  48. <lee-latter-list
  49. :data="dataByLetter"
  50. :selected="currentSelected[0]"
  51. @select="selectPro"
  52. />
  53. </view>
  54. </scroll-view>
  55. <!-- 省级END -->
  56. <!-- 市级 -->
  57. <scroll-view class="swiper-item" scroll-y>
  58. <view class="swiper-content">
  59. <view class="caption">选择城市</view>
  60. <lee-latter-list
  61. :data="level2Data"
  62. :selected="currentSelected[1]"
  63. @select="selectCity"
  64. />
  65. </view>
  66. </scroll-view>
  67. <!-- 市级END -->
  68. <!-- 市级 -->
  69. <scroll-view class="swiper-item" scroll-y>
  70. <view class="swiper-content">
  71. <view class="caption">选择区/县</view>
  72. <lee-latter-list
  73. :data="level3Data"
  74. :selected="currentSelected[2]"
  75. @select="selectQu"
  76. />
  77. </view>
  78. </scroll-view>
  79. <!-- 市级END -->
  80. </view>
  81. </view>
  82. </view>
  83. </template>
  84. <script>
  85. import LeeLatterList from './lee-latter-list.vue'
  86. import cityData from './city.json'
  87. import hotCities from './hot-cities.json'
  88. export default {
  89. components: {
  90. LeeLatterList
  91. },
  92. props: {
  93. // 容器高度
  94. height: {
  95. type: String,
  96. default: '100%'
  97. // default: '100px'
  98. },
  99. // 城市级联数据
  100. data: {
  101. type: Array,
  102. default: () => JSON.parse(JSON.stringify(cityData))
  103. },
  104. // 热门城市
  105. hotCities: {
  106. type: Array,
  107. default: () => JSON.parse(JSON.stringify(hotCities))
  108. },
  109. // 重新定位
  110. getPosition: Function,
  111. // 默认选择
  112. defaultSelected: {
  113. type: Array,
  114. default: () => []
  115. },
  116. },
  117. data() {
  118. const currentSelected = []
  119. if (this.defaultSelected) {
  120. let current = this.data
  121. for (const name of this.defaultSelected) {
  122. const item = current.find(v => v.name === name)
  123. if (!item) {
  124. currentSelected = []
  125. break
  126. }
  127. currentSelected.push(item)
  128. current = item.children || []
  129. }
  130. }
  131. return {
  132. currentLevel: Math.min(2, this.defaultSelected.length),
  133. currentSelected,
  134. showPlaceholder: true,
  135. currentPosition: null
  136. }
  137. },
  138. computed: {
  139. // 获取按Letter分组后的一级列表
  140. dataByLetter() {
  141. return this.formatByLatter(this.data)
  142. },
  143. // 当前二级列表
  144. level2Data() {
  145. const lastLevel = this.currentSelected[0]
  146. if (!lastLevel) return []
  147. return this.formatByLatter(lastLevel.children || [])
  148. },
  149. // 当前三级列表
  150. level3Data() {
  151. const lastLevel = this.currentSelected[1]
  152. if (!lastLevel) return []
  153. return this.formatByLatter(lastLevel.children || [])
  154. },
  155. placeholder() {
  156. const ln = this.currentSelected.length
  157. if (ln === 0) return '请选择地区'
  158. if (ln === 1) return '请选择城市'
  159. if (ln === 2) return '请选择县'
  160. return ''
  161. }
  162. },
  163. async created() {
  164. await this.getNewPosition()
  165. console.log(this.data)
  166. },
  167. methods: {
  168. // 选择省份
  169. selectPro(item) {
  170. this.currentSelected = [item]
  171. console.log()
  172. if (this.level2Data.length) {
  173. this.currentLevel = 1
  174. } else {
  175. this.submit()
  176. }
  177. },
  178. // 选择城市
  179. selectCity(item) {
  180. this.currentSelected.splice(1)
  181. this.$set(this.currentSelected, 1, item)
  182. if (this.level3Data.length) {
  183. this.currentLevel = 2
  184. } else {
  185. this.submit()
  186. }
  187. },
  188. // 选择区/县
  189. selectQu(item) {
  190. this.currentSelected.splice(2)
  191. this.$set(this.currentSelected, 2, item)
  192. this.submit()
  193. this.$store.commit('setChoose',{
  194. chooseProvince: this.currentSelected[0].name,
  195. chooseCity: this.currentSelected[1].name,
  196. chooseDistrict: this.currentSelected[2].name,
  197. })
  198. // this.$api.prePage().reGetStoreList();
  199. uni.switchTab({
  200. url: '/pages/index/index'
  201. })
  202. },
  203. // 跳转到指定级数
  204. skipLevel(level) {
  205. this.currentLevel = level
  206. },
  207. // 提交当前选择
  208. submit() {
  209. const selected = this.currentSelected.slice(0)
  210. const simple = selected.map(v => v.name)
  211. this.$emit('submit', { simple, selected })
  212. },
  213. // 点击热门城市
  214. hotCitySelectHandler(item) {
  215. let current = this.data
  216. const selected = []
  217. for (const v of item[1]) {
  218. const node = current.find(m => m.name === v)
  219. if (!node) return
  220. current = node.children
  221. if (!current || current.length === 0) return
  222. selected.push(node)
  223. }
  224. this.currentSelected = selected
  225. const nextLevel = selected.length
  226. if (
  227. (nextLevel === 1 && this.level2Data.length) ||
  228. (nextLevel === 2 && this.level3Data.length)
  229. ) {
  230. this.currentLevel = nextLevel
  231. } else {
  232. this.submit()
  233. }
  234. },
  235. // 使用当前定位
  236. selectCurrentPosition() {
  237. this.$emit('select-current', this.currentPosition)
  238. },
  239. // 重新定位
  240. async getNewPosition() {
  241. if (typeof this.getPosition === 'function') {
  242. this.currentPosition = await this.getPosition()
  243. }
  244. },
  245. // 按latter格式化
  246. formatByLatter(list) {
  247. return list.reduce((map, item) => {
  248. if (!item.latter) return map
  249. let tmp = map.find(v => v[0] === item.latter)
  250. if (!tmp) {
  251. tmp = [item.latter, []]
  252. map.push(tmp)
  253. }
  254. tmp[1].push(item)
  255. tmp[1].sort((a, b) => {
  256. return a.name - b.name
  257. })
  258. return map
  259. }, []).sort((a, b) => {
  260. return a[0].charCodeAt(0) - b[0].charCodeAt(0)
  261. })
  262. }
  263. }
  264. }
  265. </script>
  266. <style lang="scss">
  267. $lee-spacing-small: 10upx;
  268. $lee-spacing-base: 30upx;
  269. $lee-spacing-large: 40upx;
  270. $lee-font-size-caption: 28upx;
  271. $lee-text-height: 60upx;
  272. .lee-select-city {
  273. display: flex;
  274. flex-direction: column;
  275. position: relative;
  276. background-color: $uni-bg-color;
  277. }
  278. .swiper {
  279. flex: 1;
  280. width: 100%;
  281. height: 0;
  282. overflow: hidden;
  283. position: relative;
  284. &-wrapper {
  285. width: 300%;
  286. height: 100%;
  287. display: flex;
  288. position: relative;
  289. }
  290. &-item {
  291. width: calc(100% / 3);
  292. height: 100%;
  293. position: relative;
  294. box-sizing: border-box;
  295. }
  296. &-content {
  297. position: relative;
  298. box-sizing: border-box;
  299. padding: $lee-spacing-base;
  300. > .caption {
  301. color: $uni-text-color-grey;
  302. font-size: $lee-font-size-caption;
  303. margin-bottom: $lee-spacing-base;
  304. > .action {
  305. float: right;
  306. }
  307. }
  308. }
  309. }
  310. // 已选择显示
  311. .lee-select-display {
  312. padding: $lee-spacing-base;
  313. box-shadow: 0 2upx 2upx rgba(0, 0, 0, .15);
  314. &-item {
  315. $height: 80upx;
  316. height: $height;
  317. line-height: $height;
  318. box-sizing: border-box;
  319. padding: 0 $lee-spacing-base;
  320. font-size: $lee-font-size-caption;
  321. border-left: 1px solid $uni-color-primary;
  322. position: relative;
  323. &.active {
  324. color: $uni-color-primary;
  325. }
  326. &::before {
  327. content: '';
  328. top: 0;
  329. left: -4upx;
  330. width: 8upx;
  331. height: 50%;
  332. background-color: $uni-bg-color;
  333. position: absolute;
  334. display: none;
  335. }
  336. &:first-child::before,
  337. &:last-child::before {
  338. display: block;
  339. }
  340. &:last-child::before {
  341. top: 50%;
  342. }
  343. &::after {
  344. $size: 14upx;
  345. top: 50%;
  346. left: 0;
  347. content: '';
  348. width: $size;
  349. height: $size;
  350. border-radius: 50%;
  351. position: absolute;
  352. background-color: $uni-color-primary;
  353. transform: translate(-50%, -50%);
  354. }
  355. &:last-child::after {
  356. background-color: $uni-text-color-grey;
  357. }
  358. }
  359. }
  360. // 热门城市
  361. .gird {
  362. $height: 80upx;
  363. display: flex;
  364. flex-wrap: wrap;
  365. position: relative;
  366. margin-bottom: $lee-spacing-base;
  367. &-item {
  368. width: 20%;
  369. height: $height;
  370. box-sizing: border-box;
  371. padding: $lee-spacing-small;
  372. > view {
  373. width: 100%;
  374. height: 100%;
  375. display: flex;
  376. align-items: center;
  377. justify-content: center;
  378. font-size: $lee-font-size-caption;
  379. background-color: $uni-bg-color-hover;
  380. border-radius: 8upx;
  381. }
  382. }
  383. }
  384. // 当前定位
  385. .current-position {
  386. $height: 80upx;
  387. height: $height;
  388. line-height: $height;
  389. text-align: center;
  390. font-size: $lee-font-size-caption;
  391. margin-bottom: $lee-spacing-base;
  392. color: $uni-color-primary;
  393. background-color: lighten($uni-color-primary, 40%);
  394. border-radius: 8upx;
  395. }
  396. </style>