quanwei
2025-11-27 4711bb8fb2fb16c4eb1cdf6c0314069d85e77a67
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<template>
    <view class="chose-city">
        <!-- #ifdef APP-PLUS -->
        <header-bar></header-bar>
        <!-- #endif -->
        <!-- 城市搜索 -->
        <view class="city-search-wrap" :style="topBarHeight() == 0 ? '': 'padding-top:'+topBarTop()+'px'" id="searchBox">
            <view class="search">
                <view class="r-cancel" @click="closeModal">
                    <text class="iconfont icon-guanbi"></text>
                </view>
                <view class="l-search">
                    <view class="icon-search">
                        <view class="cuIcon-search"></view>
                    </view>
                    <input class="input-search" type="text" :value="inputValue" placeholder="城市名/拼音" placeholder-style="color:#8E8F97"
                     :focus="searchFocus" @input="searchChange" />
                    <text class="clear-input iconfont icon-icon-test" v-if="isClearBtn" @click="inputValue = ''"></text>
                </view>
            </view>
            <!-- 搜索列表  -->
            <view class="reach-content" v-show="inputValue">
                <block v-show="searchData.length">
                    <view class="li" v-for="item in searchData" :key="item.citycode" @click="selectCity(item)">
                        {{item.name}}
                    </view>
                </block>
                <view class="has-no-data" v-show="hasNoData">没有找到匹配数据~</view>
            </view>
        </view>
        <!-- 城市列表 -->
        <scroll-view class="scroll-view" scroll-y scroll-with-animation="true" enable-back-to-top="true" :scroll-into-view="toIndex"
         @scroll="scrollHandle" :style="'height:'+scrollviewHigh+'px;'" v-if="!inputValue">
            <view class="block">
                <!-- 您所在的地区 -->
                <view class="area list-item myarea" id="area">
                    <view class="title-wrapp p-0-30 d-s-c">
                        <view class="c-title">
                            <text class="l flex-1">当前:{{myCityObj.name}}{{myCityObj.city==myCityObj.name?'全城':myCityObj.city}}</text>
                            <view class="d-s-c" @click="showArea = !showArea" :class="{'area-icon' : showArea}">
                                <text>切换区县</text>
                                <view class="iconfont icon-icon1"></view>
                            </view>
                        </view>
                    </view>
                    <view class="ul bg-white p-0-30 pt10" v-if="showArea">
                        <view class="li font-clamp" @click="selectCity(realCityObj)">{{realCityObj.name}}全城</view>
                        <view class="li font-clamp" :class="{'now' : item.name==myCityObj.city}" v-for="(item, key) of regionList" :key="key" :id="item.id" @click="selectCity(item)">
                            <text class="text">{{ item.name }}</text>
                        </view>
                    </view>
                </view>
                <!-- 历史记录 -->
                <view class="area list-item history p-0-30" id="record">
                    <view class="title-wrapp pt20">
                        <view class="c-title">
                            <text class="l">定位/最近访问</text>
                        </view>
                    </view>
                    <view class="ul">
                        <view class="li font-clamp" @click="selectCity(nowCityObj, 'refresh')">
                            <text class="f26 dominant icon iconfont icon-dizhi1" v-if="hasLocation"></text>
                            <text class="f26 red icon iconfont icon-fasong" v-else></text>
                            <text class="text">{{ hasLocation ? nowCityObj.name:'定位失败,点击重新定位' }}</text>
                        </view>
                        <view class="li font-clamp" v-for="item in recordList" :key="item.id" @click="selectCity(item)">
                            {{ item.name }}
                        </view>
                    </view>
                </view>
            </view>
            <!-- 城市列表  -->
            <view class="city-list">
                <view class="list list-item" v-for="(item, key) of cityList" :key="key" :id="item.nameType">
                    <view class="c-title">{{ item.nameType }}</view>
                    <view class="item" v-for="innerItem in item.list" :key="innerItem.id" @click="selectCity(innerItem)">
                        {{ innerItem.name }}
                    </view>
                </view>
            </view>
        </scroll-view>
 
        <!-- 字母列表 -->
        <view class="alphabet" @touchstart="touchStart" @touchend="touchEnd" @touchmove.stop="touchMove">
            <view v-for="(item, index) in alphabet" :key="index" @touchstart="getLetter" @touchend="setLetter" :id="item">
                <view class="item" :class="{ active: currentLetter == item }">
                    {{ item == 'area' ? '当前' : item == 'record' ? '定位' : item }}
                </view>
            </view>
        </view>
    </view>
</template>
 
