index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <view class="ssq adffc">
  3. <view class="box">
  4. <view class="box-top adfac">
  5. <view class="box-top-btn qx" @click="handleCancel">取消</view>
  6. <view class="box-top-title">{{ title }}</view>
  7. <view class="box-top-btn qd" @click="handleConfirm">确定</view>
  8. </view>
  9. <picker-view :value="pickerValue" @change="handlePickerChange" class="picker-view">
  10. <!-- 省份列 -->
  11. <picker-view-column>
  12. <view class="picker-item" :class="{ 'selected-item': pickerValue[0] === index }" v-for="(province, index) in provinces" :key="province.id">
  13. {{ province.name }}
  14. </view>
  15. </picker-view-column>
  16. <!-- 城市列 -->
  17. <picker-view-column>
  18. <view class="picker-item" :class="{ 'selected-item': pickerValue[1] === index }" v-for="(city, index) in cities" :key="city.id">
  19. {{ city.name }}
  20. </view>
  21. </picker-view-column>
  22. <!-- 区县列 -->
  23. <picker-view-column>
  24. <view class="picker-item" :class="{ 'selected-item': pickerValue[2] === index }" v-for="(area, index) in areas" :key="area.id">
  25. {{ area.name }}
  26. </view>
  27. </picker-view-column>
  28. </picker-view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. props: {
  35. title: {
  36. typeof: String,
  37. default: '所属地区'
  38. }
  39. },
  40. data() {
  41. return {
  42. provinces: [],
  43. cities: [],
  44. areas: [],
  45. pickerValue: [0, 0, 0]
  46. };
  47. },
  48. computed: {
  49. selectedProvince: function () {
  50. return this.provinces[this.pickerValue[0]] || {};
  51. },
  52. selectedCity: function () {
  53. return this.cities[this.pickerValue[1]] || {};
  54. },
  55. selectedArea: function () {
  56. return this.areas[this.pickerValue[2]] || {};
  57. }
  58. },
  59. mounted() {
  60. this.getProvinceCityAreaData();
  61. },
  62. methods: {
  63. getProvinceCityAreaData() {
  64. this.$api.get('/sys/region/tree').then(({ data: res }) => {
  65. if (res.code !== 0) return this.$showToast(res.msg);
  66. this.provinces = this.arrayToTreeOptimized(res.data);
  67. this.initialize();
  68. });
  69. },
  70. //方法一:递归
  71. arrayToTree(list, rootPid = '0') {
  72. const tree = [];
  73. // 1. 首先找到所有根节点
  74. list.forEach((item) => {
  75. if (item.pid === rootPid) {
  76. // 2. 找到根节点后,递归寻找它的子节点
  77. const children = this.arrayToTree(list, item.id);
  78. if (children.length > 0) {
  79. item.children = children;
  80. }
  81. tree.push(item);
  82. }
  83. });
  84. return tree;
  85. },
  86. //方法二:Map映射,性能提升
  87. arrayToTreeOptimized(list, rootPid = '0') {
  88. const map = new Map();
  89. const tree = [];
  90. list.forEach((item) => {
  91. map.set(item.id, { ...item, children: [] });
  92. });
  93. // 2. 再次遍历列表,建立父子关系
  94. map.forEach((node) => {
  95. const parent = map.get(node.pid);
  96. if (parent) {
  97. // 如果找到父节点,则将当前节点添加到父节点的 children 数组中
  98. parent.children.push(node);
  99. } else if (node.pid === rootPid) {
  100. // 如果没有父节点,并且 pid 是根 pid,则为根节点
  101. tree.push(node);
  102. }
  103. });
  104. // 清理空 children 数组(可选步骤,如果不需要空数组)
  105. map.forEach((node) => {
  106. if (node.children.length === 0) {
  107. delete node.children;
  108. }
  109. });
  110. return tree;
  111. },
  112. handlePickerChange(e) {
  113. const newPickerValue = e.detail.value;
  114. const [provinceIndex, cityIndex, areaIndex] = newPickerValue;
  115. const oldProvinceIndex = this.pickerValue[0];
  116. const oldCityIndex = this.pickerValue[1];
  117. if (provinceIndex !== oldProvinceIndex) {
  118. this.cities = this.provinces[provinceIndex].children;
  119. this.areas = this.cities[0]?.children || [];
  120. this.pickerValue = [provinceIndex, 0, 0];
  121. } else if (cityIndex !== oldCityIndex) {
  122. // 更新地区列表,并将地区索引重置为0
  123. this.areas = this.cities[cityIndex]?.children || [];
  124. this.pickerValue = [provinceIndex, cityIndex, 0];
  125. } else {
  126. this.pickerValue = newPickerValue;
  127. }
  128. },
  129. initialize() {
  130. this.cities = this.provinces[0]?.children || [];
  131. this.areas = this.cities[0]?.children || [];
  132. },
  133. handleCancel() {
  134. this.$emit('cancel');
  135. },
  136. handleConfirm() {
  137. this.$emit('confirm', {
  138. provinceId:this.provinces[this.pickerValue[0]].id,
  139. cityId:this.cities[this.pickerValue[1]].id,
  140. districtId:this.areas[this.pickerValue[2]].id,
  141. provinceName:this.provinces[this.pickerValue[0]].name,
  142. cityName:this.cities[this.pickerValue[1]].name,
  143. districtName:this.areas[this.pickerValue[2]].name,
  144. });
  145. }
  146. }
  147. };
  148. </script>
  149. <style scoped lang="scss">
  150. .ssq {
  151. position: fixed;
  152. left: 0;
  153. right: 0;
  154. top: 0;
  155. bottom: 0;
  156. background: rgba(0, 0, 0, 0.5);
  157. justify-content: flex-end;
  158. z-index: 1001;
  159. .box {
  160. background: #ffffff;
  161. border-radius: 24rpx 24rpx 0 0;
  162. &-top {
  163. width: 100%;
  164. height: 84rpx;
  165. &-btn {
  166. width: 120rpx;
  167. font-size: 30rpx;
  168. text-align: center;
  169. line-height: 42rpx;
  170. &.qx {
  171. color: #909193;
  172. }
  173. &.qd {
  174. color: #3c9cff;
  175. }
  176. }
  177. &-title {
  178. flex: 1;
  179. font-size: 32rpx;
  180. color: #303133;
  181. text-align: center;
  182. line-height: 42rpx;
  183. }
  184. }
  185. }
  186. }
  187. .picker-view {
  188. width: 100%;
  189. height: 380rpx;
  190. margin: 40rpx 0 80rpx;
  191. }
  192. .picker-item {
  193. display: flex;
  194. align-items: center;
  195. justify-content: center;
  196. font-size: 36rpx;
  197. color: #303133;
  198. transition: all 0.2s;
  199. &.selected-item {
  200. font-size: 36rpx;
  201. font-weight: bold;
  202. color: #303133;
  203. }
  204. }
  205. </style>