SelectAddress.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <div class="base-select-address mobile-modal" v-if="isShow">
  3. <div class="bs-wrap">
  4. <p class="bs-wrap-title">选择地址<i class="iconfont icon-guanbi1" @click="$emit('closeAction')"></i></p>
  5. <ul class="bs-selected-list">
  6. <li class="inline-block" v-for="(addrVal, addrName, index) in currentAddress" :class="{active: isActive(addrName)}" @click="resetAddr(addrName)">{{addrVal}}</li>
  7. </ul>
  8. <ul class="bs-current-list">
  9. <li v-for="(item, index) in currentArr" @click="checkItem(index)">
  10. {{item}}
  11. </li>
  12. </ul>
  13. </div>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. props: {
  19. isShow: {
  20. type: Boolean,
  21. default: false
  22. }
  23. },
  24. data () {
  25. return {
  26. addressData: '',
  27. provinces: [],
  28. activeObj: {
  29. // 省
  30. province: -1,
  31. // 市
  32. city: -1,
  33. // 区
  34. area: -1
  35. }
  36. }
  37. },
  38. created () {
  39. this.$http.get('/data/city.json').then(res => {
  40. this.addressData = res.data
  41. for (let provinceAttr in this.addressData) {
  42. this.provinces.push(provinceAttr)
  43. }
  44. })
  45. },
  46. computed: {
  47. currentType () {
  48. if (this.activeObj.area > -1) {
  49. return 'full'
  50. } else if (this.activeObj.city > -1) {
  51. return 'area'
  52. } else if (this.activeObj.province > -1) {
  53. return 'city'
  54. } else {
  55. return 'province'
  56. }
  57. },
  58. currentAddress () {
  59. let obj = {
  60. province: '',
  61. city: '',
  62. area: ''
  63. }
  64. if (this.activeObj.province > -1) {
  65. obj.province = this.provinces[this.activeObj.province]
  66. }
  67. if (this.activeObj.city > -1) {
  68. obj.city = this.cityObject.attrArr[this.activeObj.city]
  69. }
  70. if (this.activeObj.area > -1) {
  71. obj.area = this.cityObject.valArr[this.activeObj.city][this.activeObj.area]
  72. }
  73. return obj
  74. },
  75. cityObject () {
  76. let attrArr = []
  77. let valArr = []
  78. let tmp = this.addressData[this.provinces[this.activeObj.province]]
  79. for (let attr in tmp) {
  80. attrArr.push(attr)
  81. valArr.push(tmp[attr])
  82. }
  83. return {
  84. attrArr: attrArr,
  85. valArr: valArr
  86. }
  87. },
  88. currentArr () {
  89. let arr = []
  90. if (this.currentType === 'area') {
  91. arr = this.cityObject.valArr[this.activeObj.city]
  92. } else if (this.currentType === 'city') {
  93. arr = this.cityObject.attrArr
  94. } else if (this.currentType === 'province') {
  95. arr = this.provinces
  96. }
  97. return arr
  98. }
  99. },
  100. methods: {
  101. checkItem (index) {
  102. if (this.currentType === 'province') {
  103. this.activeObj.province = index
  104. this.activeObj.area = -1
  105. this.activeObj.city = -1
  106. } else if (this.currentType === 'city') {
  107. this.activeObj.city = index
  108. this.activeObj.area = -1
  109. } else {
  110. this.activeObj.area = index
  111. this.$emit('closeAction', this.currentAddress)
  112. }
  113. },
  114. isActive (type) {
  115. return (type === 'province' && this.currentType === 'city') || (type === 'city' && this.currentType === 'area') || (type === 'area' && this.currentType === 'full')
  116. },
  117. resetAddr (addrName) {
  118. if (addrName === 'province') {
  119. this.activeObj.province = -1
  120. this.activeObj.area = -1
  121. this.activeObj.city = -1
  122. } else if (addrName === 'city') {
  123. this.activeObj.city = -1
  124. this.activeObj.area = -1
  125. } else {
  126. this.activeObj.area = -1
  127. }
  128. }
  129. }
  130. }
  131. </script>
  132. <style lang="scss" scoped>
  133. .bs-wrap {
  134. position: absolute;
  135. bottom: 0;
  136. background: #fff;
  137. width: 100%;
  138. height: 8.17rem;
  139. .bs-wrap-title {
  140. text-align: center;
  141. font-size: .3rem;
  142. color: #666;
  143. margin: .34rem 0;
  144. i {
  145. font-size: .24rem;
  146. color: #666;
  147. position: absolute;
  148. right: .12rem;
  149. top: .09rem;
  150. }
  151. }
  152. .bs-selected-list {
  153. border-bottom: 1px solid #e7e8ec;
  154. li {
  155. height: .48rem;
  156. line-height: .48rem;
  157. margin: 0 .33rem;
  158. font-size: .28rem;
  159. &.active {
  160. border-bottom: .02rem solid #f38c8c;
  161. }
  162. }
  163. }
  164. .bs-current-list {
  165. height: 100%;
  166. overflow-y: auto;
  167. li {
  168. padding: .27rem .39rem;
  169. &:hover, &:active, &:focus {
  170. background: #f7f7f7;
  171. }
  172. }
  173. }
  174. }
  175. </style>