whychdw
2019-12-09 064c6d5b84fd6ddbbe4c20c41d139f7371460985
src/libs/common.js
@@ -447,6 +447,147 @@
   return result;
}
//省-市-区县联动
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 {
    CORS,           //  跨域请求内容
    Interval,       // 计时器
@@ -461,4 +602,5 @@
    formatTime1,    // 续航时长
    GetMonomerCap,  // 剩余容量
    GetHourRate,    // 放电小时率
    LinkagePlus,    // 省-市-区县
}