export default {
  /**
   * fun 需要防抖的方法
   * delay 最小抖动间隔
   * 用法：
   * var throttleFunction = throttle(function() {
   * 		// 需要防抖的方法体
   * }, 2000)
   *
   * 外部直接调用这个包装过的方法
   * throttleFunction()
   *
   */
  throttle(fun, delay) {
    let last, deferTimer;
    return function (args) {
      let that = this;
      let _args = arguments;
      let now = +new Date().getTime();
      if (last && now < last + delay) {
        clearTimeout(deferTimer);
        deferTimer = setTimeout(function () {
          last = now;
          fun.apply(that, _args);
        }, delay);
      } else {
        last = now;
        fun.apply(that, _args);
      }
    };
  },
  /**
   * 日期时间格式转换
   * @param {String || Number} dateStr  "1995-12-17T03:24:00" || 1605592704154
   * @param {String} format  "yyyy-MM-dd hh:mm:ss"
   * @returns "2020-05-29 12:32:12"
   */
  getFormatDate(dateStr = null, format = "yyyy-MM-dd hh:mm:ss") {
    const date = new Date(
      typeof dateStr === "number" ? dateStr : Number(dateStr)
    );
    const 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)) {
      const m = RegExp.$1; // yyyy
      format = format.replace(
        //匹配年份
        m,
        (date.getFullYear() + "").substr(4 - m.length) //从末尾截取
        /* 1. substr(start, num) 从start 开始截取num 位
         * 2. substring(start, end) 从start 开始截取到end  start 取得到，end 取不到
         */
      );
    }
    for (let k in o) {
      if (new RegExp(`(${k})`).test(format)) {
        // console.log(RegExp.$1); MM dd hh mm ss
        format = format.replace(
          //匹配月、日、时分秒
          RegExp.$1,
          RegExp.$1.length == 1
            ? o[k]
            : ("00" + o[k]).substr(("" + o[k]).length)
          // 拼接 '00' 的目的是如果当前月份、天数、小时...是小于10的话, 拼接成 '01'的形式
          // ('' + o[k]).padStart(2, '0') 也可实现
        );
      }
    }

    return format;
  },

  // 将Thu Sep 20 2018 16:23:03 GMT+0800 (中国标准时间)转换为"2018-09-20 16:23:03"
  formatDateTime(inputTime) {
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? "0" + m : m;
    var d = date.getDate();
    d = d < 10 ? "0" + d : d;
    var h = date.getHours();
    h = h < 10 ? "0" + h : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;
    return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
  },

  eosFormatTime2(oldTimes1, disparityTime) {
    // let time1 = oldTimes1.split(" ");
    // let time2 = time1[1].split(":");
    // let time3 = time1[0].split("-");
    // if (disparityTime % 1 === 0) {
    //   return `${time3[1]}-${time3[2]} ${time2[0] - disparityTime}:00`;
    // } else {
    //   return `${time3[1]}-${time3[2]} ${time2[0] -
    //     Math.ceil(disparityTime)}:30`;
    // }
    var date = new Date(oldTimes1)
    let newDate = date-disparityTime*60*60*1000
    let showDate = this.getFormatDate(newDate,'MM-dd hh:mm:ss')
    return showDate
  },
  timeago(hisTime) {
    var diffValue = new Date().getTime() - hisTime,
      result = "",
      minute = 1000 * 60,
      hour = minute * 60,
      day = hour * 24,
      halfamonth = day * 15,
      month = day * 30,
      year = month * 12,
      _year = diffValue / year,
      _month = diffValue / month,
      _week = diffValue / (7 * day),
      _day = diffValue / day,
      _hour = diffValue / hour,
      _min = diffValue / minute;

    if (_year >= 1) result = parseInt(_year) + "年";
    else if (_month >= 1) result = parseInt(_month) + "个月";
    else if (_week >= 1) result = parseInt(_week) + "周";
    else if (_day >= 1) result = parseInt(_day) + "天";
    else if (_hour >= 1) result = parseInt(_hour) + "个小时";
    else if (_min >= 1) result = "" + parseInt(_min) + "分钟";
    else result = "1分钟";
    return result;
  }
};
