export default{
  getCurrentPageUrl(){//---获取当前页面url
    let pages = getCurrentPages() //获取加载的页面
    let currentPage = pages[pages.length-1] //获取当前页面的对象
    let url = currentPage.route //当前页面url
    let options = currentPage.options //如果要获取url中所带的参数可
    let queryTotal=''
    for (let key in options){
			 queryTotal+='&'+key+'='+options[key]
    }
    queryTotal=queryTotal.substr(1)
    let pageUrl=url+'?'+queryTotal//请求的全路径
    return pageUrl
  },
  today:function(){//默认当天
    var now = new Date()
    var day = ('0' + now.getDate()).slice(-2)
    var month = ('0' + (now.getMonth() + 1)).slice(-2)
    var today = now.getFullYear()+'-'+(month)+'-'+(day) 
    return today
  },
  tomorrow:function(){//明天
    var tomorrow = new Date()
    tomorrow.setTime(tomorrow.getTime() + 24*60*60*1000)
    var day = ('0' + tomorrow.getDate()).slice(-2)
    var month = ('0' + (tomorrow.getMonth() + 1)).slice(-2)
    var tomorrowStr = tomorrow.getFullYear()+'-'+month+'-'+day
    return tomorrowStr
  },
  afterTomorrow:function(){//后天
    var afterTomorrow = new Date()
    afterTomorrow.setTime(afterTomorrow.getTime() + 2*24*60*60*1000)
    var day = ('0' + afterTomorrow.getDate()).slice(-2)
    var month = ('0' + (afterTomorrow.getMonth() + 1)).slice(-2)
    var tomorrowStr = afterTomorrow.getFullYear()+'-'+month+'-'+day
    return tomorrowStr
  },
  todayTime:function(){
    var now = new Date()
    var hour = now.getHours()<10?'0'+now.getHours():now.getHours()
    var minutes = now.getMinutes()<10?'0'+now.getMinutes():now.getMinutes()
    var seconds = now.getSeconds()<10?'0'+now.getSeconds():now.getSeconds()
    var time = hour+':'+minutes+':'+seconds
    return time
  },
  dateDifference:function(start,end){//结束日期与开始日期相差几天
    var strSeparator = '-'
	    var oDate1= start.split(strSeparator)
	    var oDate2= end.split(strSeparator)
	    var strDateS = new Date(oDate1[0], oDate1[1]-1, oDate1[2])
	    var strDateE = new Date(oDate2[0], oDate2[1]-1, oDate2[2])
	    var iDays = parseInt((strDateE- strDateS) / 1000 / 60 / 60 /24)
	    return iDays
  },
  changeTime:function(option) {//把时间格式转化成数字
		 // currentTime当前时间，假设是 2019-7-2 19:03
		 var currentTime = option
		 var reg = new RegExp('-','g')//去掉时间里面的-
		 var a = currentTime.replace(reg,'')
		 var regs = new RegExp(' ','g')//去掉时间里面的空格
		 var b = a.replace(regs,'')
		 var regss = new RegExp(':','g')//去掉时间里面的:冒号
		 var c = b.replace(regss,'')
		 return c
  },
  getDistance:function(lat1,lng1,lat2,lng2){//计算距离
    var EARTH_RADIUS = 6378137.0 //单位M
    var PI = Math.PI
    function getRad(d){
      return d*PI/180.0
    }
    var f = getRad((lat1 + lat2)/2)
    var g = getRad((lat1 - lat2)/2)
    var l = getRad((lng1 - lng2)/2)
		
    var sg = Math.sin(g)
    var sl = Math.sin(l)
    var sf = Math.sin(f)
		
    var s,c,w,r,d,h1,h2
    var a = EARTH_RADIUS
    var fl = 1/298.257
		
    sg = sg*sg
    sl = sl*sl
    sf = sf*sf
		
    s = sg*(1-sl) + (1-sf)*sl
    c = (1-sg)*(1-sl) + sf*sl
		
    w = Math.atan(Math.sqrt(s/c))
    r = Math.sqrt(s*c)/w
    d = 2*w*a
    h1 = (3*r -1)/2/c
    h2 = (3*r +1)/2/s
    return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg))
  },
  phoneReg:function(){//手机正则
    return /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9]|19[0-9]|16[0-9]|12[0-9])\d{8}$/
  },
  idReg:function(){//身份证正则
	   return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$/
  },
  nameReg:function(){//中文名字正则
    return /^[\u4E00-\u9FA5]{2,40}$/
  },
  getUrlKey:function(name,url){//通过连接地址获取参数值
		 return(new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(decodeURIComponent(url)) || [, ''])[1].replace(/\+/g, '%20') || ''
  },
  /* 
		通过链接地址获取companyId，并保持在本地
		页面获取companyI方法 let companyId=this.$commonjs.getCompanyId()
	*/
  getCompanyId:function(option){
    //待按照下面getKey方法进行优化
    let enterUrl=''
    //#ifdef MP-WEIXIN
    enterUrl=option.q
    //#endif
			
    //#ifdef MP-ALIPAY
    enterUrl=uni.getStorageSync('alipayQrCode')
    //#endif
    let companyId=''
    if(enterUrl){//普通二维码扫码进入
		  companyId=this.getUrlKey('companyId',enterUrl)||uni.getStorageSync('companyId')||''
    }else{//小程序页面跳转进入
		  companyId=option.companyId||uni.getStorageSync('companyId')||''
    }
    uni.setStorageSync('companyId',companyId)
    return companyId
  },
  //获取除companyId的其他参数
  getKey:function(option,query){
    let key=''
    // 优先取值小程序页面参数，其次取值普通二维码
    //#ifdef MP-WEIXIN
    key=option[query]||this.getUrlKey(`${query}`,option.q)||''
    //#endif
		
    //#ifdef MP-ALIPAY
    key=option[query]||this.getUrlKey(`${query}`,uni.getStorageSync('alipayQrCode'))||''
    //#endif
    return key
  }
}
//调用方法时--this.$commonjs.today()