u-city-select.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <view class="" :style="vuex_skin">
  3. <u-popup v-model="value" mode="bottom" :popup="false" :mask="true" :closeable="true" :safe-area-inset-bottom="true"
  4. close-icon-color="#ffffff" :z-index="uZIndex" :maskCloseAble="maskCloseAble" @close="close">
  5. <u-tabs :active-color="vuex_theme.bgColor" v-if="value" :list="genTabsList" :is-scroll="true" :current="tabsIndex"
  6. @change="tabsChange" ref="tabs"></u-tabs>
  7. <view class="area-box">
  8. <view class="u-flex" :class="{ 'change':isChange }">
  9. <view class="area-item">
  10. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  11. <scroll-view :scroll-y="true" style="height: 100%">
  12. <u-cell-group>
  13. <u-cell-item v-for="(item,index) in provinces" :title="item.label" :arrow="false"
  14. :index="index" :key="index" @click="provinceChange(item,index)">
  15. <text v-if="isChooseP&&province==index" class="cuIcon-check text-bold text-base"></text>
  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 citys" :title="item.label" :arrow="false"
  26. :index="index" :key="index" @click="cityChange(item,index)">
  27. <text v-if="isChooseC&&city==index" class="cuIcon-check text-bold text-base"></text>
  28. </u-cell-item>
  29. </u-cell-group>
  30. </scroll-view>
  31. </view>
  32. </view>
  33. <view class="area-item">
  34. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  35. <scroll-view :scroll-y="true" style="height: 100%">
  36. <u-cell-group v-if="isChooseC">
  37. <u-cell-item v-for="(item,index) in areas" :title="item.label" :arrow="false"
  38. :index="index" :key="index" @click="areaChange(item,index)">
  39. <text v-if="isChooseA&&area==index" class="cuIcon-check text-bold text-base"></text>
  40. </u-cell-item>
  41. </u-cell-group>
  42. </scroll-view>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </u-popup>
  48. </view>
  49. </template>
  50. <script>
  51. /**
  52. * city-select 省市区级联选择器
  53. * @property {String Number} z-index 弹出时的z-index值(默认1075)
  54. * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true)
  55. * @property {String} default-region 默认选中的地区,中文形式
  56. * @property {String} default-code 默认选中的地区,编号形式
  57. */
  58. export default {
  59. name: 'u-city-select',
  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. cityValue: "",
  94. isChooseP: false, //是否已经选择了省
  95. province: 0, //省级下标
  96. provinces: [],
  97. isChooseC: false, //是否已经选择了市
  98. city: 0, //市级下标
  99. citys: [],
  100. isChooseA: false, //是否已经选择了区
  101. area: 0, //区级下标
  102. areas: [],
  103. tabsIndex: 0,
  104. //接口city数据
  105. cityList: [],
  106. }
  107. },
  108. mounted() {
  109. this.init();
  110. },
  111. computed: {
  112. isChange() {
  113. return this.tabsIndex > 1;
  114. },
  115. genTabsList() {
  116. let tabsList = [{
  117. name: "请选择"
  118. }];
  119. if (this.isChooseP) {
  120. tabsList[0]['name'] = this.provinces[this.province]['label'];
  121. tabsList[1] = {
  122. name: "请选择"
  123. };
  124. }
  125. if (this.isChooseC) {
  126. tabsList[1]['name'] = this.citys[this.city]['label'];
  127. tabsList[2] = {
  128. name: "请选择"
  129. };
  130. }
  131. if (this.isChooseA) {
  132. tabsList[2]['name'] = this.areas[this.area]['label'];
  133. }
  134. return tabsList;
  135. },
  136. uZIndex() {
  137. // 如果用户有传递z-index值,优先使用
  138. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  139. }
  140. },
  141. methods: {
  142. init() {
  143. this.getProvince()
  144. if (this.areaCode.length == 3) {
  145. this.setProvince("", this.areaCode[0]);
  146. this.setCity("", this.areaCode[1]);
  147. this.setArea("", this.areaCode[2]);
  148. } else if (this.defaultRegion.length == 3) {
  149. this.setProvince(this.defaultRegion[0], "");
  150. this.setCity(this.defaultRegion[1], "");
  151. this.setArea(this.defaultRegion[2], "");
  152. };
  153. },
  154. async getProvince() {
  155. let provinces = (await this.$api.area.province()).data.data
  156. this.provinces = []
  157. provinces.forEach(item => {
  158. let obj = {
  159. hasChildren:item.hasChildren,
  160. label: item.title,
  161. value: item.value
  162. }
  163. this.provinces.push(obj)
  164. })
  165. this.getCity(this.provinces[0].value)
  166. },
  167. async getCity(code) {
  168. let params = {
  169. parentCode: code
  170. }
  171. let citys = (await this.$api.area.list(params)).data.data
  172. this.citys = []
  173. citys.forEach(item => {
  174. let obj = {
  175. hasChildren:item.hasChildren,
  176. label: item.title,
  177. value: item.value
  178. }
  179. this.citys.push(obj)
  180. })
  181. this.getArea(this.citys[0].value)
  182. },
  183. async getArea(code) {
  184. let params = {
  185. parentCode: code
  186. }
  187. let areas = (await this.$api.area.list(params)).data.data
  188. this.areas = []
  189. areas.forEach(item => {
  190. let obj = {
  191. hasChildren:item.hasChildren,
  192. label: item.title,
  193. value: item.value
  194. }
  195. this.areas.push(obj)
  196. })
  197. },
  198. setProvince(label = "", value = "") {
  199. console.log(label);
  200. this.provinces.map((v, k) => {
  201. if (value ? v.value == value : v.label == label) {
  202. this.provinceChange(v, k);
  203. }
  204. })
  205. },
  206. setCity(label = "", value = "") {
  207. this.citys.map((v, k) => {
  208. if (value ? v.value == value : v.label == label) {
  209. this.cityChange(v, k);
  210. }
  211. })
  212. },
  213. setArea(label = "", value = "") {
  214. this.areas.map((v, k) => {
  215. if (value ? v.value == value : v.label == label) {
  216. this.isChooseA = true;
  217. this.area = k;
  218. }
  219. })
  220. },
  221. close() {
  222. this.$emit('input', false);
  223. },
  224. tabsChange(index) {
  225. this.tabsIndex = index;
  226. },
  227. async provinceChange(item, index) {
  228. if (!item.hasChildren) {
  229. let result={
  230. location:item.label,
  231. locationCode:item.value+'0000'
  232. }
  233. this.$emit('city-change', result);
  234. this.close();
  235. return
  236. }
  237. this.isChooseP = true;
  238. this.isChooseC = false;
  239. this.isChooseA = false;
  240. this.province = index;
  241. this.getCity(item.value)
  242. this.tabsIndex = 1;
  243. },
  244. cityChange(item, index) {
  245. if (!item.hasChildren) {
  246. let result={
  247. location:item.label,
  248. locationCode:item.value+'00'
  249. }
  250. this.$emit('city-change', result);
  251. this.close();
  252. return
  253. }
  254. this.isChooseC = true;
  255. this.isChooseA = false;
  256. this.city = index;
  257. this.getArea(item.value)
  258. this.tabsIndex = 2;
  259. },
  260. areaChange(item, index) {
  261. this.isChooseA = true;
  262. this.area = index;
  263. let result = {};
  264. result.province = this.provinces[this.province];
  265. result.city = this.citys[this.city];
  266. result.area = this.areas[this.area];
  267. let data={
  268. location:result.province.label+'-'+result.city.label+'-'+result.area.label,
  269. locationCode:result.province.value+'0000,'+result.city.value+'00,'+result.area.value,
  270. }
  271. console.log(data);
  272. this.$emit('city-change', data);
  273. this.close();
  274. }
  275. }
  276. }
  277. </script>
  278. <style lang="scss">
  279. .area-box {
  280. width: 100%;
  281. overflow: hidden;
  282. height: 800rpx;
  283. >view {
  284. width: 150%;
  285. transition: transform 0.3s ease-in-out 0s;
  286. transform: translateX(0);
  287. &.change {
  288. transform: translateX(-33.3333333%);
  289. }
  290. }
  291. .area-item {
  292. width: 33.3333333%;
  293. height: 800rpx;
  294. }
  295. }
  296. </style>