common.js 1.99 KB
export default {
  dateFmt: function (date, format) {
    var o = {
      "M+": date.getMonth() + 1, //month
      "d+": date.getDate(),    //day
      "h+": date.getHours(),   //hour
      "m+": date.getMinutes(), //minute
      "s+": date.getSeconds(), //second
      "q+": Math.floor((date.getMonth() + 3) / 3),  //quarter
      "S": date.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
      format = format.replace(RegExp.$1,
        RegExp.$1.length == 1 ? o[k] :
          ("00" + o[k]).substr(("" + o[k]).length));
    return format;
  },
  getUrlParam() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      var strs = str.split("&");
      for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  },
  toDataURL(src, callback) {
    var xhttp = new XMLHttpRequest()
    xhttp.onload = function () {
      var fileReader = new FileReader()
      fileReader.onloadend = function () {
        callback(fileReader.result)
      }
      fileReader.readAsDataURL(xhttp.response)
    };
    xhttp.responseType = 'blob'
    xhttp.open('GET', src, true)
    xhttp.send()
  },
  getParam(paramName) {
    let paramValue = "", isFound = !1;
    let arrSource = []
    let i = 0
    if (location.search.indexOf("?") == 0 && location.search.indexOf("=") > 1) {
      arrSource = unescape(location.search).substring(1, location.search.length).split("&");
      while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
    }
    return paramValue == "" && (paramValue = null), paramValue
  }
}