WebApp【公共组件库】@前端(For Git Submodule)
‘chensiAb’
2025-03-25 3b03f87a02458f719e2eb4bf112a13441b427d14
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
/**
 * ChinaLocations For WeApp
 * @author Tevin
 */
 
import Taro from '@tarojs/taro';
import project from '@project';
import { $hostBoot } from '@components/bases/HostBoot';
import { Tools } from '@components/common/Tools';
 
let locationTreeLv3 = [];
let locationTreeLv4 = [];
let locationRes = {};
let readyCallbacks = [];
 
Taro.request({
    url: $hostBoot.getHost() + project.host.assetsPath + '/datas/ChinaLocations.lv4.min.json',
    header: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'Ax-Rq-Type': 'separation',
    },
    credentials: 'same-origin',
    dataType: 'json',
    timeout: 30 * 1000,
    method: 'GET',
    success: response => {
        locationRes = response.data;
        readyCallbacks.forEach(callback => callback());
    },
});
 
export class ChinaLocations {
 
    onReady(callback) {
        if (Tools.isEmptyObject(locationRes)) {
            readyCallbacks.push(callback);
        } else {
            callback();
        }
    }
 
    _createLocationTree(level) {
        const treeDatas = [];
        // lv1-省
        Object.keys(locationRes).forEach((provinceCode) => {
            const province = {
                label: locationRes[provinceCode].name,
                value: provinceCode,
                children: [],
            };
            // lv2-市
            const cityContainer = locationRes[provinceCode].children;
            Object.keys(cityContainer).forEach((cityCode) => {
                const city = {
                    value: cityCode,
                    area: cityContainer[cityCode].area,  // 电话区码
                    children: [],
                };
                if (typeof cityContainer[cityCode] === 'string') {
                    city.label = cityContainer[cityCode];
                } else {
                    city.label = cityContainer[cityCode].name;
                    // lv3-区
                    const distContainer = cityContainer[cityCode].children || {};
                    Object.keys(distContainer).forEach(distCode => {
                        const dist = {
                            value: distCode,
                        };
                        if (typeof distContainer[distCode] === 'string') {
                            dist.label = distContainer[distCode];
                        } else {
                            dist.label = distContainer[distCode].name;
                            // lv4-街
                            if (level === 4) {
                                dist.children = [];
                                const streetContainer = distContainer[distCode].children || {};
                                Object.keys(streetContainer).forEach(streetCode => {
                                    const street = {
                                        label: streetContainer[streetCode],
                                        value: streetCode,
                                    };
                                    dist.children.push(street);
                                });
                                if (dist.children.length > 0) {
                                    dist.children.unshift({
                                        label: '( 不选 )',
                                        value: '',
                                    });
                                }
                            }
                        }
                        city.children.push(dist);
                    });
                }
                province.children.push(city);
            });
            treeDatas.push(province);
        });
        return treeDatas;
    }
 
    getLocationTree(level, callback) {
        this.onReady(() => {
            if (level === 3) {
                if (locationTreeLv3.length === 0) {
                    locationTreeLv3 = this._createLocationTree(3);
                }
                callback(locationTreeLv3);
            } else if (level === 4) {
                if (locationTreeLv4.length === 0) {
                    locationTreeLv4 = this._createLocationTree(4);
                }
                callback(locationTreeLv4);
            }
        });
    }
 
    // 获取省市区拼合文本
    getRegionText(regions, callback) {
        this.getRegionNames(regions, address => {
            callback(address.join(''));
        });
    }
 
    // 省市区名称
    getRegionNames(regions, callback) {
        this.onReady(() => {
            if (typeof regions === 'string') {
                regions = regions.split(',');
            }
            if (!regions || regions.length === 0 || !regions[0]) {
                callback([]);
            }
            let address = [];
            let tempLocationData = locationRes;
            regions.forEach((code) => {
                if (!code || !tempLocationData[code]) {
                    address.push('');
                    tempLocationData = [];
                } else {
                    if (typeof tempLocationData[code].name === 'string') {
                        address.push(tempLocationData[code].name);
                    } else {
                        address.push(tempLocationData[code]);
                    }
                    tempLocationData = tempLocationData[code].children;
                }
            });
            callback(address);
        });
    }
 
    // 省市区文本转code
    getRegionCodes(regions, callback) {
        this.onReady(() => {
            if (typeof regions === 'string') {
                regions = regions.split(',');
            }
            if (!regions || regions.length === 0 || !regions[0]) {
                callback([]);
            }
            const codes = [];
            // 省
            for (let provinceCode in locationRes) {
                if (locationRes.hasOwnProperty(provinceCode)) {
                    if (locationRes[provinceCode].name === regions[0]) {
                        codes[0] = provinceCode;
                        // 市
                        const provinceContainer = locationRes[provinceCode].children;
                        for (let cityCode in provinceContainer) {
                            if (provinceContainer.hasOwnProperty(cityCode)) {
                                if (provinceContainer[cityCode].name === regions[1]) {
                                    codes[1] = cityCode;
                                    // 区
                                    const distContainer = provinceContainer[cityCode].children;
                                    for (let distCode in distContainer) {
                                        if (distContainer.hasOwnProperty(distCode)) {
                                            if (distContainer[distCode] === regions[2]) {
                                                codes[2] = distCode;
                                                break;
                                            }
                                            if (distContainer[distCode].name === regions[2]) {
                                                codes[2] = distCode;
                                                // 如果存在街道,继续转街道
                                                if (regions[3]) {
                                                    const streetContainer = distContainer[distCode].children;
                                                    Object.keys(streetContainer).forEach(streetCode => {
                                                        if (streetContainer[streetCode] === regions[3]) {
                                                            codes[3] = streetCode;
                                                        }
                                                    });
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        break;
                    }
                }
            }
            callback(codes);
        });
    }
 
    // 电话区码
    getRegionsArea(regions, callback) {
        this.onReady(() => {
            if (typeof regions === 'string') {
                regions = regions.split(',');
            }
            if (!regions || regions.length === 0 || !regions[0] || !regions[1]) {
                callback('');
                return;
            }
            // 中文转编码
            if (!/^\d+$/.test(regions[0])) {
                this.getRegionCodes(regions, regionsCode => {
                    this.getRegionsArea(regionsCode, callback);
                });
                return;
            }
            let area = '';
            // 省
            for (let provinceCode in locationRes) {
                if (locationRes.hasOwnProperty(provinceCode)) {
                    if (provinceCode === regions[0]) {
                        // 市
                        const provinceChildren = locationRes[provinceCode].children;
                        for (let cityCode in provinceChildren) {
                            if (provinceChildren.hasOwnProperty(cityCode)) {
                                if (cityCode === regions[1]) {
                                    area = provinceChildren[cityCode].area;
                                    break;
                                }
                            }
                        }
                        break;
                    }
                }
            }
            callback(area);
        });
    }
}
 
export const $locations = new ChinaLocations();