| | |
| | | let locationTreeLv3 = []; |
| | | let locationTreeLv4 = []; |
| | | let locationRes = {}; |
| | | let readyCallback = () => { }; |
| | | let readyCallbacks = []; |
| | | |
| | | Taro.request({ |
| | | url: $hostBoot.getHost() + project.host.assetsPath + '/datas/ChinaLocations.lv4.min.json', |
| | |
| | | method: 'GET', |
| | | success: response => { |
| | | locationRes = response.data; |
| | | readyCallback(); |
| | | readyCallbacks.forEach(callback => callback()); |
| | | } |
| | | }); |
| | | |
| | |
| | | |
| | | onReady(callback) { |
| | | if (Tools.isEmptyObject(locationRes)) { |
| | | readyCallback = callback; |
| | | readyCallbacks.push(callback); |
| | | } else { |
| | | callback(); |
| | | } |
| | |
| | | return treeDatas; |
| | | } |
| | | |
| | | getLocationTree(level) { |
| | | if (level === 3) { |
| | | if (locationTreeLv3.length === 0) { |
| | | locationTreeLv3 = this._createLocationTree(3); |
| | | 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); |
| | | } |
| | | return locationTreeLv3; |
| | | } else if (level === 4) { |
| | | if (locationTreeLv4.length === 0) { |
| | | locationTreeLv4 = this._createLocationTree(4); |
| | | } |
| | | return locationTreeLv4; |
| | | } |
| | | }); |
| | | } |
| | | |
| | | // 获取省市区拼合文本 |
| | | getRegionText(regions, callback) { |
| | | if (regions.length === 0) { |
| | | return ''; |
| | | } |
| | | let address = ''; |
| | | let tempLocationData = locationRes; |
| | | 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; |
| | | } |
| | | this.getRegionNames(regions, address => { |
| | | callback(address.join('')); |
| | | }); |
| | | return address; |
| | | } |
| | | |
| | | // 省市区名称 |
| | | getRegionNames(regions, callback) { |
| | | if (typeof regions === 'string') { |
| | | regions = regions.split(','); |
| | | } |
| | | if (!regions || regions.length === 0 || !regions[0]) { |
| | | return []; |
| | | } |
| | | 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; |
| | | 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); |
| | | }); |
| | | 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 locationRes) { |
| | | if (locationRes.hasOwnProperty(provinceCode)) { |
| | | if (locationRes[provinceCode].name === regions[0]) { |
| | | codes[0] = provinceCode; |
| | | // 市 |
| | | const provinceChildren = locationRes[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; |
| | | 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; |
| | | callback(codes); |
| | | }); |
| | | } |
| | | |
| | | // 电话区码 |
| | | getRegionsArea(regions) { |
| | | if (typeof regions === 'string') { |
| | | regions = regions.split(','); |
| | | } |
| | | if (!regions || regions.length === 0 || !regions[0]) { |
| | | 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; |
| | | getRegionsArea(regions, callback) { |
| | | this.onReady(() => { |
| | | if (typeof regions === 'string') { |
| | | regions = regions.split(','); |
| | | } |
| | | if (!regions || regions.length === 0 || !regions[0]) { |
| | | callback(''); |
| | | } |
| | | 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; |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | return area; |
| | | callback(area); |
| | | }); |
| | | } |
| | | }; |
| | | |