addressSelectLayer.vue
10.5 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<template>
<div class="address-select-layer"
v-if="isShow && cityList.list.length">
<div class="layer-box"
@click="hideLayer">
<div class="box"
@click="stopPropagation">
<div class="bar-box">
<div class="bar-box-center">
<div class="now-city">
<div class="now-item"
:class="{'selected': nowCity.province.id != 0, 'active': cityList.activityLevel == 1}"
@click="changeCity('1')">{{nowCity.province.nowName || nowCity.province.name}}</div>
<div class="now-item"
:class="{'selected': nowCity.city.id != 0, 'active': cityList.activityLevel == 2}"
@click="changeCity('2')">{{nowCity.city.nowName || nowCity.city.name}}</div>
<div class="now-item"
:class="{'selected': nowCity.area.id != 0, 'active': cityList.activityLevel == 3}"
@click="changeCity('3')">{{nowCity.area.nowName || nowCity.area.name}}</div>
</div>
<div class="buttons-box">
<div class="func-button"
:class="{'active': cityList.activityLevel == 4}"
:disabled="nowCity.province.id == 0 || nowCity.city.id == 0 || nowCity.area.id == 0"
@click="doSubmit">确定</div>
</div>
</div>
</div>
<scroll-div class="select-view-box"
scroll-y
scroll-with-animation>
<div class="item"
v-if="cityList && cityList.list && cityList.list.length"
v-for="(item, index) in cityList.list"
:key="index"
@click="selectCity(item)">
{{item.name}}
</div>
</scroll-div>
</div>
</div>
</div>
</template>
<script>
import Vue from "vue";
import { Toast } from 'vant';
Vue.use(Toast);
export default {
data: function () {
return {
isShow: false,
stop: false,
cityList: {
activityLevel: "0",
list: []
},
nowCity: {
province: {
id: "0",
pid: "0",
level: "1",
name: "省份",
nowName: ""
},
city: {
id: "0",
pid: "0",
level: "2",
name: "城市",
nowName: ""
},
area: {
id: "0",
pid: "0",
level: "3",
name: "区县",
nowName: ""
}
}
}
},
props: ["addressInfo"],
methods: {
/**
* 显示省市县层,初始化信息等
*/
showSelectLayer () {
// 获取目前设置的省市县名称
this.getCityName();
// 设置初始化列表参数
let id = this.nowCity.area.pid;
let level = this.nowCity.area.id && this.nowCity.area.id != "0" ? this.nowCity.area.level : "0";
// 获取地址列表项目
this.getCityList(id, level);
},
/**
* 隐藏省市县选择层
*/
hideLayer () {
// 判断是否阻止事件冒泡
if (!this.stop) {
this.isShow = false;
} else {
this.stop = false;
}
},
/**
* 阻止事件冒泡
*/
stopPropagation () {
this.stop = true;
},
/**
* 获取省市县列表
* @param {number} id [列表id]
* @param {number} level [活动项目级别,1 省 2 室 3 区县]
*/
getCityList (id = 0, level = 0) {
/* 获取省列表 开始 */
// 请求接口(获取省列表)
this.http("/Api/" + (this.getApiVersion().index) + "/getCityList", {
id: id
}, this.getCityListCallback, {
method: "POST",
level
})
/* 获取省列表 结束 */
},
/**
* 初始化省市县列表回调
* @param {object} res [服务器返回数据]
* @param {object} ext [扩展参数]
*/
getCityListCallback (res, ext) {
// 返回处理
if (res.code == 200) {
// 解构数据
let datas = res.data;
// 设置初始化省列表
this.$set(this.cityList, "activityLevel", parseInt(ext.level) + 1);
this.$set(this.cityList, "list", datas.list);
// 显示选择省市县浮层
this.isShow = true;
// 隐藏loading
Toast.clear();
// uni.hideLoading();
} else if (res.code == 400) {
// 获取列表失败
Toast(res.message);
} else {
// 获取列表失败
Toast("获取列表失败");
}
},
/**
* 获取目前设置的省市县名称
*/
getCityName () {
/* 获取目前设置的省市县名称信息 开始 */
// 判断是否设置了省市县地址
if (this.addressInfo.province_id && this.addressInfo.city_id && this.addressInfo.area_id) {
// 拆分省市县名称
let cityNameArr = this.addressInfo.city_name.split(" ");
// 判断数组长度,赋值给相关变量
if (cityNameArr.length === 3) {
// 赋值
this.$set(this.nowCity.province, "id", this.addressInfo.province_id);
this.$set(this.nowCity.province, "pid", "0");
this.$set(this.nowCity.province, "nowName", cityNameArr[0]);
this.$set(this.nowCity.city, "id", this.addressInfo.city_id);
this.$set(this.nowCity.city, "pid", this.addressInfo.province_id);
this.$set(this.nowCity.city, "nowName", cityNameArr[1]);
this.$set(this.nowCity.area, "id", this.addressInfo.area_id);
this.$set(this.nowCity.area, "pid", this.addressInfo.city_id);
this.$set(this.nowCity.area, "nowName", cityNameArr[2]);
}
}
/* 获取目前设置的省市县名称信息 结束 */
},
/**
* 设置用户修改后的省市县数据
*/
setCityName () {
/* 获取目前设置的省市县名称信息 开始 */
// 判断是否设置了省市县地址
if (this.nowCity.province.id && this.nowCity.city.id && this.nowCity.area.id) {
// 拆分省市县名称
let cityNameString = this.nowCity.province.nowName + " " + this.nowCity.city.nowName + " " + this.nowCity.area.nowName;
// 判断数组长度,赋值给相关变量
if (cityNameString) {
// 赋值
this.$set(this.addressInfo, "province_id", this.nowCity.province.id);
this.$set(this.addressInfo, "city_id", this.nowCity.city.id);
this.$set(this.addressInfo, "area_id", this.nowCity.area.id);
this.$set(this.addressInfo, "city_name", cityNameString);
}
}
/* 获取目前设置的省市县名称信息 结束 */
},
/**
* 设置nowCity的值,该值在确定后会被赋值给addressInfo的相关属性
* @param {object} nowCityItem [nowCity相关对象值]
*/
setNowCity (nowCityItem) {
// 判断项目,设置nowCity值
switch (nowCityItem.level) {
case "1":
// 设置新数据
this.$set(this.nowCity.province, "id", nowCityItem.id);
this.$set(this.nowCity.province, "pid", nowCityItem.pid);
this.$set(this.nowCity.province, "nowName", nowCityItem.name);
// 清除下级数据
this.$set(this.nowCity.city, "id", "0");
this.$set(this.nowCity.city, "pid", "0");
this.$set(this.nowCity.city, "nowName", "");
this.$set(this.nowCity.area, "id", "0");
this.$set(this.nowCity.area, "pid", "0");
this.$set(this.nowCity.area, "nowName", "");
break;
case "2":
// 设置新数据
this.$set(this.nowCity.city, "id", nowCityItem.id);
this.$set(this.nowCity.city, "pid", nowCityItem.pid);
this.$set(this.nowCity.city, "nowName", nowCityItem.name);
// 清除下级数据
this.$set(this.nowCity.area, "id", "0");
this.$set(this.nowCity.area, "pid", "0");
this.$set(this.nowCity.area, "nowName", "");
break;
case "3":
this.$set(this.nowCity.area, "id", nowCityItem.id);
this.$set(this.nowCity.area, "pid", nowCityItem.pid);
this.$set(this.nowCity.area, "nowName", nowCityItem.name);
break;
}
},
/**
* 选择地址列表项目
* @param {object} item [地址项对象]
*/
selectCity (item) {
// 设置nowCity值
this.setNowCity(item);
if (item.level < 3) {
// 显示loading
Toast.loading({
duration: 0, // 持续展示 toast
message: '加载中...',
forbidClick: true,
loadingType: 'spinner',
});
// uni.showLoading({
// title: "",
// mask: true
// })
// 获取省市县列表
this.getCityList(item.id, item.level);
} else {
// 设置activityLevel=4,可以确定了
this.$set(this.cityList, "activityLevel", parseInt(item.level) + 1);
}
},
/**
* 选择地址列表项目
* @param {object} nowCity [需要切换的数据对象]
*/
changeCity (level) {
// 如果是空的,点击无效
switch (level) {
case "1":
if (this.nowCity.province.id == "0") {
return false;
}
break;
case "2":
if (this.nowCity.city.id == "0") {
return false;
}
break;
case "3":
if (this.nowCity.area.id == "0") {
return false;
}
break;
}
// 需要读取的列表id
let id = "0";
// 根据级别,获取参数,做相应设置
switch (level) {
case "1":
id = this.nowCity.province.pid;
break;
case "2":
id = this.nowCity.city.pid;
break;
case "3":
id = this.nowCity.area.pid;
break;
}
// 显示loading
Toast.loading({
duration: 0, // 持续展示 toast
message: '加载中...',
forbidClick: true,
loadingType: 'spinner',
});
// uni.showLoading({
// title: "",
// mask: true
// })
// 获取省市县列表
this.getCityList(id, parseInt(level) - 1);
},
/**
* 点击确定,修改地址的相关地址设置参数
*/
doSubmit () {
// 设置用户修改后的省市县数据
this.setCityName();
// 隐藏选择省市县浮层
this.isShow = false;
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style rel="stylesheet/less" lang="less">
@import './addressSelectLayer';
</style>