CompSelectAddress.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <view class="SelectAddress">
  3. <picker
  4. mode="multiSelector"
  5. :value="addressSelectListData"
  6. @columnchange="addressColumnchange"
  7. @change="addressChange"
  8. :range="addressListData"
  9. >
  10. <!-- <view class="noneAddress" v-if="!addressListStr">
  11. <uni-easyinput
  12. trim="all"
  13. placeholder="请选择您的地区"
  14. disabled
  15. ></uni-easyinput>
  16. </view> -->
  17. <view class="haveAddress">
  18. <uni-easyinput
  19. trim="all"
  20. placeholder="请选择您的地区"
  21. disabled
  22. :value="addressListStr"
  23. ></uni-easyinput>
  24. </view>
  25. </picker>
  26. </view>
  27. </template>
  28. <script>
  29. import chinaAddress from "./citydata";
  30. export default {
  31. name: "CompSelectAddress",
  32. data() {
  33. return {
  34. chinaAddress: [], // 全国省市区
  35. addressListData: [], // 全国省市区
  36. addressSelectListData: [0, 0, 0], // 当前选择的省市区 index
  37. addressListStr: "", // 当前选择的省市区
  38. };
  39. },
  40. props: {
  41. value: {
  42. type: Array,
  43. default: [],
  44. },
  45. },
  46. mounted() {
  47. this.chinaAddress = chinaAddress;
  48. // 获取全国省份
  49. this.getProvince();
  50. // 回显地址
  51. if (this.value) {
  52. const showAddressList = this.value;
  53. this.addressListStr = showAddressList.join("-");
  54. }
  55. },
  56. methods: {
  57. // 获取全国省份list
  58. getProvince() {
  59. const chinaAddress = this.chinaAddress;
  60. const provinceListData = [];
  61. for (const province_key in chinaAddress) {
  62. provinceListData.push(chinaAddress[province_key].label);
  63. }
  64. this.addressListData[0] = provinceListData;
  65. // 默认加载第一个省份的城市
  66. this.getCity(provinceListData[0]);
  67. },
  68. // 根据省份获取城市
  69. getCity(province) {
  70. // 获取省份对象
  71. const chinaAddress = this.chinaAddress;
  72. const chinaAddress_Item = chinaAddress.find((item) => {
  73. return item.label === province;
  74. });
  75. // 获取城市list
  76. const cityChildren = chinaAddress_Item.children;
  77. if (!cityChildren) {
  78. // 如果省份没有市区 清空上次的市区和区域数据
  79. this.addressListData[1] = "";
  80. this.addressListData[2] = "";
  81. return;
  82. }
  83. // 解析城市list
  84. const cityListData = [];
  85. for (const city_key in cityChildren) {
  86. cityListData.push(cityChildren[city_key].label);
  87. }
  88. this.addressListData[1] = cityListData;
  89. // 默认加载第一个省份的第一个城市的区域
  90. this.getCounty(province, cityListData[0]);
  91. },
  92. // 根据省份和城市 获取区域
  93. getCounty(province, city) {
  94. // 获取省份对象
  95. const chinaAddress = this.chinaAddress;
  96. const chinaAddress_Item = chinaAddress.find((item) => {
  97. return item.label === province;
  98. });
  99. // 获取城市list
  100. const cityChildren = chinaAddress_Item.children;
  101. // 根据城市获取区域
  102. const cityChildren_item = cityChildren.find((item) => {
  103. return item.label === city;
  104. });
  105. // 解析区域list
  106. const countyListData = [];
  107. if (cityChildren_item.children) {
  108. for (const county_key in cityChildren_item.children) {
  109. countyListData.push(cityChildren_item.children[county_key].label);
  110. }
  111. } else {
  112. countyListData.push(cityChildren_item.label);
  113. }
  114. this.addressListData[2] = countyListData;
  115. },
  116. // 区域更新
  117. addressColumnchange(e) {
  118. const { column, value } = e.detail;
  119. this.addressSelectListData[column] = value;
  120. // 当前选择的节点地址
  121. const selectItem = this.addressListData[column][value];
  122. if (column === 0) {
  123. // 根据省份获取城市
  124. this.getCity(selectItem);
  125. } else if (column === 1) {
  126. // 获取当前城市的省份
  127. const province = this.addressListData[0][this.addressSelectListData[0]];
  128. // 根据省份和城市获取区域
  129. if (selectItem) {
  130. this.getCounty(province, selectItem);
  131. }
  132. }
  133. this.$forceUpdate();
  134. },
  135. // 区域确定
  136. addressChange(e) {
  137. const { value } = e.detail;
  138. // 获取省、市、区
  139. let province = this.addressListData[0][value[0]] || "";
  140. let city = this.addressListData[1][value[1]] || "";
  141. let county = this.addressListData[2][value[2]] || "";
  142. // 拼接页面显示的地址链接
  143. const addressListStr = [];
  144. if (province) {
  145. addressListStr.push(province);
  146. }
  147. if (city) {
  148. addressListStr.push(city);
  149. }
  150. if (county) {
  151. addressListStr.push(county);
  152. }
  153. this.addressListStr = addressListStr.join("-");
  154. // 传值给父页面
  155. const addressList = [province, city, county];
  156. this.$emit("selectAddress", addressList);
  157. },
  158. },
  159. };
  160. </script>
  161. <style>
  162. .noneAddress {
  163. font-size: 28rpx;
  164. font-family: PingFangSC-Regular, PingFang SC;
  165. font-weight: 400;
  166. color: #cdcdcd;
  167. }
  168. .haveAddress {
  169. font-size: 28rpx;
  170. font-family: PingFangSC-Regular, PingFang SC;
  171. font-weight: 400;
  172. color: #333333;
  173. }
  174. </style>