city-picker.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <u-popup
  3. v-model="value"
  4. mode="bottom"
  5. :popup="false"
  6. :mask="true"
  7. :closeable="true"
  8. :safe-area-inset-bottom="true"
  9. close-icon-color="#ffffff"
  10. :z-index="uZIndex"
  11. :maskCloseAble="maskCloseAble"
  12. @close="close"
  13. >
  14. <u-tabs v-if="value" :list="genTabsList" :is-scroll="true" :current="tabsIndex" @change="tabsChange" ref="tabs"></u-tabs>
  15. <view class="area-box">
  16. <view class="u-flex" :class="{ change: isChange }">
  17. <view class="area-item">
  18. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  19. <scroll-view :scroll-y="true" style="height: 100%">
  20. <u-cell-group>
  21. <u-cell-item v-for="(item, index) in addressList" :title="item.name" :arrow="false" :index="index" :key="index" @click="provinceChange">
  22. <u-icon v-show="isChooseP && province == index" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
  23. </u-cell-item>
  24. </u-cell-group>
  25. </scroll-view>
  26. </view>
  27. </view>
  28. <view class="area-item">
  29. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  30. <scroll-view :scroll-y="true" style="height: 100%">
  31. <u-cell-group v-if="isChooseP">
  32. <u-cell-item v-for="(item, index) in addressList[province].children" :title="item.name" :arrow="false" :index="index" :key="index" @click="cityChange">
  33. <u-icon v-show="isChooseC && city == index" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
  34. </u-cell-item>
  35. </u-cell-group>
  36. </scroll-view>
  37. </view>
  38. </view>
  39. <view class="area-item">
  40. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  41. <scroll-view :scroll-y="true" style="height: 100%">
  42. <u-cell-group v-if="isChooseC">
  43. <u-cell-item v-for="(item, index) in addressList[province].children[city].children" :title="item.name" :arrow="false" :index="index" :key="index" @click="areaChange">
  44. <u-icon v-show="isChooseA && area == index" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
  45. </u-cell-item>
  46. </u-cell-group>
  47. </scroll-view>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </u-popup>
  53. </template>
  54. <script>
  55. export default {
  56. name: 'city-picker',
  57. props: {
  58. // 通过双向绑定控制组件的弹出与收起
  59. value: {
  60. type: Boolean,
  61. default: false
  62. },
  63. // 默认显示的地区,可传类似["河北省", "秦皇岛市", "北戴河区"]
  64. defaultRegion: {
  65. type: Array,
  66. default() {
  67. return [];
  68. }
  69. },
  70. // 默认显示地区的编码,defaultRegion和areaCode同时存在,areaCode优先,可传类似["13", "1303", "130304"]
  71. areaCode: {
  72. type: Array,
  73. default() {
  74. return [];
  75. }
  76. },
  77. // 是否允许通过点击遮罩关闭Picker
  78. maskCloseAble: {
  79. type: Boolean,
  80. default: true
  81. },
  82. // 弹出的z-index值
  83. zIndex: {
  84. type: [String, Number],
  85. default: 0
  86. }
  87. },
  88. data() {
  89. return {
  90. // addressList:[],
  91. cityValue: '',
  92. isChooseP: false, //是否已经选择了省
  93. province: 0, //省级下标
  94. provinces: [],
  95. isChooseC: false, //是否已经选择了市
  96. city: 0, //市级下标
  97. citys: [],
  98. isChooseA: false, //是否已经选择了区
  99. area: 0, //区级下标
  100. areas: [],
  101. tabsIndex: 0
  102. };
  103. },
  104. mounted() {
  105. if(this.addressList.length<1){
  106. this.getRegionList();
  107. }else{
  108. this.init();
  109. }
  110. },
  111. computed: {
  112. isChange() {
  113. return this.tabsIndex > 1;
  114. },
  115. genTabsList() {
  116. let tabsList = [
  117. {
  118. name: '请选择'
  119. }
  120. ];
  121. if (this.isChooseP) {
  122. tabsList[0].name = this.addressList[this.province].name;
  123. tabsList[1] = {
  124. name: '请选择'
  125. };
  126. }
  127. if (this.isChooseC) {
  128. tabsList[1].name = this.addressList[this.province].children[this.city].name;
  129. tabsList[2] = {
  130. name: '请选择'
  131. };
  132. }
  133. if (this.isChooseA) {
  134. tabsList[2].name = this.addressList[this.province].children[this.city].children[this.area].name;
  135. }
  136. return tabsList;
  137. },
  138. uZIndex() {
  139. // 如果用户有传递z-index值,优先使用
  140. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  141. },
  142. // 地址列表
  143. addressList() {
  144. return this.$store.state.pub.addressList;
  145. },
  146. },
  147. methods: {
  148. getRegionList(){
  149. this.$u.api.getRegionListAjax().then(({code,data})=>{
  150. if(code==1){
  151. this.$store.commit('pub/commitAddressList',data)
  152. this.init();
  153. }
  154. })
  155. },
  156. init() {
  157. // 这里只会传code形式的,不传文本
  158. if(this.areaCode[0]){
  159. this.setProvince();
  160. }
  161. },
  162. setProvince() {
  163. // 查询省 索引
  164. const list = this.addressList;
  165. list.map((v, k) => {
  166. if (v.code == this.areaCode[0]) {
  167. this.provinceChange(k);
  168. if(this.areaCode[1]){
  169. // 查询城市索引
  170. this.setCity(k);
  171. }
  172. }
  173. });
  174. },
  175. setCity(pi,ci,ai) {
  176. // pi,ci,ai 省市区 索引
  177. const list = this.addressList[pi].children;
  178. list.map((v, k) => {
  179. if (v.code == this.areaCode[1]) {
  180. this.cityChange(k);
  181. if(this.areaCode[2]){
  182. // 查询地区索引
  183. this.setArea(pi,k);
  184. }
  185. }
  186. });
  187. },
  188. setArea(pi,ci,ai) {
  189. const list = this.addressList[pi].children[ci].children;
  190. list.map((v, k) => {
  191. if (v.code == this.areaCode[2]) {
  192. this.isChooseA = true;
  193. this.area = k;
  194. }
  195. });
  196. },
  197. close() {
  198. this.$emit('input', false);
  199. },
  200. tabsChange(index) {
  201. this.tabsIndex = index;
  202. },
  203. provinceChange(index) {
  204. this.isChooseP = true;
  205. this.isChooseC = false;
  206. this.isChooseA = false;
  207. this.province = index;
  208. this.citys = this.addressList[index].children;
  209. this.tabsIndex = 1;
  210. },
  211. cityChange(index) {
  212. this.isChooseC = true;
  213. this.isChooseA = false;
  214. this.city = index;
  215. if(this.citys[index].children[0]){
  216. this.areas = this.citys[index].children;
  217. this.tabsIndex = 2;
  218. return false;
  219. }
  220. let result = {};
  221. result.province = this.addressList[this.province];
  222. result.city = this.addressList[this.province].children[this.city];
  223. this.$emit('city-change', result);
  224. this.close();
  225. },
  226. areaChange(index) {
  227. this.isChooseA = true;
  228. this.area = index;
  229. let result = {};
  230. result.province = this.addressList[this.province];
  231. result.city = this.addressList[this.province].children[this.city];
  232. result.area = this.addressList[this.province].children[this.city].children[this.area];
  233. this.$emit('city-change', result);
  234. this.close();
  235. },
  236. }
  237. };
  238. </script>
  239. <style lang="scss" scoped>
  240. .area-box {
  241. width: 100%;
  242. overflow: hidden;
  243. height: 800rpx;
  244. > view {
  245. width: 150%;
  246. transition: transform 0.3s ease-in-out 0s;
  247. transform: translateX(0);
  248. &.change {
  249. transform: translateX(-33.3333333%);
  250. }
  251. }
  252. .area-item {
  253. width: 33.3333333%;
  254. height: 800rpx;
  255. }
  256. }
  257. </style>