// import jQuery from 'jquery'
// import axios from "axios";
// import qs from 'qs';
// 时间转换
/**
 * 日期格式化
 * @param date  传入的时间
 * @param fmt   格式化样式
 * @returns {string}  返回格式化之后的时间
 */
// Sun Jan 17 2021 00:00:00 GMT+0800 (中国标准时间) 转化格式
export function formatDate(date, fmt = "yyyy-MM-dd") {
   date=new Date(date)
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  let o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + "";
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      );
    }
  }
  return fmt;
}
function padLeftZero(str) {
  return ("00" + str).substr(str.length);
}
// 身份证号显示*
export function fn(idcard) {
  return idcard.replace(/^(.{4})(?:\d+)(.{4})$/, "$1 ********** $2");
}
//姓名脱敏
export function formatName(str) {
  return str.substr(0, 1) + new Array(str.length).join("*");
}
// base64  to blob二进制
export function dataURItoBlob(dataURI) {
  var mimeString = dataURI
    .split(",")[0]
    .split(":")[1]
    .split(";")[0]; // mime类型
  var byteString = atob(dataURI.split(",")[1]); //base64 解码
  var arrayBuffer = new ArrayBuffer(byteString.length); //创建缓冲数组
  var intArray = new Uint8Array(arrayBuffer); //创建视图

  for (var i = 0; i < byteString.length; i++) {
    intArray[i] = byteString.charCodeAt(i);
  }
  return new Blob([intArray], { type: mimeString });
}
//获取当前年月日
export function getDay (day, hours) {
  var today = new Date()
  var targetday = today.getTime() + 1000 * 60 * 60 * 24 * day + hours
  today.setTime(targetday)
  var tYear = today.getFullYear()
  var tMonth = today.getMonth()
  var tDate = today.getDate()
  tMonth = doHandleMonth(tMonth + 1)
  tDate = doHandleMonth(tDate)
  return tYear + '-' + tMonth + '-' + tDate 
}
function doHandleMonth(month) {
  var m = month
  if (month.toString().length === 1) {
    m = '0' + month
  }
  return m
}
//时间戳转化
export function timestamp(date) {
  if(date == ''){
    return '';
  }else if(date.toString().length == 10){
    let time = new Date(date * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let y = time.getFullYear();
    let m = time.getMonth() < 9 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
    let d = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
    let h = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
    let mm = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
    let s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
    let timedate = y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s;
    return timedate;
  }else{
     let time = new Date(date * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let y = time.getFullYear();
    let m = time.getMonth() < 9 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
    let d = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
    let h = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
    let mm = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
    let s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
    let timedate = y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s;
    return timedate;
  }
}
//获取当前时分秒
export function time() {
  var myDate = new Date();
  var shijian = myDate.toLocaleTimeString();

  var timing ;
  if (window.navigator.userAgent.indexOf("MSIE")>=1) {
    timing = shijian
  }else{
    var shijian1 = shijian.substring(0, 2);
    timing = shijian.substring(2, shijian.length);
    var timelist = timing.split(':');
    if (timelist[0] < 10) {
      timelist[0] = timelist[0].replace('', '0');
      timing = timelist.toString().replace(/,/g, ':')
    }
    if (shijian1 == '下午') {
      if (timelist[0] != 12) {
        timelist[0] = Number(timelist[0]) + 12;
        timing = timelist.toString().replace(/,/g, ':')
      }
    }
  }
  return shijian;

}
//根据身份证号获取年龄
export function queryage(idc) {//得到年龄
  var brthday=idc.substring(6,10)+"-"+idc.substring(10,12)+"-"+idc.substring(12,14);
  let age=getAge(brthday);
  return age
  }
  // 判断用户的年龄
  function getAge(birthday){
  let birthdays = new Date(birthday.replace(/-/g, '/'));
  let d = new Date();
  let age =
  d.getFullYear() -
  birthdays.getFullYear() -
  (d.getMonth() < birthdays.getMonth() ||
  (d.getMonth() == birthdays.getMonth() &&
  d.getDate() < birthdays.getDate())? 1: 0);
  return age;
}