// const CryptoJS = require("./AES.js").CryptoJS; //引用AES源码js 
const DateFormat = function (date, fmt) { //author: meizz
  var o = {
    "M+": new Date(date).getMonth() + 1, //月份
    "d+": new Date(date).getDate(), //日
    "h+": new Date(date).getHours(), //小时
    "m+": new Date(date).getMinutes(), //分
    "s+": new Date(date).getSeconds(), //秒
    "q+": Math.floor((new Date(date).getMonth() + 3) / 3), //季度
    "S": new Date(date).getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (new Date(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
}
//加密方法
// function encrypt(word) {
//   var keyStr = 'SH426B787D035B7585864EF93VV30BA9'; // 密文（密钥）
//   var key = CryptoJS.enc.Utf8.parse(keyStr);
//   var srcs = CryptoJS.enc.Utf8.parse(word);
//   var encrypted = CryptoJS.AES.encrypt(srcs, key, {
//     mode: CryptoJS.mode.ECB,
//     padding: CryptoJS.pad.Pkcs7,
//   });
//   return encrypted.toString();
// }
//时间
function getTime() {
  var timestamp = Date.parse(new Date());  
    timestamp = timestamp / 1000;  
    //获取当前时间
    var n = timestamp * 1000;  
    var date = new Date(n);  
    //年  
    var Y = date.getFullYear();  
    //月  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);  
    //日  
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();  
    //时  
    var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();  
    //分  
    var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();  
    //秒  
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();  
    return h + ':' + m + ':' + s;
}
function getTime1() {
  var timestamp = Date.parse(new Date());  
    timestamp = timestamp / 1000;  
    //获取当前时间
    var n = timestamp * 1000;  
    var date = new Date(n);  
    //年  
    var Y = date.getFullYear();  
    //月  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);  
    //日  
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();  
    //时  
    var h = date.getHours();  
    //分  
    var m = date.getMinutes();  
    //秒  
    var s = date.getSeconds();  
    return Y + '-' + M + '-' + D;
    
}
function getDistances(lat1, lng1, lat2, lng2) {
  let EARTH_RADIUS = 6378.137;// 地球半径
  let radLat1 = lat1 * Math.PI / 180.0; //lat1 * Math.PI / 180.0=>弧度计算
  let radLat2 = lat2 * Math.PI / 180.0;
  let a = radLat1 - radLat2;
  let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * EARTH_RADIUS; 
  s = Math.round(s * 10000) / 10000;// 输出为公里
  return { m: s * 1000, km: Number(s.toFixed(2)) }
}
function DataCompare(t1,t2){
  let time1 = new Date(Date.parse(t1))
  let time2 = new Date(Date.parse(t2))
  return time1>time2

}
function getTime2() {
  var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;  
    //获取当前时间
    var n = timestamp * 1000;  
    var date = new Date(n);  
    //年  
    var Y = date.getFullYear();  
    //月  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);  
    //日  
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();  
    //时  
    var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();  
    //分  
    var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();  
    //秒  
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();  
    return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s;
}
module.exports = {
  // encrypt,
  getDistances,
  getTime,
  getTime1,
  getTime2,
  DateFormat,
  DataCompare,
};
