u-city-select.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 active-color="#FF9447" v-if="value" :list="genTabsList" :is-scroll="true" :current="tabsIndex"
  5. @change="tabsChange" 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 provinces" :title="item.label" :arrow="false"
  13. :index="index" :key="index" @click="provinceChange(item,index)">
  14. <text v-if="isChooseP&&province==index" class="cuIcon-check text-bold"></text>
  15. </u-cell-item>
  16. </u-cell-group>
  17. </scroll-view>
  18. </view>
  19. </view>
  20. <view class="area-item">
  21. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  22. <scroll-view :scroll-y="true" style="height: 100%">
  23. <u-cell-group v-if="isChooseP">
  24. <u-cell-item v-for="(item,index) in citys" :title="item.label" :arrow="false"
  25. :index="index" :key="index" @click="cityChange(item,index)">
  26. <text v-if="isChooseC&&city==index" class="cuIcon-check text-bold"></text>
  27. </u-cell-item>
  28. </u-cell-group>
  29. </scroll-view>
  30. </view>
  31. </view>
  32. <view class="area-item">
  33. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  34. <scroll-view :scroll-y="true" style="height: 100%">
  35. <u-cell-group v-if="isChooseC">
  36. <u-cell-item v-for="(item,index) in areas" :title="item.label" :arrow="false"
  37. :index="index" :key="index" @click="areaChange(item,index)">
  38. <text v-if="isChooseA&&area==index" class="cuIcon-check text-bold"></text>
  39. </u-cell-item>
  40. </u-cell-group>
  41. </scroll-view>
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. </u-popup>
  47. </template>
  48. <script>
  49. /**
  50. * city-select 省市区级联选择器
  51. * @property {String Number} z-index 弹出时的z-index值(默认1075)
  52. * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true)
  53. * @property {String} default-region 默认选中的地区,中文形式
  54. * @property {String} default-code 默认选中的地区,编号形式
  55. */
  56. export default {
  57. name: 'u-city-select',
  58. props: {
  59. // 通过双向绑定控制组件的弹出与收起
  60. value: {
  61. type: Boolean,
  62. default: false
  63. },
  64. // 默认显示的地区,可传类似["河北省", "秦皇岛市", "北戴河区"]
  65. defaultRegion: {
  66. type: Array,
  67. default () {
  68. return [];
  69. }
  70. },
  71. // 默认显示地区的编码,defaultRegion和areaCode同时存在,areaCode优先,可传类似["13", "1303", "130304"]
  72. areaCode: {
  73. type: Array,
  74. default () {
  75. return [];
  76. }
  77. },
  78. // 是否允许通过点击遮罩关闭Picker
  79. maskCloseAble: {
  80. type: Boolean,
  81. default: true
  82. },
  83. // 弹出的z-index值
  84. zIndex: {
  85. type: [String, Number],
  86. default: 0
  87. }
  88. },
  89. data() {
  90. return {
  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. //接口city数据
  103. cityList: [],
  104. }
  105. },
  106. mounted() {
  107. this.init();
  108. },
  109. computed: {
  110. isChange() {
  111. return this.tabsIndex > 1;
  112. },
  113. genTabsList() {
  114. let tabsList = [{
  115. name: "请选择"
  116. }];
  117. if (this.isChooseP) {
  118. tabsList[0]['name'] = this.provinces[this.province]['label'];
  119. tabsList[1] = {
  120. name: "请选择"
  121. };
  122. }
  123. if (this.isChooseC) {
  124. tabsList[1]['name'] = this.citys[this.city]['label'];
  125. tabsList[2] = {
  126. name: "请选择"
  127. };
  128. }
  129. if (this.isChooseA) {
  130. tabsList[2]['name'] = this.areas[this.area]['label'];
  131. }
  132. return tabsList;
  133. },
  134. uZIndex() {
  135. // 如果用户有传递z-index值,优先使用
  136. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  137. }
  138. },
  139. methods: {
  140. init() {
  141. this.getProvince()
  142. if (this.areaCode.length == 3) {
  143. this.setProvince("", this.areaCode[0]);
  144. this.setCity("", this.areaCode[1]);
  145. this.setArea("", this.areaCode[2]);
  146. } else if (this.defaultRegion.length == 3) {
  147. this.setProvince(this.defaultRegion[0], "");
  148. this.setCity(this.defaultRegion[1], "");
  149. this.setArea(this.defaultRegion[2], "");
  150. };
  151. },
  152. async getProvince() {
  153. let provinces = (await this.$api.area.province()).data
  154. this.provinces = []
  155. provinces.forEach(item => {
  156. let obj = {
  157. hasChildren:item.hasChildren,
  158. label: item.title,
  159. value: item.value
  160. }
  161. this.provinces.push(obj)
  162. })
  163. this.getCity(this.provinces[0].value)
  164. },
  165. async getCity(code) {
  166. let params = {
  167. parentCode: code
  168. }
  169. let citys = (await this.$api.area.list(params)).data
  170. this.citys = []
  171. citys.forEach(item => {
  172. let obj = {
  173. hasChildren:item.hasChildren,
  174. label: item.title,
  175. value: item.value
  176. }
  177. this.citys.push(obj)
  178. })
  179. this.getArea(this.citys[0].value)
  180. },
  181. async getArea(code) {
  182. let params = {
  183. parentCode: code
  184. }
  185. let areas = (await this.$api.area.list(params)).data
  186. this.areas = []
  187. areas.forEach(item => {
  188. let obj = {
  189. hasChildren:item.hasChildren,
  190. label: item.title,
  191. value: item.value
  192. }
  193. this.areas.push(obj)
  194. })
  195. },
  196. setProvince(label = "", value = "") {
  197. this.provinces.map((v, k) => {
  198. if (value ? v.value == value : v.label == label) {
  199. this.provinceChange(v, k);
  200. }
  201. })
  202. },
  203. setCity(label = "", value = "") {
  204. this.citys.map((v, k) => {
  205. if (value ? v.value == value : v.label == label) {
  206. this.cityChange(v, k);
  207. }
  208. })
  209. },
  210. setArea(label = "", value = "") {
  211. this.areas.map((v, k) => {
  212. if (value ? v.value == value : v.label == label) {
  213. this.isChooseA = true;
  214. this.area = k;
  215. }
  216. })
  217. },
  218. close() {
  219. this.$emit('input', false);
  220. },
  221. tabsChange(index) {
  222. this.tabsIndex = index;
  223. },
  224. async provinceChange(item, index) {
  225. if (!item.hasChildren) {
  226. let result={
  227. location:item.label,
  228. locationCode:item.value+'0000'
  229. }
  230. this.$emit('city-change', result);
  231. this.close();
  232. return
  233. }
  234. this.isChooseP = true;
  235. this.isChooseC = false;
  236. this.isChooseA = false;
  237. this.province = index;
  238. this.getCity(item.value)
  239. this.tabsIndex = 1;
  240. },
  241. cityChange(item, index) {
  242. if (!item.hasChildren) {
  243. let result={
  244. location:item.label,
  245. locationCode:item.value+'00'
  246. }
  247. this.$emit('city-change', result);
  248. this.close();
  249. return
  250. }
  251. this.isChooseC = true;
  252. this.isChooseA = false;
  253. this.city = index;
  254. this.getArea(item.value)
  255. this.tabsIndex = 2;
  256. },
  257. areaChange(item, index) {
  258. this.isChooseA = true;
  259. this.area = index;
  260. let result = {};
  261. result.province = this.provinces[this.province];
  262. result.city = this.citys[this.city];
  263. result.area = this.areas[this.area];
  264. let data={
  265. location:result.province.label+'-'+result.city.label+'-'+result.area.label,
  266. locationCode:result.province.value+'0000,'+result.city.value+'00,'+result.area.value,
  267. }
  268. this.$emit('city-change', data);
  269. this.close();
  270. }
  271. }
  272. }
  273. </script>
  274. <style lang="scss">
  275. .area-box {
  276. width: 100%;
  277. overflow: hidden;
  278. height: 800rpx;
  279. >view {
  280. width: 150%;
  281. transition: transform 0.3s ease-in-out 0s;
  282. transform: translateX(0);
  283. &.change {
  284. transform: translateX(-33.3333333%);
  285. }
  286. }
  287. .area-item {
  288. width: 33.3333333%;
  289. height: 800rpx;
  290. }
  291. }
  292. </style>