vporto.js 7.49 KB
import Vue from "vue";

Vue.prototype.dev_unid = "2cb6e39adc5176b81879f6c22f1d9e1c";
/**
 * @param {unid} 设备id
 */
Vue.prototype.setDevunid = function(devuid) {
  Vue.prototype.dev_unid = devuid;
};
/**
 * tar文件上传设置
 */
Vue.prototype.uploadFile = function(file, type, size) {
  const isType = file.raw.type === type;
  let isSize = true;
  if (size) {
    isSize = file.size / 1024 / 1024 < size;
  }

  if (!isType) {
    this.$message.error(`上传头像文件只能${type}是格式!`);
  }
  if (size && !isSize) {
    this.$message.error(`文件大小不能超过 ${size}MB!`);
  }
  return isType && isSize;
};
Vue.prototype.globalWs = null;

Vue.prototype.getCode = function(codeName, code) {
  var name = window.localStorage.getItem(codeName + "-" + code);
  name = name ? name : "";
  return name;
};
/**
 * @param {src} 图片路径
 * 图片放大
 */
Vue.prototype.zoomImg = function(data) {
  store.commit("setImgsrc", data);
};
Vue.filter("sexfn", function(value) {
  if (!value) return;
  var str = "";
  if (value == 1) str = "男";
  if (value == 2) str = "女";
  return str;
});

Vue.filter("dbnamefn", function(value) {
  let dbData = store.state.initinfo.facedbdata,
    dbname = "";
  dbData.forEach(ele => {
    if (ele.code == value) {
      dbname = ele.name;
    }
  });
  return dbname;
});

Vue.filter("filterDeployType", function(value) {
  let dbData = store.state.alarmtype,
    dbname = "";
  dbData.forEach(ele => {
    if (ele.type == value) {
      dbname = ele.name;
    }
  });
  return dbname;
});

/**
 * @param {provenceid} 省份id
 * @param {cityid} 城市id
 */
Vue.prototype.showCity = function(provenceid, cityid) {
  this.cData = this.$store.state.initinfo.province;
  var province = provenceid;
  var provinceNmae = "";
  var cityName = "";
  var curProvince = "";
  var city = cityid;
  if (province) {
    this.cData.forEach(function(ele) {
      if (ele.code == province) {
        if (ele.cities) curProvince = ele.cities;
        provinceNmae = ele.name;
      }
    }, this);
    if (city) {
      if (curProvince)
        curProvince.list_data.forEach(function(ele) {
          if (ele.code == city) {
            cityName = ele.name;
          }
        });
    }
  } else {
    provinceNmae = "--";
    cityName = "--";
  }
  return provinceNmae + "-" + cityName;
};
/**
 * @param {obj}
 *  格式化时间
 */
Vue.prototype.timeForm = function(obj) {
  var nowY = obj.getFullYear();
  var nowM =
    obj.getMonth() + 1 < 10 ? "0" + (obj.getMonth() + 1) : obj.getMonth() + 1;
  var nowD = obj.getDate() < 10 ? "0" + obj.getDate() : obj.getDate();
  var nowH = obj.getHours() < 10 ? "0" + obj.getHours() : obj.getHours();
  var nowMi = obj.getMinutes() < 10 ? "0" + obj.getMinutes() : obj.getMinutes();
  var nowS = obj.getSeconds() < 10 ? "0" + obj.getSeconds() : obj.getSeconds();
  var timeNow =
    nowY + "-" + nowM + "-" + nowD + " " + nowH + ":" + nowMi + ":" + nowS;
  return timeNow;
};
Vue.prototype.yesterDay = function() {
  var data = new Date().getTime() - 24 * 60 * 60 * 1000;
  var curdate = this.timeForm(new Date(data));
  return curdate.split(" ")[0] + " " + "00:00:00";
};
Vue.prototype.nowDay = function(){
  var data = new Date();
  return this.timeForm(data);
};
/**
 * @param {obj}
 * UTC时间处理
 */
Vue.prototype.setUtcTime = function(obj) {
  if (obj && obj != "") {
    let num = new Date(obj).getTime();
    let offset = Math.abs(new Date().getTimezoneOffset());
    let time;
    if (isNaN(num)) {
      let ary = obj.split(":");
      let h = parseInt(ary[0]) - offset / 60;
      if (h < 0) {
        h += 24;
      }
      if (h < 10) {
        h = "0" + h;
      }
      time = h + ":" + ary[1] + ":" + ary[2];
    } else {
      time = this.timeForm(new Date(new Date(obj).getTime() - offset * 60000));
    }
    return time;
  }
};
/**IE
 * 检查浏览器
 */
