common.js 3.74 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中"?"符后的字串,哈希值之前,取token用
    var theRequest = new Object();
    // console.log(location.href)
    // console.log(location.search)
    // debugger
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      // console.log(str)
      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()
  },
  getParamhref() {
    var url = location.href; //获取url中"?"符后的字串
    var theRequest = new Object();
    // console.log(location.href)
    // console.log(url.substr(url.indexOf('?') + 1))
    if (url.indexOf("?") != -1) {
      // var str = url.substr(1);
      var str = url.substr(url.indexOf('?') + 1)
      // console.log(str)
      // debugger
      var strs = str.split("&");
      for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  },
  // 倒计时
  leftTimer(enddate, Interval) {
    if (enddate > new Date()) {
      var leftTime = (enddate) - new Date(); //计算剩余的毫秒数
    } else {
      if (Interval) {
        clearInterval(Interval)
      }
      // console.log(enddate)
      // console.log(new Date())
      var leftTime = 0
    }

    var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
    var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
    var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
    var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
    days = this.checkTime(days);
    hours = this.checkTime(hours);
    minutes = this.checkTime(minutes);
    seconds = this.checkTime(seconds);
    // console.log(hours)
    // console.log(minutes)
    // console.log(seconds)
    if (days > 0) {
      return days + "天"
    } else if (hours >= 0 || minutes >= 0 || seconds >= 0) {
      return hours + ":" + minutes + ":" + seconds;
    }
    // if (days >= 0 || hours >= 0 || minutes >= 0 || seconds >= 0) return days + "天" + hours + ":" + minutes + ":" + seconds;
    // if (days >= 0 || hours >= 0 || minutes >= 0 || seconds >= 0) return "<span>" + days + "</span>天<span>" + hours + "</span>时<span>" + minutes + "</span>分<span>" + seconds + "</span>秒";
    // if (days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0) {
    //   window.clearInterval(_ordertimer);
    //   _ordertimer = null;
    // }
  },
  checkTime(i) { //将0-9的数字前面加上0,例1变为01
    if (i < 10) {
      i = "0" + i;
    }
    return i;
  }
}