/**
|
* ChinaLocations For WeApp
|
* @author Tevin
|
*/
|
|
import Taro from '@tarojs/taro';
|
import project from '@project';
|
import { $hostBoot } from '@components/bases/HostBoot';
|
|
const locationTree = [];
|
let ChinaLocationData = {};
|
let readyCallback = () => { };
|
|
Taro.request({
|
url: $hostBoot.getHost() + project.host.assetsPath + '/datas/ChinaLocation.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 => {
|
ChinaLocationData = response.data;
|
Object.keys(ChinaLocationData).forEach((code1) => {
|
const province = {
|
label: ChinaLocationData[code1].name,
|
value: code1,
|
children: [],
|
};
|
const children1 = ChinaLocationData[code1].children;
|
Object.keys(children1).forEach((code2) => {
|
const city = {
|
label: children1[code2].name,
|
value: code2,
|
area: children1[code2].area, // 电话区码
|
children: [],
|
};
|
if (typeof children1[code2].children !== 'undefined') {
|
const children2 = children1[code2].children;
|
Object.keys(children2).forEach((code3) => {
|
city.children.push({
|
label: children2[code3],
|
value: code3,
|
});
|
});
|
}
|
province.children.push(city);
|
});
|
locationTree.push(province);
|
});
|
readyCallback();
|
}
|
});
|
|
export default {
|
onReady(callback) {
|
if (locationTree.length > 0) {
|
callback();
|
} else {
|
readyCallback = callback;
|
}
|
},
|
getLocationTree() {
|
return locationTree;
|
},
|
// 获取省市区拼合文本
|
getRegionText(regions, callback) {
|
if (regions.length === 0) {
|
return '';
|
}
|
let address = '';
|
let tempLocationData = ChinaLocationData;
|
regions.forEach((code) => {
|
if (!code || !tempLocationData[code]) {
|
address.push('');
|
tempLocationData = [];
|
} else {
|
if (typeof tempLocationData[code].name === 'string') {
|
address += tempLocationData[code].name;
|
} else {
|
address += tempLocationData[code];
|
}
|
tempLocationData = tempLocationData[code].children;
|
}
|
});
|
return address;
|
},
|
// 省市区名称
|
getRegionNames(regions, callback) {
|
if (typeof regions === 'string') {
|
regions = regions.split(',');
|
}
|
if (!regions || regions.length === 0 || !regions[0]) {
|
return [];
|
}
|
let address = [];
|
let tempLocationData = ChinaLocationData;
|
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;
|
}
|
});
|
return address;
|
|
},
|
// 省市区文本转code
|
getRegionCodes(regions, callback) {
|
if (typeof regions === 'string') {
|
regions = regions.split(',');
|
}
|
if (!regions || regions.length === 0 || !regions[0]) {
|
return '';
|
}
|
const codes = [];
|
// 省
|
for (let provinceCode in ChinaLocationData) {
|
if (ChinaLocationData.hasOwnProperty(provinceCode)) {
|
if (ChinaLocationData[provinceCode].name === regions[0]) {
|
codes[0] = provinceCode;
|
// 市
|
const provinceChildren = ChinaLocationData[provinceCode].children;
|
for (let cityCode in provinceChildren) {
|
if (provinceChildren.hasOwnProperty(cityCode)) {
|
if (provinceChildren[cityCode].name === regions[1]) {
|
codes[1] = cityCode;
|
// 区
|
const areaChildren = provinceChildren[cityCode].children || [];
|
for (let areaCode in areaChildren) {
|
if (areaChildren.hasOwnProperty(areaCode)) {
|
if (areaChildren[areaCode] === regions[2]) {
|
codes[2] = areaCode;
|
break;
|
}
|
}
|
}
|
break;
|
}
|
}
|
}
|
break;
|
}
|
}
|
}
|
return codes;
|
},
|
// 电话区码
|
getRegionsArea(regions) {
|
if (typeof regions === 'string') {
|
regions = regions.split(',');
|
}
|
if (!regions || regions.length === 0 || !regions[0]) {
|
return '';
|
}
|
let area = '';
|
// 省
|
for (let provinceCode in ChinaLocationData) {
|
if (ChinaLocationData.hasOwnProperty(provinceCode)) {
|
if (provinceCode === regions[0]) {
|
// 市
|
const provinceChildren = ChinaLocationData[provinceCode].children;
|
for (let cityCode in provinceChildren) {
|
if (provinceChildren.hasOwnProperty(cityCode)) {
|
if (cityCode === regions[1]) {
|
area = provinceChildren[cityCode].area;
|
break;
|
}
|
}
|
}
|
break;
|
}
|
}
|
}
|
return area;
|
}
|
};
|