index.vue
3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<template>
<el-cascader
class="el_cascader"
:options="areaList"
v-model="areas"
:props="cascaderProps"
placeholder="请选择"
:key="areasKeys"
@change="onChange"
@expand-change="onChange"
/>
</template>
<script>
import { getAddress } from "@/api/system";
export default {
name: "AreaCascader",
props: {
value: {
type: Array,
default: () => [],
},
},
data() {
return {
areaList: [],
areas: [],
cascaderProps: {
label: "areaName",
value: "id",
children: "children",
// lazy: true,
// lazyLoad: this.loadProvince,
},
areasKeys: "",
};
},
watch: {
value: {
handler: function (n, o) {
if (n && n != o) {
this.areas = n;
this.fillProvince(n);
}
},
immediate: true,
},
},
created() {
this.getList(0);
},
methods: {
// 获取省市区
getList(id) {
let params = {
id: id,
type: "children",
};
let _this = this;
return new Promise((resolve) => {
getAddress(params).then((response) => {
let arr = response.data || [];
arr.map((k) => {
k.children = [];
return k;
});
if (id == 0) {
_this.areaList = arr;
}
resolve(arr);
});
});
},
// 填充省市区
fillProvince(codes) {
let _this = this;
this.getList(0).then((res) => {
_this.getCity(codes);
});
},
// 根据省份code 获取城市列表
getCity(codes) {
let _this = this;
let list = this.areaList || [];
let params = {};
params.id = codes[0];
params.type = "children";
getAddress(params).then((response) => {
let arr = response.data || [];
let idx = list.findIndex((k) => k.areaCode == codes[0]);
let province = list[idx];
arr.map((k) => {
k.children = [];
return k;
});
province.children = arr;
_this.$set(_this.areaList, idx, province);
if (codes.length > 2) {
_this.getArea(codes);
}
});
},
// 根据城市code 获取地区列表
getArea(codes) {
let _this = this;
let list = this.areaList || [];
let params = {};
params.id = codes[1];
params.type = "children";
getAddress(params).then((response) => {
let arr = response.data || [];
let province = list.find((k) => k.areaCode == codes[0]);
let idx = (province.children || []).findIndex(
(k) => k.areaCode == codes[1]
);
let city = province.children[idx];
arr.map((k) => {
delete k.children;
return k;
});
city.children = arr;
_this.$set(_this.areaList, idx, province);
// _this.areas = codes;
if (codes.length == 3) {
_this.areasKeys = Math.random();
}
});
},
// 监听编号
onChange(node) {
console.log(";555555555");
console.log(node);
// 切换省份
if (node.length == 1) {
this.getCity(node);
}
// 切换城市
if (node.length == 2) {
this.getArea(node);
}
},
},
};
</script>