china-admin-area
Version:
中华人民共和国行政区域数据,适配element-ui antd级联组件
144 lines (132 loc) • 3.32 kB
JavaScript
'use strict';
/**
* 1. 中国省市区基础数据(不包含港澳台地区)
* 2. 处理 element-ui antd 级联组件需要的地址数据(不包含港澳台地区)
*
* @module china-admin-area/regions
* @ignore
*/
const REGION_DATA = require('../data.json')
const { deepClone } = require('./utils')
const textToProvince = {}
const textToProvincePlus = {}
const provinceObject = REGION_DATA['86'] // 省份对象
const regionsData = []
let provinceAndCityData = []
// 处理省级数据
for (const prop in provinceObject) {
regionsData.push({
value: prop, // 省份code值
label: provinceObject[prop] // 省份汉字
});
}
// 处理市级数据
for (let i = 0, len = regionsData.length; i < len; i++) {
const provinceCode = regionsData[i].value
const provinceChildren = []
for (const prop in REGION_DATA[provinceCode]) {
provinceChildren.push({
value: prop,
label: REGION_DATA[provinceCode][prop]
})
}
if (provinceChildren.length) {
regionsData[i].children = provinceChildren
}
}
provinceAndCityData = deepClone(regionsData)
// 处理区级数据
for (let i = 0, len = regionsData.length; i < len; i++) {
const province = regionsData[i].children
if (province) {
for (let j = 0, len = province.length; j < len; j++) {
const cityCode = province[j].value
const cityChildren = []
for (const prop in REGION_DATA[cityCode]) {
// 移除区级数据中的 市辖区数据
const _label = REGION_DATA[cityCode][prop];
if (_label != '市辖区') {
cityChildren.push({
value: prop,
label: _label
})
}
}
if (cityChildren.length) {
province[j].children = cityChildren
}
}
}
}
//处理省级对象映射数据
regionsData.forEach(prop => {
const provinceText = prop.label
let _arr = []
_arr.push(prop)
textToProvince[provinceText] = _arr
})
// 添加“全部”选项
const provinceAndCityDataPlus = deepClone(provinceAndCityData)
provinceAndCityDataPlus.unshift({
value: '',
label: '全部'
})
for (let i = 0, len = provinceAndCityDataPlus.length; i < len; i++) {
const province = provinceAndCityDataPlus[i].children
if (province && province.length) {
province.unshift({
value: '',
label: '全部'
})
for (let j = 0, len = province.length; j < len; j++) {
const city = province[j].children
if (city && city.length) {
city.unshift({
value: '',
label: '全部'
})
}
}
}
}
const regionsDataPlus = deepClone(regionsData)
regionsDataPlus.unshift({
value: '',
label: '全部'
})
for (let i = 0, len = regionsDataPlus.length; i < len; i++) {
const province = regionsDataPlus[i].children
if (province && province.length) {
province.unshift({
value: '',
label: '全部'
})
for (let j = 0, len = province.length; j < len; j++) {
const city = province[j].children
if (city && city.length) {
city.unshift({
value: '',
label: '全部'
})
}
}
}
}
//处理省级对象映射数据 带全部
regionsDataPlus.forEach(prop => {
const provinceText = prop.label
if (provinceText != '全部') {
let _arr = []
_arr.push(prop)
textToProvincePlus[provinceText] = _arr
}
})
module.exports = {
baseRegions: REGION_DATA,
provinceAndCityData,
regionsData,
provinceAndCityDataPlus,
regionsDataPlus,
textToProvince,
textToProvincePlus
}