From 0879cf5da29bbdae1268b70056671537bb3ef9e6 Mon Sep 17 00:00:00 2001 From: Tevin <tingquanren@163.com> Date: Mon, 07 Nov 2022 19:17:49 +0800 Subject: [PATCH] 微调控制台方法 --- forms/chinaArea/ChinaLocations.weapp.js | 70 ++++++++++++++++++++++++++-------- 1 files changed, 53 insertions(+), 17 deletions(-) diff --git a/forms/chinaArea/ChinaLocations.weapp.js b/forms/chinaArea/ChinaLocations.weapp.js index d674b32..8a92430 100644 --- a/forms/chinaArea/ChinaLocations.weapp.js +++ b/forms/chinaArea/ChinaLocations.weapp.js @@ -1,18 +1,18 @@ /** - * ChinaLocations + * ChinaLocations For WeApp * @author Tevin */ import Taro from '@tarojs/taro'; -import { Fetcher } from '@components/bases/Fetcher'; import project from '@project'; +import { $hostBoot } from '@components/bases/HostBoot'; const locationTree = []; let ChinaLocationData = {}; let readyCallback = () => { }; Taro.request({ - url: Fetcher.host + project.host.assetsPath + '/datas/ChinaLocation.json', + url: $hostBoot.getHost() + project.host.assetsPath + '/datas/ChinaLocation.json', header: { 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', @@ -35,6 +35,7 @@ const city = { label: children1[code2].name, value: code2, + area: children1[code2].area, // 电话区码 children: [], }; if (typeof children1[code2].children !== 'undefined') { @@ -56,7 +57,7 @@ export default { onReady(callback) { - if (ChinaLocationData.length > 0) { + if (locationTree.length > 0) { callback(); } else { readyCallback = callback; @@ -73,12 +74,17 @@ let address = ''; let tempLocationData = ChinaLocationData; regions.forEach((code) => { - if (typeof tempLocationData[code].name === 'string') { - address += tempLocationData[code].name; + if (!code || !tempLocationData[code]) { + address.push(''); + tempLocationData = []; } else { - address += tempLocationData[code]; + if (typeof tempLocationData[code].name === 'string') { + address += tempLocationData[code].name; + } else { + address += tempLocationData[code]; + } + tempLocationData = tempLocationData[code].children; } - tempLocationData = tempLocationData[code].children; }); return address; }, @@ -93,15 +99,17 @@ let address = []; let tempLocationData = ChinaLocationData; regions.forEach((code) => { - if (!tempLocationData[code]) { - return; - } - if (typeof tempLocationData[code].name === 'string') { - address.push(tempLocationData[code].name); + if (!code || !tempLocationData[code]) { + address.push(''); + tempLocationData = []; } else { - address.push(tempLocationData[code]); + if (typeof tempLocationData[code].name === 'string') { + address.push(tempLocationData[code].name); + } else { + address.push(tempLocationData[code]); + } + tempLocationData = tempLocationData[code].children; } - tempLocationData = tempLocationData[code].children; }); return address; @@ -127,7 +135,7 @@ if (provinceChildren[cityCode].name === regions[1]) { codes[1] = cityCode; // 区 - const areaChildren = provinceChildren[cityCode].children; + const areaChildren = provinceChildren[cityCode].children || []; for (let areaCode in areaChildren) { if (areaChildren.hasOwnProperty(areaCode)) { if (areaChildren[areaCode] === regions[2]) { @@ -145,6 +153,34 @@ } } 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; + } }; \ No newline at end of file -- Gitblit v1.9.1