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;
|