whychdw
2019-12-09 064c6d5b84fd6ddbbe4c20c41d139f7371460985
src/libs/common.js
@@ -1,6 +1,6 @@
import $ from 'jquery';
const CORS= 'http://localhost:8080/LD_9/';        // 开启跨域请求(调试时开启)
//const CORS='';           // 关闭跨域请求(发布时开启)
// const CORS='';           // 关闭跨域请求(发布时开启)
//定义计时器
function Interval() {
@@ -80,6 +80,25 @@
   var newArray = arr.join(",").split(",");
   return Math.min.apply({},newArray);
}
/*从多维数组中获取和*/
function getSumFromArr (arr) {
   var newArray = arr.join(",").split(",");
    var sum =0;
    for(var i=0; i<newArray.length; i++) {
        var _newArray = newArray[i];
        sum += Number(_newArray);
    }
    return sum;
}
/*从多维数组中获取平均值*/
function getAvgFromArr (arr) {
    var avg = 0;
    if(arr.length !=0) {
        var sum = getSumFromArr(arr);
        avg = sum/arr.length;
    }
    return avg;
}
function ajax(option) {
    $.ajax({
@@ -92,6 +111,58 @@
        error: option.error,
        complete: option.complete
    });
}
//将秒转化成时:分:秒
function formatSeconds(value) {
   if(value > 0){
   }else{
      value = 0;
   }
   var theTime = parseInt(value);// 秒
   var theTime1 = 0;// 分
   var theTime2 = 0;// 小时
   // alert(theTime);
   if(theTime >= 60) {
      theTime1 = parseInt(theTime/60);
      theTime = parseInt(theTime%60);
      //alert(theTime1+"-"+theTime);
      if(theTime1 >= 60) {
         theTime2 = parseInt(theTime1/60);
         theTime1 = parseInt(theTime1%60);
      }
   }
   var result = (theTime<10?"0":"")+parseInt(theTime);
   if(theTime1 >= 0) {
      result =(theTime1<10?"0":"")+parseInt(theTime1)+":"+result;
   }
   if(theTime2 >= 0) {
      result =(theTime2<10?"0":"")+parseInt(theTime2)+":"+result;
   }
   //console.info(result);
   return result;
}
// 设置后备时间(续航时间)
function sethoubeiTime(value){
    value = Math.abs(value);
    var str = "";
    if(value>0){
        var hour = parseInt(value);
        var min = parseInt((parseFloat(value) - hour)*60);
        if(hour<10){
            str = "0";
        }
        str+=hour+"H";
        if(min<10){
            str+="0";
        }
        str+=min+"M";
    }else{
        str = "00H00M";
    }
    return str;
}
// 格式化时间
@@ -115,6 +186,408 @@
    return format;
};
function Title(){
    this.min=0;            //最小值
    this.max=0;            //最大值
    this.avg=0;            //平均值
    this.sum=0;            //总和
    this.ahight=0;         //高告警阀值
    this.alow=0;         //低告警阀值
    this.clow=0;         //次低更换阀值
    this.lc=0;            //低告警单体数
    this.lp=0;            //低告警百分比
}
function createTitle(){
    var obj=new Object();
    obj.min=0;
    obj.max=0;
    obj.avg=0;
    obj.sum=0;
    obj.ahight=0;
    obj.alow=0;
    obj.clow=0;
    obj.lc=0;
    obj.lp=0;
    return obj;
}
Title.prototype.setMin=function(min){
    this.min=min;
};
Title.prototype.getMin=function(){
    return this.min;
};
Title.prototype.setMax=function(max){
    this.max=max;
};
Title.prototype.getMax=function(){
    return this.max;
};
Title.prototype.setAvg=function(avg){
    this.avg=avg;
};
Title.prototype.getAvg=function(){
    return this.avg;
};
Title.prototype.setSum=function(sum){
    this.sum=sum;
};
Title.prototype.getSum=function(){
    return this.sum;
};
Title.prototype.setAhight=function(ahight){
    this.ahight=ahight;
};
Title.prototype.getAhight=function(){
return this.ahight;
};
Title.prototype.setAlow=function(alow){
    this.alow=alow;
};
Title.prototype.getAlow=function(){
    return this.alow;
};
Title.prototype.setClow=function(clow){
    this.clow=clow;
};
Title.prototype.getClow=function(){
    return this.clow;
};
Title.prototype.setLc=function(lc){
    this.lc=lc;
};
Title.prototype.getLc=function(){
    return this.lc;
};
Title.prototype.setLp=function(lp){
    this.lp=lp;
};
Title.prototype.getLp=function(){
    return lp;
};
Title.prototype.getAllTile=function(lname){
    //alert(this.avg);
    var title="";
    var maxText='最大值';         //最大值
    var minText='最小值';         //最小值
    var avgText='平均值';          //平均值
    var lowText='落后值';         //落后值
    var lcText='落后数量';            //落后数量
    var lpText='落后数量比';          //落后数量比
    if("Voltage"==lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(3))+"V;"+minText+"="+(parseFloat(this.min).toFixed(3))+"V;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"V;"+lowText+"="+this.alow+"V;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(3))+"V;"+minText+"="+(parseFloat(this.min).toFixed(3))+"V;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"V;累加和="+(parseFloat(this.sum).toFixed(3))+"V";
    }else if("Resistance"==lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(3))+"mΩ;"+minText+"="+(parseFloat(this.min).toFixed(3))+"mΩ;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"mΩ;"+lowText+"="+this.alow+"mΩ;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(3))+"mΩ;"+minText+"="+(parseFloat(this.min).toFixed(3))+"mΩ;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"mΩ";
    }else if("Temperature"==lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(1))+"℃;"+minText+"="+(parseFloat(this.min).toFixed(1))+"℃;"+avgText+"="+(parseFloat(this.avg).toFixed(1))+"℃;"+lowText+"="+this.alow+"℃;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(1))+"℃;"+minText+"="+(parseFloat(this.min).toFixed(1))+"℃;"+avgText+"="+(parseFloat(this.avg).toFixed(1))+"℃";
    }else if("Conductance"==lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(0))+";"+minText+"="+(parseFloat(this.min).toFixed(0))+";"+avgText+"="+(parseFloat(this.avg).toFixed(0))+";"+lowText+"="+this.alow+";"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(0))+";"+minText+"="+(parseFloat(this.min).toFixed(0))+";"+avgText+"="+(parseFloat(this.avg).toFixed(0));
    }else if("MonJHCurr"==lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(3))+"V;"+minText+"="+(parseFloat(this.min).toFixed(3))+"V;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"V;"+lowText+"="+this.alow+"V;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(3))+"mA;"+minText+"="+(parseFloat(this.min).toFixed(3))+"mA;"+avgText+"="+(parseFloat(this.avg).toFixed(3))+"mA";
    }else if("Serpercent"==lname || "Percent_total_capacity" == lname){
        //title=maxText+"="+this.max+"%;"+minText+"="+this.min+"%;"+avgText+"="+this.avg+"%;"+lowText+"="+this.alow+"%;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+this.max+"%;"+minText+"="+this.min+"%;"+avgText+"="+this.avg+"%";
    }else if("Actual_capacity"==lname || "Residual_capacity" == lname){
        //title=maxText+"="+(parseFloat(this.max).toFixed(0))+"AH;"+minText+"="+(parseFloat(this.min).toFixed(0))+"AH;"+avgText+"="+(parseFloat(this.avg).toFixed(0))+"AH;"+lowText+"="+this.alow+"AH;"+lcText+"="+this.lc+";"+lpText+"="+this.lp+"%";
        title=maxText+"="+(parseFloat(this.max).toFixed(0))+"AH;"+minText+"="+(parseFloat(this.min).toFixed(0))+"AH;"+avgText+"="+(parseFloat(this.avg).toFixed(0))+"AH";
    }
    return title;
};
var  CapType_Rest = 0;         //当查询剩余容量时传递
var  CapType_Real = 1;         //当查询实际容量时传递
var STDAH;                  //标纯容量
var MonomerVolType;            //电池电压类型
// 获取标纯电流
function GetFDCurrent(stdcap,hourrate)
{
   var res = 0.055;
    switch(hourrate)
    {
        case 1: res = 0.514; break;
        case 2: res = 0.306; break;
        case 3: res = 0.250; break;
        case 4: res = 0.200; break;
        case 5: res = 0.166; break;
        case 6: res = 0.146; break;
        case 7: res = 0.131; break;
        case 8: res = 0.118; break;
        case 9: res = 0.108; break;
        case 10: res = 0.100; break;
        case 20: res = 0.055; break;
        default: res = 0.055; break;
    }
    return (stdcap * res);
}
// 获取放电小时率      stdah:标纯容量              current:当前电流
function GetHourRate(stdah,current)
{
   var index = 0;
   var value=[5.14, 3.06, 2.50, 2.00, 1.66, 1.46, 1.31, 1.18, 1.08, 1.00, 0.55, 0.40];
   var res;
    current = Math.abs(current);
   res = current/(stdah/10);
    if(res >= 5.14) return 1;
    else if(res <= 0.55) return 20;
    else
    {
      for(var index=0; index<10; index++)
        {
         if((res<=value[index]) && (res>value[index+1]))
            break;
            else continue;
        }
        if((value[index]-res) < (res-value[index+1]))
        {
         return (index+1);
        }
      else
        {
         if(index+2 > 10) return (20);
         else return (index+2);
        }
    }
}
function N_TO_10H(n_H)
{
    switch(n_H)
    {
        case  1 : return(1/0.55);
        case  2 : return(1/0.61);
        case  3 : return(1/0.75);
        case  4 : return(1/0.79);
        case  5 : return(1/0.833);
        case  6 : return(1/0.876);
        case  7 : return(1/0.917);
        case  8 : return(1/0.944);
        case  9 : return(1/0.974);
        case  10: return(1/1);
        case  20: return(1/1.1);
    }
    return 1.0;
}
//获取剩余容量   STDAH:标称容量         HourRate:放电小时率   SumAH:测试容量      MaxMonomerVol:最大电池电压      MonomerVol:当前电池组的最低单体电压
//MonomerVolType:电池标称电压      CapType:容量类型(定值是常量)
function GetMonomerCap(STDAH, HourRate, SumAH, MaxMonomerVol, MonomerVol, MonomerVolType,CapType)
{
    if((MaxMonomerVol - MonomerVolType*0.9) <= 0)
        return 0;
    if(SumAH < 0)
        SumAH *= (-1);
    var tmp_cap;
    tmp_cap = MonomerVol - MonomerVolType * 0.9;
    tmp_cap *= (STDAH - SumAH * N_TO_10H(HourRate));
    var dt_vol = MaxMonomerVol - MonomerVolType*0.9;
    if(dt_vol < 0.01)
        dt_vol = 0.01;
    tmp_cap = tmp_cap/dt_vol;
    if(tmp_cap < 0)
        tmp_cap = 0;
    if(CapType == CapType_Rest)
        return tmp_cap;
    else if(CapType == CapType_Real)
        return (tmp_cap + SumAH * N_TO_10H(HourRate));
    else
        return ((tmp_cap + SumAH * N_TO_10H(HourRate))*100 / STDAH);
}
//将秒转化成00M00H类型
function formatTime1(value) {
   var theTime = parseInt(value);// 秒
   var theTime1 = 0;// 分
   var theTime2 = 0;// 小时
   if(theTime >= 60) {
      theTime1 = parseInt(theTime/60);
      theTime = parseInt(theTime%60);
      // alert(theTime1+"-"+theTime);
      if(theTime1 >= 60) {
         theTime2 = parseInt(theTime1/60);
         theTime1 = parseInt(theTime1%60);
      }
   }
   var result = "";
   if(theTime1 >= 0) {
      result =(theTime1<10?"0":"")+parseInt(theTime1)+"M"+result;
   }
   if(theTime2 >= 0) {
      result =(theTime2<10?"0":"")+parseInt(theTime2)+"H"+result;
   }
   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,       // 计时器
@@ -122,4 +595,12 @@
    getMaxFromArr,  // 获取数组中的最大值
    getMinFromArr,   // 获取数组中的最小值
    ajax,            // 请求
    formatSeconds,   // 将秒转化成时:分:秒
    Title,
    getSumFromArr,   // 获取累加和
    getAvgFromArr,   // 获取平均值
    formatTime1,    // 续航时长
    GetMonomerCap,  // 剩余容量
    GetHourRate,    // 放电小时率
    LinkagePlus,    // 省-市-区县
}