getAreaData.js
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import areaData from './areaData'
export const newAreaData = function (data) { // 得到一个含有‘全部’字段的 省市区 的地区数组
let newAreaData = data ?JSON.parse(JSON.stringify(data)) : JSON.parse(JSON.stringify(areaData))
newAreaData.map((outerItem , outerIndex)=>{
outerItem.children&&outerItem.children.unshift({'value': outerItem.value + '100' , 'label' : "全部"})
outerItem.children&&outerItem.children.map((innerItem , innerIndex)=>{
if(innerIndex!=0){
innerItem.children&&innerItem.children.unshift({'value': innerItem.value + '100' , 'label' : "全部"})
}
})
})
return newAreaData
}
export const getnewAreaDataByCode = (code = '') => {
let name = '';
const eachName = (items) => {
items.map((item, i) => {
if (item.value == code) {
name = item.label;
}
if (item.children)
eachName(item.children);
});
}
eachName(newAreaData());
return name;
}
export const getToponymyCode = function (value) { //得到省市区 或 省全部 或 市全部 最后一个code码 全部 字段除外
let codeAndName = [] , searchCode = '' , pureCode = []
if (value && value.length > 0){
if (value.length == 2){
codeAndName[0] = { 'province_code': value[0] , 'province' : getnewAreaDataByCode( value[0] ) };
codeAndName[1] = { 'city_code': '' , 'city' : ''};
codeAndName[2] = { 'district_code': '' , 'district' : ''};
searchCode = '&province_code=' + value[0] + '&city_code=%20&district_code=%20'
} else if (value.length == 3){
if ((value[1]+'100') == value[2] ){
codeAndName[0] ={ 'province_code': value[0] , 'province' : getnewAreaDataByCode( value[0] ) };
codeAndName[1] ={ 'city_code': value[1] , 'city' : getnewAreaDataByCode( value[1] ) };
codeAndName[2] ={ 'district_code': '' , 'district' : '' };
searchCode = '&province_code=' + value[0] +'&city_code=' + value[1] + '&district_code=%20'
}else{
codeAndName[0] = { 'province_code': value[0] , 'province' : getnewAreaDataByCode( value[0] ) }
codeAndName[1] = { 'city_code': value[1] , 'city' : getnewAreaDataByCode( value[1] ) }
codeAndName[2] = { 'district_code': value[2] , 'district' : getnewAreaDataByCode( value[2] ) }
searchCode = '&province_code=' + value[0] +'&city_code=' + value[1] + '&district_code='+ value[2]
}
}
}
return {
codeAndName,
searchCode
}
}
export const getPureCode = function (value) { //得到省市区 或 省全部 或 市全部 最后一个code码 全部 字段除外
let pureCode = []
if (value){
if(value.province_code&&value.city_code&&value.district_code){
pureCode = [value.province_code , value.city_code , value.district_code]
}else if(value.province_code&&value.city_code&&!value.district_code){
pureCode = [value.province_code , value.city_code , value.city_code+ '100' , ]
}else if(value.province_code&&!value.city_code &&!value.district_code){
pureCode = [value.province_code , value.province_code + '100' ]
}
}
return {
pureCode
}
}
export const getPureCodeByType = function (value, type) { //得到省市区 或 省全部 或 市全部 最后一个code码 全部 字段除外
let pureCodeByType = []
if (value && type) {
if (value[`${type}` + '_province_code'] && value[`${type}` + '_city_code'] && value[`${type}` + '_district_code']) {
pureCodeByType = [value[`${type}` + '_province_code'], value[`${type}` + '_city_code'], value[`${type}` + '_district_code']]
} else if (value[`${type}` + '_province_code'] && value[`${type}` + '_city_code'] && !value[`${type}` + '_district_code']) {
pureCodeByType = [value[`${type}` + '_province_code'], value[`${type}` + '_city_code'], value[`${type}` + '_city_code'] + '100' ]
} else if (value[`${type}` + '_province_code'] && !value[`${type}` + '_city_code'] && !value[`${type}` + '_district_code'] ){
pureCodeByType = [value[`${type}` + '_province_code'], value[`${type}` + '_province_code'] + '100']
}
} else if (value && !type) {
if (value.province_code && value.city_code && value.district_code) {
pureCodeByType = [value.province_code, value.city_code, value.district_code]
} else if (value.province_code && value.city_code && !value.district_code) {
pureCodeByType = [value.province_code, value.city_code, value.city_code + '100' ]
} else if (value.province_code && !value.city_code && !value.district_code) {
pureCodeByType = [value.province_code, value.province_code + "100"]
}
}
return {
pureCodeByType
}
}