SelectAddress.vue 5.2 KB

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