function formatJson(filterVal, jsonData) {
  return jsonData.map(v => filterVal.map(j => {
    if (j.indexOf('.') > 0) {
      var keys = j.split('.')
      var temp = v

      for (var a = 0; a < keys.length; a++) {
        if (temp) {
          if (typeof temp[keys[a]] == 'object') {
            temp = temp[keys[a]]
          } else {
            return temp[keys[a]]
          }
        }
      }
    } else {
      return v[j]
    }
  }))
}

function bTokmg(limit) {
  var size = "";
  if (limit < 0.1 * 1024) { //如果小于0.1KB转化成B
    size = limit.toFixed(2) + "B";
  } else if (limit < 0.1 * 1024 * 1024) { //如果小于0.1MB转化成KB
    size = (limit / 1024).toFixed(2) + "KB";
  } else if (limit < 0.1 * 1024 * 1024 * 1024) { //如果小于0.1GB转化成MB
    size = (limit / (1024 * 1024)).toFixed(2) + "MB";
  } else { //其他转化成GB
    size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
  }

  var sizestr = size + "";
  var len = sizestr.indexOf("\.");
  var dec = sizestr.substr(len + 1, 2);
  if (dec == "00") { //当小数点后为00时 去掉小数部分
    return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
  }
  return sizestr;
}


function isImage(ext) {
  return ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff', 'jfif'].indexOf(ext.toLowerCase()) != -1;
}

function isOffice(ext) {
  return ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'].indexOf(ext.toLowerCase()) != -1;
}

function getQR(type,code,data) {
  var obj ={
    type:type,
    code:code,
    data:data
  }
  return JSON.stringify(obj);
}


function getCurrentDateTime() {
    const now = new Date();
    const year = now.getFullYear();
    const month = (now.getMonth() + 1).toString().padStart(2, '0');
    const day = now.getDate().toString().padStart(2, '0');
    const hours = now.getHours().toString().padStart(2, '0');
    const minutes = now.getMinutes().toString().padStart(2, '0');
    const seconds = now.getSeconds().toString().padStart(2, '0');
	 const milliseconds = now.getMilliseconds().toString().padStart(3, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds} ${milliseconds}`;
}


export default {
  formatJson,
  bTokmg,
  isImage,
  isOffice,
  getQR,
  getCurrentDateTime
};