<script>
    // import cityJson from '@/static/dataJson/city.json'
    export default {
        data() {
            return {
                isIPX: null,
                regionId: null, // 区域ID
                isToggle: true,
                isReach: false,
                inputValue: '',
                searchData: [], // 搜索的数据
                isClearBtn: false,
                toIndex: '', // 跳转的索引的字母
                tipsLetter: '', // 滑动显示字母
                timer: null,
                hasNoData: false,
                searchFocus: false,
                letterDetails: [],
                currentLetter: 'area', //默认选择
                cityArr: [],
                recordList: [],
                cityList: [],
                hasLocation: true,
                alphabet:[],
                refresh: false,
                // 是否显示区县
                showArea: false,
                // 区县列表
                regionList: [],
                // 当前存储的二级城市
                realCityObj: [],
                /*手机高度*/
                phoneHeight: 0,
                /*可滚动视图区域高度*/
                scrollviewHigh: 0,
            };
        },
        props: ['myCityObj', 'nowCityObj', 'citydata'],
        mounted() {
            this.cityArr = this.citydata.cityList
            if (this.cityArr && this.cityArr[0]) {
                this.cityArr.map(v => {
                    v.nameType = v.pinyin.substr(0, 1).toUpperCase()
                })
                this.cityList = this.groupArr(this.cityArr, 'nameType')
            }
            this.recordList = uni.getStorageSync('recordList') || [];
            this.alphabet = this.citydata.alphabet
            this.getLocation()
            this.realCityObj = uni.getStorageSync('realCityObj');
            this.regionList = uni.getStorageSync('regionObj');
            this.init();
        },
        watch: {
            // 城市搜索输入框
            inputValue(newVal) {
                this.isClearBtn = newVal ? true : false;
 
                if (this.timer) {
                    clearTimeout(this.timer);
                }
 
                if (!this.inputValue) {
                    this.searchData = [];
                    return;
                }
                this.timer = setTimeout(() => {
                    const result = [];
                    this.cityList.map(v => {
                        v.list.forEach((item) => {
                            if (/^[a-zA-Z]+$/.test(item.pinyin) && item.pinyin.toLowerCase().includes(this.inputValue.toLowerCase()) ||
                                item.name.includes(this.inputValue)) {
                                result.push(item);
                            }
                        });
                    })
                    this.searchData = result;
                    if (this.searchData.length === 0) {
                        this.hasNoData = true;
                    } else {
                        this.hasNoData = false;
                    }
                }, 500);
            },
            isReach(val) {
                this.searchFocus = val;
            },
        },
        methods: {
            /*初始化*/
            init() {
                let _this = this;
                uni.getSystemInfo({
                    success(res) {
                            _this.phoneHeight = res.windowHeight;
                            // 计算组件的高度
                            let h = _this.phoneHeight - 60;
                            _this.scrollviewHigh = h;
                            let view = uni.createSelectorQuery().in(this).select('#searchBox');
                            view.boundingClientRect(data => {
                                
                                
                            }).exec();
                        
                    }
                });
            },
            getLocation() {
                const that = this
                uni.getLocation({
                    // #ifdef MP-ALIPAY
                    type: 'wgs84',
                    // #endif
                    success(res) {
                        console.log('---',res)
                        that.hasLocation = true
                        that.$emit('selectCityByLatLng', res, that.refresh)
                    },
                    fail(err) {
                        that.hasLocation = false
                        uni.showToast({
                            icon:'none',
                            title: '获取定位失败,请手动选择或重试'
                        })
                    },
                })
            },
            groupArr(list, field) {
                var fieldList = [],
                    att = [];
                list.map((e) => {
                    fieldList.push(e[field])
                })
                //数组去重
                fieldList = fieldList.filter((e, i, self) => {
                    return self.indexOf(e) == i
                })
                for (var j = 0; j < fieldList.length; j++) {
                    //过滤出匹配到的数据
                    var arr = list.filter((e) => {
                        return e[field] == fieldList[j];
                    })
                    att.push({
                        nameType: arr[0].nameType,
                        list: arr
                    })
                }
                return att;
            },
            selectCity(item, type) {
                // if(type === 'refresh' && !this.hasLocation){
                //     this.refresh = true;
                //     // 获取定位
                //     return this.getLocation()
                // }
                // 如果是选择区县
                if (item.level == 2) {
                    this.regionList = item.regions
                    this.realCityObj = item;
                    uni.setStorageSync('realCityObj', item);
                    uni.setStorageSync('regionObj', this.regionList);
                }
                // 保存历史
                if (uni.getStorageSync('myCityObj')) {
                    this.setRecord(uni.getStorageSync('myCityObj'));
                }
                uni.setStorageSync('myCityObj', item)
                this.$emit('selectCity', item)
                // 当前项目是需要选择到区域,所以选择城市后回到区县的地方
                this.toIndex = 'area';
                setTimeout(() => {
                    this.toIndex = '';
                }, 1000);
            },
            setRecord(item) {
                let recordList = this.recordList;
                console.log(recordList);
                if (recordList.length > 0) {
                    for (let i = 0; i < recordList.length; i++) {
                        if (recordList[i].name == item.name) {
                            return;
                        }
                    }
                }
                recordList.unshift(item);
                uni.setStorageSync('recordList', recordList.slice(0, 2)); // 只存两个历史
            },
            closeModal(){
                this.$emit('closeModal')
            },
            //列表滚动,和右边字母表对应
            scrollHandle(e) {
                let view = uni.createSelectorQuery().in(this).selectAll('.list-item');
                view
                    .boundingClientRect((d) => {
                        let top = d[0].top;
                        d.forEach((item) => {
                            item.top = item.top - top;
                            item.bottom = item.bottom - top;
                            this.letterDetails.push({
                                id: item.id,
                                top: item.top,
                                bottom: item.bottom,
                            });
                        });
                    })
                    .exec();
 
                const scrollTop = e.detail.scrollTop;
                this.letterDetails.some((item) => {
                    if (scrollTop >= item.top && scrollTop <= item.bottom - 20) {
                        this.currentLetter = item.id;
                        //当前固定用的是粘性定位,如果不用粘性定位,在这里设置
                        return true;
                    }
                });
            },
 
            //搜索
            searchChange(e) {
                let {
                    value
                } = e.detail;
                this.inputValue = value;
            },
            // 触发开始
            touchStart(e) {
                // console.log(e);
            },
            //移动时
            touchMove(e) {
                uni.vibrateShort();
                let y = e.touches[0].clientY;
                let offsettop = e.currentTarget.offsetTop;
 
                //判断选择区域,只在选择区才会生效
                if (y > offsettop) {
                    let num = parseInt((y - offsettop) / 15); //右边每个字母元素的高度
                    let letter = this.alphabet[num];
                    this.tipsLetter = letter;
 
                    let curentLetter = this.letterTransform(letter);
                    uni.showToast({
                        title: curentLetter,
                        icon: 'none',
                    });
                }
            },
            //触发结束
            touchEnd() {
                this.toIndex = this.tipsLetter;
            },
            //移动开始获取字母,并放大提示
            getLetter(e) {
                uni.vibrateShort();
                let {
                    id
                } = e.currentTarget;
                this.tipsLetter = id;
 
                let curentLetter = this.letterTransform(id);
                uni.showToast({
                    title: curentLetter,
                    icon: 'none',
                });
            },
            //移动结束设置字母,赋值到toIndex
            setLetter() {
                this.toIndex = this.tipsLetter;
            },
 
            //提示字母转换
            letterTransform(letter) {
                let str = '';
                if (letter == 'area') {
                    str = '当前';
                } else if (letter == 'record') {
                    str = '定位';
                } else {
                    str = letter;
                }
                return str;
            },
        },
    }
