From 433cf5bfd7d0bd602c3147c15720a654ea47b028 Mon Sep 17 00:00:00 2001 From: Tevin <tingquanren@163.com> Date: Mon, 24 Mar 2025 18:31:32 +0800 Subject: [PATCH] Merge branch 'master' of ssh://dev.zhiheiot.com:29418/mob-components --- forms/chinaArea/ChinaLocations.js | 355 +++++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 226 insertions(+), 129 deletions(-) diff --git a/forms/chinaArea/ChinaLocations.js b/forms/chinaArea/ChinaLocations.js index 61825ca..ee602ce 100644 --- a/forms/chinaArea/ChinaLocations.js +++ b/forms/chinaArea/ChinaLocations.js @@ -1,154 +1,251 @@ /** - * ChinaLocations For Normal + * ChinaLocations For WeApp * @author Tevin */ -import ChinaLocationData from './ChinaLocationData.json'; +import Taro from '@tarojs/taro'; +import project from '@project'; +import { $hostBoot } from '@components/bases/HostBoot'; +import { Tools } from '@components/common/Tools'; -const locationTree = []; +let locationTreeLv3 = []; +let locationTreeLv4 = []; +let locationRes = {}; +let readyCallbacks = []; -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); +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 default { +export class ChinaLocations { + onReady(callback) { - callback(); - }, - getLocationTree() { - return locationTree; - }, + 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) { - if (regions.length === 0) { - return ''; - } - let address = ''; - let tempLocationData = ChinaLocationData; - regions.forEach((code) => { - if (typeof tempLocationData[code].name === 'string') { - address += tempLocationData[code].name; - } else { - address += tempLocationData[code]; - } - tempLocationData = tempLocationData[code].children; + getRegionText(regions, callback) { + this.getRegionNames(regions, address => { + callback(address.join('')); }); - return address; - }, + } + // 省市区名称 - getRegionNames(regions) { - if (typeof regions === 'string') { - regions = regions.split(','); - } - if (!regions || regions.length === 0 || !regions[0]) { - return []; - } - let address = []; - let tempLocationData = ChinaLocationData; - regions.forEach((code) => { - if (!tempLocationData[code]) { - return; + getRegionNames(regions, callback) { + this.onReady(() => { + if (typeof regions === 'string') { + regions = regions.split(','); } - if (typeof tempLocationData[code].name === 'string') { - address.push(tempLocationData[code].name); - } else { - address.push(tempLocationData[code]); + if (!regions || regions.length === 0 || !regions[0]) { + callback([]); } - tempLocationData = tempLocationData[code].children; + 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); }); - return address; - }, + } + // 省市区文本转code - getRegionCodes(regions) { - 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; + 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; } } + 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; + callback(codes); + }); } -}; \ No newline at end of file + + // 电话区码 + 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(); \ No newline at end of file -- Gitblit v1.9.1