SelectAddress.vue 4.6 KB

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