SelectAddress.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <div class="base-select-address mobile-modal" :class="className" 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. inFix: {
  24. type: Boolean,
  25. default: false
  26. },
  27. className: {
  28. type: String,
  29. default: ''
  30. }
  31. },
  32. data () {
  33. return {
  34. addressData: '',
  35. provinces: [],
  36. activeObj: {
  37. // 省
  38. province: -1,
  39. // 市
  40. city: -1,
  41. // 区
  42. area: -1
  43. }
  44. }
  45. },
  46. created () {
  47. this.$http.get('/data/city.json').then(res => {
  48. this.addressData = res.data
  49. for (let provinceAttr in this.addressData) {
  50. this.provinces.push(provinceAttr)
  51. }
  52. })
  53. },
  54. watch: {
  55. isShow: function (val) {
  56. if (val) {
  57. this.activeObj = {
  58. // 省
  59. province: -1,
  60. // 市
  61. city: -1,
  62. // 区
  63. area: -1
  64. }
  65. }
  66. }
  67. },
  68. computed: {
  69. currentType () {
  70. if (this.activeObj.area > -1) {
  71. return 'full'
  72. } else if (this.activeObj.city > -1) {
  73. return 'area'
  74. } else if (this.activeObj.province > -1) {
  75. return 'city'
  76. } else {
  77. return 'province'
  78. }
  79. },
  80. currentAddress () {
  81. let obj = {
  82. province: '',
  83. city: '',
  84. area: ''
  85. }
  86. if (this.activeObj.province > -1) {
  87. obj.province = this.provinces[this.activeObj.province]
  88. }
  89. if (this.activeObj.city > -1) {
  90. obj.city = this.cityObject.attrArr[this.activeObj.city]
  91. }
  92. if (this.activeObj.area > -1) {
  93. obj.area = this.cityObject.valArr[this.activeObj.city][this.activeObj.area]
  94. }
  95. return obj
  96. },
  97. cityObject () {
  98. let attrArr = []
  99. let valArr = []
  100. let tmp = this.addressData[this.provinces[this.activeObj.province]]
  101. for (let attr in tmp) {
  102. attrArr.push(attr)
  103. valArr.push(tmp[attr])
  104. }
  105. return {
  106. attrArr: attrArr,
  107. valArr: valArr
  108. }
  109. },
  110. currentArr () {
  111. let arr = []
  112. if (this.currentType === 'area') {
  113. arr = this.cityObject.valArr[this.activeObj.city]
  114. } else if (this.currentType === 'city') {
  115. arr = this.cityObject.attrArr
  116. } else if (this.currentType === 'province') {
  117. arr = this.provinces
  118. }
  119. return arr
  120. }
  121. },
  122. methods: {
  123. checkItem (index) {
  124. if (this.currentType === 'province') {
  125. this.activeObj.province = index
  126. this.activeObj.area = -1
  127. this.activeObj.city = -1
  128. } else if (this.currentType === 'city') {
  129. this.activeObj.city = index
  130. this.activeObj.area = -1
  131. } else {
  132. this.activeObj.area = index
  133. this.$emit('closeAction', this.currentAddress)
  134. }
  135. },
  136. isActive (type) {
  137. return (type === 'province' && this.currentType === 'city') || (type === 'city' && this.currentType === 'area') || (type === 'area' && this.currentType === 'full')
  138. },
  139. resetAddr (addrName) {
  140. if (addrName === 'province') {
  141. this.activeObj.province = -1
  142. this.activeObj.area = -1
  143. this.activeObj.city = -1
  144. } else if (addrName === 'city') {
  145. this.activeObj.city = -1
  146. this.activeObj.area = -1
  147. } else {
  148. this.activeObj.area = -1
  149. }
  150. }
  151. }
  152. }
  153. </script>
  154. <style lang="scss" scoped>
  155. .bs-wrap {
  156. position: absolute;
  157. bottom: 0;
  158. background: #fff;
  159. width: 100%;
  160. height: 8.17rem;
  161. .bs-wrap-title {
  162. text-align: center;
  163. font-size: .3rem;
  164. color: #666;
  165. margin: .34rem 0;
  166. i {
  167. font-size: .24rem;
  168. color: #666;
  169. position: absolute;
  170. right: .12rem;
  171. top: .09rem;
  172. display: block;
  173. width: .5rem;
  174. height: .5rem;
  175. text-align: right;
  176. }
  177. }
  178. .bs-selected-list {
  179. border-bottom: 1px solid #e7e8ec;
  180. li {
  181. height: .48rem;
  182. line-height: .48rem;
  183. margin: 0 .33rem;
  184. font-size: .28rem;
  185. &.active {
  186. border-bottom: .02rem solid #f38c8c;
  187. }
  188. }
  189. }
  190. .bs-current-list {
  191. height: 100%;
  192. overflow-y: auto;
  193. padding-bottom: 2rem;
  194. li {
  195. padding: .27rem .39rem;
  196. &:hover, &:active, &:focus {
  197. background: #f7f7f7;
  198. }
  199. }
  200. }
  201. }
  202. .fix-bs-address {
  203. background: #fff;
  204. .bs-wrap {
  205. border-top: 1px solid #ccc;
  206. border-bottom: 1px solid #ccc;
  207. height: 100%;
  208. .bs-current-list {
  209. height: 7.3rem;
  210. }
  211. }
  212. }
  213. /*.inWrap .bs-current-list {
  214. padding-bottom: 2rem;
  215. }*/
  216. </style>