index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view class="wrapper">
  3. <view class="tit">
  4. 选择地址
  5. </view>
  6. <view class="nav flex">
  7. <view class="nav-item clamp" v-for="(item,index) in navList" :key="index" :class="{'choose': current == index}" @click="getCity(index)">
  8. {{item.label}}
  9. </view>
  10. </view>
  11. <view class="">
  12. <scroll-view scroll-y="true" style="height: 500rpx;">
  13. <view class="city" v-for="(city,cindex) in list" :key="cindex" @click="chooseCity(city)">
  14. {{city.label}}
  15. </view>
  16. </scroll-view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. import { getCity} from '@/api/user.js'
  22. export default {
  23. data() {
  24. return {
  25. list: [],
  26. current: 0,
  27. navList: [{label: '请选择',value: 0}]
  28. }
  29. },
  30. props: {
  31. lists: {
  32. type: Array,
  33. default: () => []
  34. }
  35. },
  36. mounted() {
  37. if(this.lists.length > 0) {
  38. this.setBase()
  39. }else {
  40. this.getCity(0)
  41. }
  42. },
  43. methods: {
  44. setBase() {
  45. let len = this.lists.length
  46. this.current = len - 1
  47. this.navList = this.lists
  48. this.getCity(this.current)
  49. },
  50. chooseCity(item) {
  51. if(this.current == 3) {
  52. this.$set(this.navList,this.current,item)
  53. this.$emit('chooseEnd',this.navList)
  54. }else {
  55. this.$set(this.navList,this.current,item)
  56. this.$set(this.navList,this.current + 1,{label: '请选择',value: 0})
  57. this.navList = this.navList.slice(0,this.current +2)
  58. this.list = []
  59. this.getCity(++this.current)
  60. }
  61. },
  62. getCity(index) {
  63. this.current = index
  64. let qdata = {}
  65. if(index == 0) {
  66. qdata.pid = 0
  67. }else {
  68. qdata.pid = this.navList[index - 1].value
  69. }
  70. getCity(qdata).then(res => {
  71. this.list = res.data.map(item => {
  72. return {
  73. label: item.label,
  74. value: item.value
  75. }
  76. })
  77. })
  78. },
  79. }
  80. }
  81. </script>
  82. <style lang="scss" scoped>
  83. $action-c: #ef5656;
  84. $fz: 30rpx;
  85. // .on
  86. .wrapper {
  87. padding: 20rpx 0;
  88. width: 750rpx;
  89. background-color: #fff;
  90. border-radius: 20rpx 20rpx 0 0;
  91. font-size: $fz;
  92. // position: fixed;
  93. // bottom: 0;
  94. .tit {
  95. padding: 20rpx;
  96. font-size: $fz;
  97. font-weight: bold;
  98. }
  99. }
  100. .nav {
  101. justify-content: flex-start;
  102. border-bottom: 1px solid #eee;
  103. margin-bottom: 15rpx;
  104. .nav-item {
  105. width: 25%;
  106. text-align: center;
  107. padding: 20rpx 10rpx;
  108. }
  109. .choose {
  110. position: relative;
  111. color: #ef5656;
  112. &::after {
  113. content: '';
  114. position: absolute;
  115. bottom: 0rpx;
  116. left: 0;
  117. right: 0;
  118. width: 40px;
  119. height: 2px;
  120. background-color: $action-c;
  121. margin: auto;
  122. }
  123. }
  124. }
  125. .city {
  126. padding: 15rpx 0 10rpx 50rpx;
  127. }
  128. </style>