city-picker - 副本.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 provinces" :title="item.label" :arrow="false" :index="index" :key="index" @click="provinceChange">
  22. <u-icon v-if="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 citys" :title="item.label" :arrow="false" :index="index" :key="index" @click="cityChange">
  33. <u-icon v-if="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 areas" :title="item.label" :arrow="false" :index="index" :key="index" @click="areaChange">
  44. <u-icon v-if="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. import provinces from 'uview-ui/libs/util/province.js';
  56. import citys from 'uview-ui/libs/util/city.js';
  57. import areas from 'uview-ui/libs/util/area.js';
  58. export default {
  59. name: 'city-picker',
  60. props: {
  61. // 通过双向绑定控制组件的弹出与收起
  62. value: {
  63. type: Boolean,
  64. default: false
  65. },
  66. // 默认显示的地区,可传类似["河北省", "秦皇岛市", "北戴河区"]
  67. defaultRegion: {
  68. type: Array,
  69. default() {
  70. return [];
  71. }
  72. },
  73. // 默认显示地区的编码,defaultRegion和areaCode同时存在,areaCode优先,可传类似["13", "1303", "130304"]
  74. areaCode: {
  75. type: Array,
  76. default() {
  77. return [];
  78. }
  79. },
  80. // 是否允许通过点击遮罩关闭Picker
  81. maskCloseAble: {
  82. type: Boolean,
  83. default: true
  84. },
  85. // 弹出的z-index值
  86. zIndex: {
  87. type: [String, Number],
  88. default: 0
  89. }
  90. },
  91. data() {
  92. return {
  93. regionList:[],
  94. cityValue: '',
  95. isChooseP: false, //是否已经选择了省
  96. province: 0, //省级下标
  97. provinces: provinces,
  98. isChooseC: false, //是否已经选择了市
  99. city: 0, //市级下标
  100. citys: citys[0],
  101. isChooseA: false, //是否已经选择了区
  102. area: 0, //区级下标
  103. areas: areas[0][0],
  104. tabsIndex: 0
  105. };
  106. },
  107. mounted() {
  108. this.getRegionList();
  109. this.init();
  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.provinces[this.province]['label'];
  123. tabsList[1] = {
  124. name: '请选择'
  125. };
  126. }
  127. if (this.isChooseC) {
  128. tabsList[1]['name'] = this.citys[this.city]['label'];
  129. tabsList[2] = {
  130. name: '请选择'
  131. };
  132. }
  133. if (this.isChooseA) {
  134. tabsList[2]['name'] = this.areas[this.area]['label'];
  135. }
  136. return tabsList;
  137. },
  138. uZIndex() {
  139. // 如果用户有传递z-index值,优先使用
  140. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  141. }
  142. },
  143. methods: {
  144. getRegionList(){
  145. this.$u.api.getRegionListAjax().then(({code,data})=>{
  146. if(code==1){
  147. this.regionList = true;
  148. }
  149. })
  150. },
  151. init() {
  152. if (this.areaCode.length == 3) {
  153. this.setProvince('', this.areaCode[0]);
  154. this.setCity('', this.areaCode[1]);
  155. this.setArea('', this.areaCode[2]);
  156. } else if (this.defaultRegion.length == 3) {
  157. this.setProvince(this.defaultRegion[0], '');
  158. this.setCity(this.defaultRegion[1], '');
  159. this.setArea(this.defaultRegion[2], '');
  160. }
  161. },
  162. setProvince(label = '', value = '') {
  163. this.provinces.map((v, k) => {
  164. if (value ? v.value == value : v.label == label) {
  165. this.provinceChange(k);
  166. }
  167. });
  168. },
  169. setCity(label = '', value = '') {
  170. this.citys.map((v, k) => {
  171. if (value ? v.value == value : v.label == label) {
  172. this.cityChange(k);
  173. }
  174. });
  175. },
  176. setArea(label = '', value = '') {
  177. this.areas.map((v, k) => {
  178. if (value ? v.value == value : v.label == label) {
  179. this.isChooseA = true;
  180. this.area = k;
  181. }
  182. });
  183. },
  184. close() {
  185. this.$emit('input', false);
  186. },
  187. tabsChange(index) {
  188. this.tabsIndex = index;
  189. },
  190. provinceChange(index) {
  191. this.isChooseP = true;
  192. this.isChooseC = false;
  193. this.isChooseA = false;
  194. this.province = index;
  195. this.citys = citys[index];
  196. this.tabsIndex = 1;
  197. },
  198. cityChange(index) {
  199. this.isChooseC = true;
  200. this.isChooseA = false;
  201. this.city = index;
  202. this.areas = areas[this.province][index];
  203. this.tabsIndex = 2;
  204. },
  205. areaChange(index) {
  206. this.isChooseA = true;
  207. this.area = index;
  208. let result = {};
  209. result.province = this.provinces[this.province];
  210. result.city = this.citys[this.city];
  211. result.area = this.areas[this.area];
  212. this.$emit('city-change', result);
  213. this.close();
  214. },
  215. }
  216. };
  217. </script>
  218. <style lang="scss" scoped>
  219. .area-box {
  220. width: 100%;
  221. overflow: hidden;
  222. height: 800rpx;
  223. > view {
  224. width: 150%;
  225. transition: transform 0.3s ease-in-out 0s;
  226. transform: translateX(0);
  227. &.change {
  228. transform: translateX(-33.3333333%);
  229. }
  230. }
  231. .area-item {
  232. width: 33.3333333%;
  233. height: 800rpx;
  234. }
  235. }
  236. </style>