</script>
 
<style lang="scss" scoped>
    .area-icon .iconfont {
        transform: rotateX(180deg);
    }
    .area.history .title-wrapp {
        background: transparent;
    }
    .area.myarea .c-title {
        color: #666666;
    }
    .chose-city {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 99;
        background: #fff;
    }
 
    .city-search-wrap {
        width: 100%;
        box-sizing: border-box;
        height: 128rpx;
 
        .search {
            width: 750rpx;
            height: 60rpx;
            display: flex;
            align-items: center;
            font-size: 28rpx;
            color: #222;
            padding: 0 30rpx;
            box-sizing: border-box;
            background: #fff;
 
            .l-search {
                width: 450rpx;
                position: relative;
                height: 60rpx;
                line-height: 60rpx;
 
                .icon-search {
                    font-size: 28rpx;
                    position: absolute;
                    left: 30rpx;
                    top: 0;
                    color: #8e8f97;
                    font-weight: 700;
                    height: 60rpx;
                    line-height: 60rpx;
                }
                .input-search {
                    width: 450rpx;
                    height: 60rpx;
                    box-sizing: border-box;
                    padding: 0 84rpx 0 84rpx;
                    text-align: left;
                    background: #f4f5f9;
                    border-radius: 60rpx;
                    border: 0;
                }
 
                .clear-input {
                    font-size: 30rpx;
                    position: absolute;
                    right: 10rpx;
                    top: 50%;
                    transform: translateY(-50%);
                    padding: 10rpx;
                    color: #8e8f97;
                }
            }
 
            .r-cancel {
                width: 80rpx;
                box-sizing: border-box;
                font-size: 28rpx;
                height: 60rpx;
                line-height: 60rpx;
                background: transparent;
                border: 0;
                color: #519AD2;
                
                .iconfont {
                    color: #333333;
                }
            }
        }
    }
 
    .reach-content {
        padding-left: 36rpx;
        box-sizing: border-box;
        .li {
            width: 714rpx;
            font-size: 28rpx;
            height: 100rpx;
            line-height: 100rpx;
            color: #333;
            position: relative;
            box-sizing: border-box;
            border-bottom: 2rpx solid #F5F5F5;
        }
    }
 
    .block {
        // padding: 0 36rpx;
        box-sizing: border-box;
        background-color: #f7f7f7;
    }
 
    .top-search {
        line-height: 72rpx;
        padding: 14rpx 30rpx 0;
        box-sizing: border-box;
        margin-bottom: 26rpx;
 
        .item {
            background: #F5F5F5;
            border-radius: 12rpx;
            font-size: 28rpx;
            text-align: center;
            color: #999999;
            /* #ifdef MP-ALIPAY */
            height: 72rpx;
            line-height: 72rpx;
 
            /* #endif */
            text {
                padding-left: 20rpx;
                color: #c1c2cd;
                vertical-align: middle;
                position: relative;
                top: -4rpx;
            }
 
        }
    }
 
    .scroll-view {
        width: 100%;
        height: calc(100vh - 110rpx);
        box-sizing: border-box;
    }
 
    .area {
        padding-bottom: 8rpx;
 
        .title-wrapp {
            // position: sticky;
            // top: 0;
            // left: 0;
            background: #fff;
        }
 
        .c-title {
            width: 100%;
            box-sizing: border-box;
            font-size: 28rpx;
            color: #999999;
            margin-bottom: 24rpx;
            display: inline-flex;
            justify-content: space-between;
            align-items: center;
 
            .r {
                font-size: 24rpx;
                color: #8e8f97;
                display: inline-block;
                align-items: center;
 
                .iconfont {
                    font-size: 24rpx;
                }
            }
        }
 
        .ul {
            display: flex;
            flex-wrap: wrap;
 
            .li {
                width: 205rpx;
                padding: 0 10rpx;
                box-sizing: border-box;
                height: 72rpx;
                line-height: 68rpx;
                text-align: center;
                font-size: 26rpx;
                color: #333;
                border-radius: 8rpx;
                margin: 0 18rpx 28rpx 0;
                border: 2rpx solid #E2E2E2;
                background-color: #ffffff;
                text-overflow: ellipsis;
                white-space: nowrap;
                overflow: hidden;
 
                &:nth-child(3n) {
                    margin-right: 0;
                }
 
                &.now {
                    padding: 0 32rpx 0 22rpx;
                    background-color: #f7f7f7;
 
                    .icon {
                        width: 50rpx;
                        height: 50rpx;
                        background-size: 100%;
                        vertical-align: middle;
                        position: relative;
                        top: -4rpx;
                    }
 
                    .text {
                        padding-left: 10rpx;
                    }
                }
 
                &.active {
                    font-weight: 500;
                    background: #ffde45;
                }
            }
 
            .hover {
                background: #ffde45;
            }
        }
    }
 
    .city-list {
        width: 750rpx;
        padding-bottom: 50rpx;
 
        .c-title {
            height: 60rpx;
            line-height: 60rpx;
            font-size: 30rpx;
            font-weight: 500;
            color: #272636;
            background: #f7f7f7;
            box-sizing: border-box;
            padding-left: 30rpx;
            position: sticky;
            top: 0;
            left: 0;
            z-index: 2;
            border-top: 2rpx solid #eee;
        }
 
        .item {
            width: 714rpx;
            margin-left: 30rpx;
            padding: 0 30rpx 0 0;
            height: 80rpx;
            line-height: 80rpx;
            color: #333;
            font-size: 28rpx;
            box-sizing: border-box;
            border-bottom: 2rpx solid #F5F5F5;
            
            &:last-child {
                border-bottom: 0;
            }
        }
    }
 
    .alphabet {
        position: fixed;
        right: 0;
        bottom: 20%;
        width: calc(750rpx - 680rpx);
        text-align: center;
        font-size: 20rpx;
        font-weight: 700;
        color: #8e8f97;
        z-index: 99;
 
        .item {
            height: 15px;
            line-height: 15px;
        }
 
        .active {
            color: #222;
        }
    }
 
    .has-no-data {
        font-size: 24rpx;
        text-align: center;
        color: #8e8f97;
        margin-top: 50rpx;
    }
</style>