|
1 | 1 | <template> |
2 | | - <u-popup v-model="value" mode="bottom" :popup="false" :mask="true" :closeable="true" :safe-area-inset-bottom="true" |
3 | | - close-icon-color="#ffffff" :z-index="uZIndex" :maskCloseAble="maskCloseAble" @close="close"> |
4 | | - <u-tabs v-if="value" :list="genTabsList" :is-scroll="true" :current="tabsIndex" @change="tabsChange" ref="tabs"></u-tabs> |
| 2 | + <u-popup :show="value" mode="bottom" round="32rpx" :closeable="true" |
| 3 | + :z-index="uZIndex" :closeOnClickOverlay="maskCloseAble" @close="close"> |
| 4 | + <u-tabs v-if="value" :list="genTabsList" :current="tabsIndex" @change="tabsChange"></u-tabs> |
5 | 5 | <view class="area-box"> |
6 | 6 | <view class="u-flex" :class="{ 'change':isChange }"> |
7 | 7 | <view class="area-item"> |
8 | 8 | <view class="u-padding-10 u-bg-gray" style="height: 100%;"> |
9 | 9 | <scroll-view :scroll-y="true" style="height: 100%"> |
10 | 10 | <u-cell-group> |
11 | | - <u-cell-item v-for="(item,index) in provinces" :title="item.label" :arrow="false" :index="index" :key="index" |
| 11 | + <u-cell v-for="(item,index) in provinces" :key="index" :title="item.label" :name="index" |
12 | 12 | @click="provinceChange"> |
13 | 13 | <u-icon v-if="isChooseP&&province===index" slot="right-icon" size="34" name="checkbox-mark"></u-icon> |
14 | | - </u-cell-item> |
| 14 | + </u-cell> |
15 | 15 | </u-cell-group> |
16 | 16 | </scroll-view> |
17 | 17 | </view> |
18 | 18 | </view> |
19 | | - <view class="area-item"> |
| 19 | + <view v-if="isChooseP" class="area-item"> |
20 | 20 | <view class="u-padding-10 u-bg-gray" style="height: 100%;"> |
21 | 21 | <scroll-view :scroll-y="true" style="height: 100%"> |
22 | | - <u-cell-group v-if="isChooseP"> |
23 | | - <u-cell-item v-for="(item,index) in citys" :title="item.label" :arrow="false" :index="index" :key="index" |
| 22 | + <u-cell-group> |
| 23 | + <u-cell v-for="(item,index) in citys" :key="index" :title="item.label" :name="index" |
24 | 24 | @click="cityChange"> |
25 | 25 | <u-icon v-if="isChooseC&&city===index" slot="right-icon" size="34" name="checkbox-mark"></u-icon> |
26 | | - </u-cell-item> |
| 26 | + </u-cell> |
27 | 27 | </u-cell-group> |
28 | 28 | </scroll-view> |
29 | 29 | </view> |
30 | 30 | </view> |
31 | 31 |
|
32 | | - <view class="area-item"> |
| 32 | + <view v-if="isChooseC" class="area-item"> |
33 | 33 | <view class="u-padding-10 u-bg-gray" style="height: 100%;"> |
34 | 34 | <scroll-view :scroll-y="true" style="height: 100%"> |
35 | | - <u-cell-group v-if="isChooseC"> |
36 | | - <u-cell-item v-for="(item,index) in areas" :title="item.label" :arrow="false" :index="index" :key="index" |
| 35 | + <u-cell-group> |
| 36 | + <u-cell v-for="(item,index) in areas" :key="index" :title="item.label" :name="index" |
37 | 37 | @click="areaChange"> |
38 | 38 | <u-icon v-if="isChooseA&&area===index" slot="right-icon" size="34" name="checkbox-mark"></u-icon> |
39 | | - </u-cell-item> |
| 39 | + </u-cell> |
| 40 | + </u-cell-group> |
| 41 | + </scroll-view> |
| 42 | + </view> |
| 43 | + </view> |
| 44 | + |
| 45 | + <view v-if="isChooseA && level == 4" class="area-item"> |
| 46 | + <view class="u-padding-10 u-bg-gray" style="height: 100%;"> |
| 47 | + <scroll-view :scroll-y="true" style="height: 100%"> |
| 48 | + <u-cell-group> |
| 49 | + <u-cell v-for="(item,index) in streets" :key="index" :title="item.label" :name="index" |
| 50 | + @click="streetChange"> |
| 51 | + <u-icon v-if="isChooseS&&street===index" slot="right-icon" size="34" name="checkbox-mark"></u-icon> |
| 52 | + </u-cell> |
40 | 53 | </u-cell-group> |
41 | 54 | </scroll-view> |
42 | 55 | </view> |
|
85 | 98 | zIndex: { |
86 | 99 | type: [String, Number], |
87 | 100 | default: 0 |
| 101 | + }, |
| 102 | + level: { |
| 103 | + type: Number, |
| 104 | + default: 3 |
88 | 105 | } |
89 | 106 | }, |
90 | 107 | data() { |
|
99 | 116 | isChooseA: false, //是否已经选择了区 |
100 | 117 | area: 0, //区级下标 |
101 | 118 | areas: [], |
| 119 | + isChooseS: false, //是否已经选择了街道 |
| 120 | + street: 0, //街道下标 |
| 121 | + streets: [], |
102 | 122 | tabsIndex: 0, |
103 | 123 | } |
104 | 124 | }, |
|
127 | 147 | } |
128 | 148 | if (this.isChooseA) { |
129 | 149 | tabsList[2]['name'] = this.areas[this.area]['label']; |
| 150 | + if(this.level == 4) { |
| 151 | + tabsList[3] = { |
| 152 | + name: "请选择" |
| 153 | + }; |
| 154 | + } |
| 155 | + } |
| 156 | + if (this.isChooseS && this.level == 4) { |
| 157 | + tabsList[3]['name'] = this.streets[this.street]['label']; |
130 | 158 | } |
131 | 159 | return tabsList; |
132 | 160 | }, |
|
138 | 166 | methods: { |
139 | 167 | async init() { |
140 | 168 | // 获取所有的省份 |
| 169 | + // https://www.yuque.com/apifm/nu0f75/anab2a |
141 | 170 | const res = await this.$wxapi.province() |
142 | 171 | this.provinces = [] |
143 | 172 | if(res.code == 0) { |
|
149 | 178 | }) |
150 | 179 | } |
151 | 180 | console.log(this.areaCode); |
152 | | - if (this.areaCode.length == 3) { |
153 | | - await this.setProvince("", this.areaCode[0]); |
154 | | - await this.setCity("", this.areaCode[1]); |
155 | | - await this.setArea("", this.areaCode[2]); |
156 | | - } else if (this.defaultRegion.length == 3) { |
157 | | - await this.setProvince(this.defaultRegion[0], ""); |
158 | | - await this.setCity(this.defaultRegion[1], ""); |
159 | | - await this.setArea(this.defaultRegion[2], ""); |
| 181 | + if(this.level == 3) { |
| 182 | + if (this.areaCode.length == 3) { |
| 183 | + await this.setProvince("", this.areaCode[0]); |
| 184 | + await this.setCity("", this.areaCode[1]); |
| 185 | + await this.setArea("", this.areaCode[2]); |
| 186 | + } else if (this.defaultRegion.length == 3) { |
| 187 | + await this.setProvince(this.defaultRegion[0], ""); |
| 188 | + await this.setCity(this.defaultRegion[1], ""); |
| 189 | + await this.setArea(this.defaultRegion[2], ""); |
| 190 | + } |
| 191 | + } |
| 192 | + if(this.level == 4) { |
| 193 | + if (this.areaCode.length == 4) { |
| 194 | + await this.setProvince("", this.areaCode[0]); |
| 195 | + await this.setCity("", this.areaCode[1]); |
| 196 | + await this.setArea("", this.areaCode[2]); |
| 197 | + await this.setStreet("", this.areaCode[3]); |
| 198 | + } else if (this.defaultRegion.length == 4) { |
| 199 | + await this.setProvince(this.defaultRegion[0], ""); |
| 200 | + await this.setCity(this.defaultRegion[1], ""); |
| 201 | + await this.setArea(this.defaultRegion[2], ""); |
| 202 | + await this.setStreet(this.defaultRegion[3], ""); |
| 203 | + } |
160 | 204 | } |
161 | 205 | }, |
162 | 206 | async setProvince(label = "", value = "") { |
|
166 | 210 | console.log(k); |
167 | 211 | if(k != -1) { |
168 | 212 | const v = this.provinces[k] |
169 | | - await this.provinceChange(k) |
| 213 | + await this.provinceChange({ name: k }) |
170 | 214 | } |
171 | 215 | }, |
172 | 216 | async setCity(label = "", value = "") { |
|
175 | 219 | }) |
176 | 220 | if(k != -1) { |
177 | 221 | const v = this.citys[k] |
178 | | - await this.cityChange(k) |
| 222 | + await this.cityChange({ name: k }) |
179 | 223 | } |
180 | 224 | }, |
181 | 225 | async setArea(label = "", value = "") { |
|
184 | 228 | }) |
185 | 229 | if(k != -1) { |
186 | 230 | const v = this.areas[k] |
187 | | - this.isChooseA = true; |
188 | | - this.area = k; |
| 231 | + if(this.level == 3) { |
| 232 | + this.isChooseA = true; |
| 233 | + this.area = k; |
| 234 | + } |
| 235 | + if(this.level == 4) { |
| 236 | + await this.areaChange({ name: k }) |
| 237 | + } |
| 238 | + } |
| 239 | + }, |
| 240 | + async setStreet(label = "", value = "") { |
| 241 | + const k = this.streets.findIndex(v => { |
| 242 | + return value ? v.value == value : v.label == label |
| 243 | + }) |
| 244 | + if(k != -1) { |
| 245 | + const v = this.streets[k] |
| 246 | + this.isChooseS = true; |
| 247 | + this.street = k; |
189 | 248 | } |
190 | 249 | }, |
191 | 250 | close() { |
192 | 251 | this.$emit('input', false); |
193 | 252 | }, |
194 | | - tabsChange(index) { |
195 | | - this.tabsIndex = index; |
| 253 | + tabsChange(e) { |
| 254 | + this.tabsIndex = e.index; |
| 255 | + if(e.index == 0) { |
| 256 | + this.provinceChange({ name: this.province }) |
| 257 | + } |
| 258 | + if(e.index == 1) { |
| 259 | + this.cityChange({ name: this.city }) |
| 260 | + } |
| 261 | + if(e.index == 2) { |
| 262 | + this.areaChange({ name: this.area }) |
| 263 | + } |
| 264 | + if(e.index == 3) { |
| 265 | + this.streetChange({ name: this.street }) |
| 266 | + } |
196 | 267 | }, |
197 | | - async provinceChange(index) { |
| 268 | + async provinceChange(e) { |
| 269 | + const index = e.name |
198 | 270 | this.isChooseP = true; |
199 | 271 | this.isChooseC = false; |
200 | 272 | this.isChooseA = false; |
| 273 | + this.isChooseS = false; |
201 | 274 | this.province = index; |
202 | | - // 接口读取 |
| 275 | + // https://www.yuque.com/apifm/nu0f75/kfukig |
203 | 276 | const res = await this.$wxapi.nextRegion(this.provinces[index].value) |
204 | 277 | this.citys = [] |
205 | 278 | if(res.code == 0) { |
|
213 | 286 | // 接口读取结束 |
214 | 287 | this.tabsIndex = 1; |
215 | 288 | }, |
216 | | - async cityChange(index) { |
| 289 | + async cityChange(e) { |
| 290 | + const index = e.name |
217 | 291 | this.isChooseC = true; |
218 | 292 | this.isChooseA = false; |
| 293 | + this.isChooseS = false; |
219 | 294 | this.city = index; |
220 | | - // 接口读取 |
| 295 | + // https://www.yuque.com/apifm/nu0f75/kfukig |
221 | 296 | const res = await this.$wxapi.nextRegion(this.citys[index].value) |
222 | 297 | this.areas = [] |
223 | 298 | if(res.code == 0) { |
|
231 | 306 | // 接口读取结束 |
232 | 307 | this.tabsIndex = 2; |
233 | 308 | }, |
234 | | - areaChange(index) { |
| 309 | + async areaChange(e) { |
| 310 | + const index = e.name |
235 | 311 | this.isChooseA = true; |
| 312 | + this.isChooseS = false; |
236 | 313 | this.area = index; |
| 314 | + if(this.level == 3) { |
| 315 | + let result = {}; |
| 316 | + result.province = this.provinces[this.province]; |
| 317 | + result.city = this.citys[this.city]; |
| 318 | + result.area = this.areas[this.area]; |
| 319 | + this.$emit('city-change', result); |
| 320 | + this.close(); |
| 321 | + } |
| 322 | + if(this.level == 4) { |
| 323 | + // https://www.yuque.com/apifm/nu0f75/kfukig |
| 324 | + const res = await this.$wxapi.nextRegion(this.areas[index].value) |
| 325 | + this.streets = [] |
| 326 | + if(res.code == 0) { |
| 327 | + res.data.forEach(ele => { |
| 328 | + this.streets.push({ |
| 329 | + label: ele.name, |
| 330 | + value: ele.id |
| 331 | + }) |
| 332 | + }) |
| 333 | + } |
| 334 | + // 接口读取结束 |
| 335 | + this.tabsIndex = 3; |
| 336 | + } |
| 337 | + }, |
| 338 | + streetChange(e) { |
| 339 | + const index = e.name |
| 340 | + this.isChooseS = true; |
| 341 | + this.street = index; |
237 | 342 | let result = {}; |
238 | 343 | result.province = this.provinces[this.province]; |
239 | 344 | result.city = this.citys[this.city]; |
240 | 345 | result.area = this.areas[this.area]; |
| 346 | + result.street = this.streets[this.street]; |
241 | 347 | this.$emit('city-change', result); |
242 | 348 | this.close(); |
243 | 349 | } |
|
257 | 363 | transform: translateX(0); |
258 | 364 |
|
259 | 365 | &.change { |
260 | | - transform: translateX(-33.3333333%); |
| 366 | + transform: translateX(-0%); |
261 | 367 | } |
262 | 368 | } |
263 | | -
|
264 | | - .area-item { |
265 | | - width: 33.3333333%; |
266 | | - height: 800rpx; |
| 369 | + .u-flex { |
| 370 | + width: 100vw; |
| 371 | + display: flex; |
| 372 | + .area-item { |
| 373 | + flex: 1; |
| 374 | + height: 800rpx; |
| 375 | + } |
267 | 376 | } |
268 | 377 | } |
269 | 378 | </style> |
0 commit comments