WXL
5 天以前 871522ed7e06fd9c62a87c178d7f5c88d7853a20
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
<template>
    <view class="u-city-locate">
        <up-index-list :indexList="indexList">
            <template #header>
                <view class="u-current-city-wrap">
                    <view class="u-current-city-title">{{ t("up.cityLocate.locateCity") }}</view>
                    <view class="u-current-city-item" @tap="location">
                        <view class="u-location-city">{{locationCity}}</view>
                    </view>
                </view>
            </template>
            <template :key="index" v-for="(item, index) in cityList">
                <!-- #ifdef APP-NVUE -->
                <up-index-anchor :text="indexList[index]"></up-index-anchor>
                <!-- #endif -->
                <up-index-item>
                    <!-- #ifndef APP-NVUE -->
                    <up-index-anchor :text="indexList[index]"></up-index-anchor>
                    <!-- #endif -->
                    <view class="hot-city-list" v-if="index == 0">
                        <view class="" v-for="(item1, index1) in item" @tap="selectedCity(item1)">
                            <view class="hot-city-item">{{ item1[nameKey] }}</view>
                        </view>
                    </view>
                    <view v-else class="item-list" v-for="(item1, index1) in item" :key="index1">
                        <view class="list__item" @tap="selectedCity(item1)">
                            <text class="list__item__city-name">{{item1[nameKey]}}</text>
                        </view>
                        <up-line></up-line>
                    </view>
                </up-index-item>
            </template>
            <template #footer>
                <view class="u-safe-area-inset--bottom">
                    <text class="list__footer"></text>
                </view>
            </template>
        </up-index-list>
    </view>
</template>
 
<script>
    import { t } from '../../libs/i18n'
    export default{
        name: 'u-city-locate',
        props:{
            indexList: {
                type: Array,
                default: ['🔥']
            },
            cityList:{
                type: Array,
                default: () => {
                    return [
                        [{
                            name: '北京',
                            value: 'beijing'
                        },
                        {
                            name: '上海',
                            value: 'shanghai'
                        },
                        {
                            name: '广州',
                            value: 'guangzhou'
                        },
                        {
                            name: '深圳',
                            value: 'shenzhen'
                        },
                        {
                            name: '杭州',
                            value: 'hangzhou'
                        }]
                    ]
                }
            },
            locationType: {
                type: String,
                default: 'wgs84'
            },
            currentCity: {
                type: String,
                default: ''
            },
            nameKey: {
                type: String,
                default: 'name'
            }
        },
        computed:{
        },
        watch:{
            currentCity(val) {
                this.locationCity = val;
            }
        },
        data(){
            return{
                locationCity: t("up.cityLocate.locating") + '....'
            }
        },
        emits: ['location-success', 'select-city'],
        methods:{
            t,
            // 获取城市
            selectedCity(city){
                this.locationCity = city[this.nameKey];
                this.$emit('select-city', {
                    locationCity: this.locationCity
                });
            },
            // 定位操作
            location(){
                let That = this;
                uni.getLocation({
                    type: this.locationType,
                    geocode:true,
                    success(res){
                        console.log(res);
                        That.locationCity = res.address && res.address.city;
                        That.$emit('location-success', {
                            ...res,
                            locationCity: That.locationCity
                        });
                    },
                    fail(){
                        That.locationCity = t("up.cityLocate.fail");
                    }
                });
            },
        },
        // 页面挂载后进行异步操作
        created(){
        },
        mounted(){
            this.location();
        }
    }
</script>
 
<style lang="scss">
    .list__item {
        padding: 8px 1px;
    }
    .u-current-city-title {
        color: grey;
        margin-bottom: 5px;
    }
    .u-current-city-item {
        height: 30px;
    }
    .hot-city-list {
        display: flex !important;
        flex-direction: row !important;
        padding: 12px 0;
        .hot-city-item {
            padding: 6px 12px;
            margin: 5px;
            border: 1px solid #ededed;
        }
    }
</style>