longyvfengyun
2023-12-25 d8d792a6842832e8f6af6604274c438b25053afe
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import extend from './extend'
//省-市-区县联动
function LinkagePlus() {
    this.data= [];
    this.provinces = [];
}
 
// 设置数据
LinkagePlus.prototype.setData = function(data) {
    this.data = data;
    this._setProvinces(data);
};
 
// 设置所有的省
LinkagePlus.prototype._setProvinces = function(data) {
    var result = [];
    Object.keys(data).forEach(function(key){
        var temp = {};
        temp.key = key;
        temp.name = data[key].name;
        result.push(temp);
    });
    this.provinces =  result;
};
// 获取指定省的编号
LinkagePlus.prototype._getProvinceKey = function(province) {
    var result = {
        code: 0,
        province: province,
        key: 0,
        msg: '未查询到"'+province+'"的编号'
    };
    var provinces = this.provinces;
    for(var i=0; i<provinces.length; i++) {
        var _province = provinces[i];
        if(_province.name == province) {
            result.code=1;
            result.key = _province.key;
            result.msg = '获取"'+province+'"的编号成功';
        }
    }
 
    return result;
};
// 获取指定市的编号
LinkagePlus.prototype._getCityKey = function(province, city) {
    var result = {
        code: 0,
        province: province,
        provinceKey: 0,
        city: city,
        key: 0,
        msg: '未查询到"'+province+'-'+city+'"的编号'
    };
    var provinceKey = this._getProvinceKey(province);
    result.provinceKey = provinceKey.key;
    if(provinceKey.code == 1) {
        var cities = this.data[provinceKey.key].child;
        Object.keys(cities).forEach(function(key) {
            var _city = cities[key];
            if(_city.name == city || _city.name=='市辖区') {
                result.code = 1;
                result.key = key;
                result.city = _city.name;
                result.msg = '获取"'+province+'-'+_city.name+'"的编号成功';
            }
        });
    }else {
        result = extend({}, result, provinceKey);
    }
 
    return result;
};
 
// 获取省列表
LinkagePlus.prototype.getProvince = function() {
    return this.provinces;
};
// 获取市列表
LinkagePlus.prototype.getCity = function(province) {
    var result = {
        code: 0,
        province: province,
        data: [],
        msg:'未获取到"'+province+'"的市'
    };
    var provinceKey = this._getProvinceKey(province);
    if(provinceKey.code == 1) {
        var cities = this.data[provinceKey.key].child;
        result.code = 1;
        result.data = this._formatCity(cities);
        result.msg = '获取"'+province+'"的市成功';
    }
    return result;
};
// 格式化获取到的数据
LinkagePlus.prototype._formatCity = function(data) {
    var result = [];
    Object.keys(data).forEach(function(key){
        var temp = {};
        temp.key = key;
        temp.name = data[key].name;
        result.push(temp);
    });
    return result;
};
// 获取市区县
LinkagePlus.prototype.getCounty = function(province, city) {
    var result = {
        code: 0,
        province: province,
        city: city,
        data: [],
        msg:'未获取到"'+province+'-'+city+'"的区县'
    };
    var cityKey = this._getCityKey(province, city);
    if(cityKey.code == 1) {
        var cities = this.data[cityKey.provinceKey].child;
        var counties = cities[cityKey.key].child;
        result.code = 1;
        result.data = this._formatCounty(counties);
        result.city = cityKey.city;
        result.msg = '获取"'+province+'-'+cityKey.city+'"的区县成功';
    }else {
        result = extend({}, result, cityKey);
    }
    return result;
};
// 格式化获取到的数据
LinkagePlus.prototype._formatCounty = function(data) {
    var result = [];
    Object.keys(data).forEach(function(key){
        if(data[key] != '市辖区') {
            var temp = {};
            temp.key = key;
            temp.name = data[key];
            if(data[key])
            result.push(temp);
        }
        
    });
    return result;
};
 
export default LinkagePlus;