city-picker.vue 6.9 KB

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