const formatTime = (date, fmt) => {
  if (!date) {
    return fmt;
  }
  var o = {
    "M+": date.getMonth() + 1, //月份 
    "d+": date.getDate(), //日 
    "H+": date.getHours(), //小时 
    "m+": date.getMinutes(), //分 
    "s+": date.getSeconds(), //秒 
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
    "S": date.getMilliseconds() //毫秒 
  };

  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//将年月日数组转成时间戳
const argusToTimestamp = arr => {
  var lenth = arr.length;
  if (lenth < 3) {
    for (var i = 0; i < 3 - lenth; i++) {
      arr.push("01")
    }
  }
  return Date.parse(new Date(arr.join("/")))
}

// 时间戳转成年月
function formateym(dates, split = ".") {
  var date = new Date(+dates)
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return date.getFullYear() + split + formatNumber(date.getMonth() + 1)
}

// 时间转成年月
function formateYM(date, status = true) {
  const _date = date.replace(/-/g, '/')
  var time = new Date(date)
  var year = time.getFullYear()
  var month = time.getMonth() + 1
  var day = time.getDate()
  if (status) {
    return `${year}年${month}月`
  } else {
    return `${year}年${month}月${day}`
  }
}
// 时间转成年月
function formatetime(date, str, status = true) {
  const _date = date.replace ? date.replace(/-/g, '/') : date;
  var time = new Date(_date)
  var year = time.getFullYear()
  var month = time.getMonth() + 1
  var day = time.getDate()
  if (status) {
    return `${year}${str}${month}`
  } else {
    return `${year}${str}${month}${str}${day}`
  }
}
function testPhone(data) {
  const phone_zz = /^1[3-9]\d{9}$/;
  return phone_zz.test(data)
}
/**
 *  判断经纬度是否超出中国境内
 */
function isLocationOutOfChina(latitude, longitude) {
  if (longitude < 72.004 || longitude > 137.8347 || latitude < 0.8293 || latitude > 55.8271)
    return true;
  return false;
}

function transformLatWithXY(x, y) {
  var pi = 3.14159265358979324;
  var lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
  lat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  lat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  lat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  return lat;
}

function transformLonWithXY(x, y) {
  var pi = 3.14159265358979324;
  var lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  lon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  lon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  lon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
  return lon;
}

function isContains(point, p1, p2) {
  return (point.latitude >= Math.min(p1.latitude, p2.latitude) && point.latitude <= Math.max(p1.latitude, p2.latitude)) && (point.longitude >= Math.min(p1.longitude, p2.longitude) && point.longitude <= Math.max(p1.longitude, p2.longitude));
}

/** 
 * 将WGS-84(国际标准)转为GCJ-02(火星坐标): 
 */ 
function transformFromWGSToGCJ(latitude, longitude) { 
  var lat = ""; 
  var lon = ""; 
  var ee = 0.00669342162296594323; 
  var a = 6378245.0; 
  var pi = 3.14159265358979324; 
    if (isLocationOutOfChina(latitude, longitude)) { 
  lat = latitude; 
  lon = longitude; 
  } 
  else { 
  var adjustLat = transformLatWithXY(longitude - 105.0, latitude - 35.0); 
  var adjustLon = transformLonWithXY(longitude - 105.0, latitude - 35.0); 
  var radLat = latitude / 180.0 * pi; 
  var magic = Math.sin(radLat); 
  magic = 1 - ee * magic * magic; 
  var sqrtMagic = Math.sqrt(magic); 
  adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi); 
  adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi); 
  latitude = latitude + adjustLat; 
  longitude = longitude + adjustLon; 
  } 
  return { latitude: latitude, longitude: longitude }; 
    }
module.exports = {
  formatTime: formatTime,
  formatNumber,
  formateym,
  argusToTimestamp,
  formateYM,
  formatetime,
  testPhone,
  transformFromWGSToGCJ
}