function checkIE() {
  var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  var isIE =
    userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
  var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
  var isIE11 =
    userAgent.indexOf("Trident") > -1 && userAgent.indexOf("rv:11.0") > -1;
  if (isIE || isEdge || isIE11) {
    return true;
  } else {
    return false;
  }
}
let versioninfo = require("../../../public/js/version");
let sha1 = require('js-sha1')
Vue.prototype.baseencode = function(str){
  // return str
  let base = "";
  // if(versioninfo && versioninfo.version ){
  //   let vsstring = versioninfo.version.split(".");
  //   let vs =  Number(String(vsstring[0])+String(vsstring[1])+String(vsstring[2]))
  //   if(vs > 207) {
  //     base = str;
  //   } else {
  //     var encode = encodeURI(str);
  //     // 对编码的字符串转化base64
  //     var base64 = btoa(encode);
  //     base = base64;
  //   }
  // } else {
  //   base = str;
  // }
  // return str
  return sha1(str);
}
Vue.prototype.showLocalTime = function(obj) {
  if (obj == undefined) return;
  let dt = "",
    ndt = "";
  if (checkIE()) {
    dt = obj.replace(/-/g, "/");
    ndt = String(dt).split(".")[0];
  } else {
    ndt = obj;
  }
  let num = new Date(ndt).getTime();
  let offset = Math.abs(new Date().getTimezoneOffset());
  let time;
  if (isNaN(num)) {
    let ary = obj.split(":");
    let h = Number(ary[0]) + offset / 60;
    if (h >= 24) {
      h -= 24;
    }
    if (h < 10) {
      h = "0" + h;
    }
    time = h + ":" + ary[1] + ":" + ary[2];
  } else {
    time = this.timeForm(new Date(new Date(ndt).getTime() + offset * 60000));
  }
  return time;
};

/**
 * 本地存储相机信息
 */
Vue.prototype.setCameralocalStor = function(key, val) {
  if (window.localStorage) {
    localStorage[key] = val;
  } else {
    alert("浏览器不支持本地存储");
  }
};

/**
 * @param {key}
 * 获取本地存储相机信息
 */
Vue.prototype.getCameralocalStor = function(key) {
  var val = "";
  if (window.localStorage) {
    val = localStorage[key];
  } else {
    alert("浏览器不支持本地存储");
  }
  return val;
};

/**
 * 图片上传限制
 */
Vue.prototype.uploadImgCondition = function(file) {
  const isJPG = file.type === "image/jpeg";
  const isLt2M = file.size / 1024 / 1024 < 2;

  if (!isJPG) {
    this.$message.error("上传头像图片只能是 JPG 格式!");
  }
  if (!isLt2M) {
    this.$message.error("上传头像图片大小不能超过 2MB!");
  }
  return isJPG && isLt2M;
};

/**
 * base64图片
 */
Vue.prototype.base64Img = function(file) {
  // eslint-disable-next-line no-redeclare
  var file = file.file;
  return new Promise((resolve, reject) => {
    var srcs = "";
    var reader = new FileReader();
    if (file) {
      reader.readAsDataURL(file);
    }
    reader.onloadend = function(evt) {
      srcs = reader.result.split(",")[1];
      let data = {
        pic_base64: srcs,
        format: file.type.split("/")[1],
        ofilename: file.name,
        t_format: "jpg"
      };
      data.src = evt.target.result;
      resolve(data);
    };
  });
};

/**
 * code操作
 */

var codeNation = 1004; //民族
var codeCulture = 1005; //文化程度
var codeBlood = 1006; //血型
var codeRelationTOhead = 1009; //与户主关系
var codePersonType = 1012; //人员类别
var profession = 1013; //职业
var faith = 1020; //宗教信仰
var army = 1021; //服兵役情况
var personType = 1067; //关注人员类型
var marriage = 1007; //婚姻状况
// 获取code编码
Vue.prototype.getCode = (codeName, code) => {
  var name = localStorage.getItem(codeName + "-" + code);
  name = name ? name : "";
  return name;
};