WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2023-07-31 f0adae265c4a40159c665fb424305a27b1afcece
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
/**
 * CChinaArea - 表单项,中国地址三级联动
 * @author Tevin
 */
 
<template>
    <view class="c-china-area">
        <picker
            class="c-china-area-picker"
            mode="multiSelector"
            :range="range"
            :value="current"
            range-key="label"
            @columnchange="evt=>updateColumns(evt.detail)"
            @change="evt=>handleChange(evt.detail.value)"
        >
            <view @tap="evt=>handleOpen(evt)">
                <AtInput
                    ref="input"
                    :name="itemRes.name"
                    :title="itemRes.label"
                    :required="itemRes.required"
                    :error="itemRes.error"
                    :placeholder="placeholder"
                    :value="selected"
                >
                    <AtIcon :value="loading ? 'loading':'chevron-right'" />
                </AtInput>
            </view>
        </picker>
    </view>
</template>
 
<script>
import Taro from '@tarojs/taro';
import { AtInput, AtIcon } from 'taro-ui-vue';
import { $locations } from '@components/forms/chinaArea/ChinaLocations';
import './cChinaArea.scss';
 
export default {
    name: 'CChinaArea',
    components: {
        AtInput,
        AtIcon,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
        // 是否自动通过地理定位获取省市区
        autoGeo: {
            type: Boolean,
            default: false,
        },
        // 联动级别
        level: {
            type: Number,
            default: 3,
        },
        // 占位提示
        placeholder: String,
    },
    data() {
        return {
            loading: true,
            range: [],
            current: [],
        };
    },
    computed: {
        selected() {
            const curVal = this.itemRes.formData[this.itemRes.name];
            if (curVal && (curVal.length === 3 || curVal.length === 4)) {
                return curVal.join(' / ');
            } else {
                return '';
            }
        },
    },
    methods: {
        handleOpen(evt) {
            if (process.env.TARO_ENV === 'h5') {
                if (evt && evt.target.className.indexOf('at-input__title') >= 0) {
                    evt.stopPropagation();
                    evt.preventDefault();
                    return;
                }
            }
            $locations.getLocationTree(this.level, locationTree => {
                const curVal = this.itemRes.formData[this.itemRes.name];
                const range2 = (locationTree[0] || {}).children || [];
                const range3 = (range2[0] || {}).children || [];
                const range = [locationTree, range2, range3];
                const current = [0, 0, 0];
                if (this.level === 4) {
                    const range4 = (range3[0] || {}).children || [];
                    range[3] = range4;
                    current[3] = '';
                }
                // 有值
                if (curVal && curVal.length > 0) {
                    $locations.getRegionCodes(curVal, codes => {
                        // 省
                        if (codes[0]) {
                            const proviceIndex = locationTree.findIndex(
                                provice => provice.value === codes[0]
                            );
                            if (proviceIndex >= 0) {
                                range[1] = locationTree[proviceIndex].children;
                                range[2] =
                                    locationTree[proviceIndex].children[0].children;
                                current[0] = proviceIndex;
                                // 市
                                if (codes[1]) {
                                    const cityIndex = range[1].findIndex(
                                        city => city.value === codes[1]
                                    );
                                    if (cityIndex >= 0) {
                                        range[2] = range[1][cityIndex].children;
                                        current[1] = cityIndex;
                                    }
                                    // 区
                                    if (codes[2]) {
                                        const distIndex = range[2].findIndex(
                                            dist => dist.value === codes[2]
                                        );
                                        if (distIndex >= 0) {
                                            current[2] = distIndex;
                                        }
                                        // 街
                                        if (this.level === 4) {
                                            range[3] =
                                                range[2][current[2]].children || [];
                                            const streetIndex = range[3].findIndex(
                                                street => street.value === codes[3]
                                            );
                                            if (streetIndex >= 0) {
                                                current[3] = streetIndex;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
                this.range = range;
                this.current = current;
            });
        },
        updateColumns(roll) {
            $locations.getLocationTree(this.level, locationTree => {
                // 第一列滚动
                if (roll.column === 0) {
                    const cities = locationTree[roll.value].children;
                    this.range = [this.range[0], cities, cities[0].children];
                    this.current = [roll.value, 0, 0];
                    if (this.level === 4) {
                        const range3 = (cities[0].children[0] || {}).children || [];
                        this.range.splice(3, 1, range3);
                        this.current.splice(3, 1, '');
                    }
                }
                // 第二列滚动
                else if (roll.column === 1) {
                    const dists =
                        locationTree[this.current[0]].children[roll.value].children;
                    this.range = [this.range[0], this.range[1], dists];
                    this.current = [this.current[0], roll.value, 0];
                    if (this.level === 4) {
                        const range3 = dists[0].children || [];
                        this.range.splice(3, 1, range3);
                        this.current.splice(3, 1, '');
                    }
                }
                // 第三列滚动
                else if (roll.column === 2) {
                    this.current.splice(2, 1, roll.value);
                    if (this.level === 4) {
                        const streets =
                            locationTree[this.current[0]].children[this.current[1]]
                                .children[roll.value].children || [];
                        this.range.splice(3, 1, streets);
                        this.current.splice(3, 1, '');
                    }
                }
                // 第四列滚动
                else if (roll.column === 3) {
                    this.current.splice(3, 1, roll.value);
                }
            });
        },
        handleChange(detail) {
            $locations.getLocationTree(this.level, locationTree => {
                const names = [];
                const provice = locationTree[detail[0]];
                names[0] = provice.label;
                const city = provice.children[detail[1]] || {};
                names[1] = city.label;
                const dist = city.children[detail[2]] || {};
                names[2] = dist.label;
                if (this.level === 4) {
                    const street = (dist.children || [])[detail[3]] || {};
                    if (street.value) {
                        names[3] = street.label;
                    }
                }
                this.itemRes.onChange(names);
            });
        },
        _getGeoLocation() {
            this.loading = true;
            Taro.getLocation({
                type: 'wgs84',
                success: res => {
                    const latitude = res.latitude;
                    const longitude = res.longitude;
                    const myGeo = new BMap.Geocoder();
                    // 根据坐标得到地址描述
                    myGeo.getLocation(new BMap.Point(longitude, latitude), result => {
                        if (!result) {
                            this.locationLoading = false;
                            return;
                        }
                        const address = result.addressComponents;
                        const regions = [
                            address.province.replace(/省|市|自治区|特别行政区/g, ''),
                            address.city,
                            address.district,
                            address.street,
                        ];
                        $locations.getRegionCodes(regions, codes => {
                            if (codes && codes.length > 0) {
                                $locations.getRegionNames(codes, names => {
                                    this.itemRes.onChange(names);
                                });
                            }
                        });
                        this.loading = false;
                    });
                },
                cancel: res => (this.loading = false),
                fail: err => (this.loading = false),
            });
        },
    },
    mounted() {
        $locations.onReady(() => {
            this.loading = false;
            this.handleOpen();
            // 开启自动定位时,延迟 0.1 秒后发起定位
            if (this.autoGeo) {
                if (process.env.TARO_ENV === 'weapp') {
                    setTimeout(() => {
                        // TODO:小程序中定位
                    }, 100);
                } else if (process.env.TARO_ENV === 'h5') {
                    if (typeof wx === 'undefined') {
                        console.warn('无法进行地理位置定位,wx 不存在!');
                        return;
                    }
                    wx.ready(() => {
                        if (typeof BMap === 'undefined') {
                            console.warn('无法进行地理位置定位,BMap 不存在!');
                            return;
                        }
                        this._getGeoLocation();
                    });
                }
            }
        });
    },
};
</script>