// const BASE_IMG_URL = 'https://file.xyyxt.net'
const BASE_IMG_URL = 'https://file-dev.xyyxt.net'
import router from '@/router/index'//引入路由对象
export default {
	getUrl(){
		return BASE_IMG_URL + '/'
	},
	isLogin() {
		if (uni.getStorageSync('union_id')) {
			return true;
		} else {
			return false
		}
	},
	splitImgHost(url) {
		if (!url) {
			return ''
		}
		if (url.indexOf("http") != -1 || url.indexOf("https") != -1 || url.indexOf("base64") != -1) {
			return url;
		}
		return BASE_IMG_URL + '/' + url
	},
	onlyForma(timeStamp, Diszing = true) {
		var date = new Date(timeStamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		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();
		if (Diszing) {
			return Y + M + D + ' ' + h + m + s; //时分秒可以根据自己的需求加上
		}
		return Y + M + D;
	},
	computedAge(birthday) {
		let birthDayTime = new Date(birthday).getTime();
		//当前时间 毫秒
		let nowTime = new Date().getTime();
		//一年毫秒数(365 * 86400000 = 31536000000)
		return Math.ceil((nowTime - birthDayTime) / 31536000000);
	},
	time10to13(time, int) {
		if (time === null) {
			return null
		}
		// int = 1 为13转10
		// int = 2 为10转13
		if (int === 1) {
			return parseInt(time / 1000)
		} else {
			return parseInt(time * 1000)
		}
	},

	//计算两个时间之间的时间差 多少天时分秒
	intervalTime(startTime, endTime) {
		// var timestamp=new Date().getTime(); //计算当前时间戳
		var timestamp = (Date.parse(new Date())) / 1000;//计算当前时间戳 (毫秒级)
		var date1 = ""; //开始时间
		if (timestamp < startTime) {
			date1 = startTime;
		} else {
			date1 = timestamp; //开始时间
		}
		var date2 = endTime; //结束时间
		// var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
		var date3 = (date2 - date1) * 1000; //时间差的毫秒数
		//计算出相差天数
		var days = Math.floor(date3 / (24 * 3600 * 1000));
		//计算出小时数

		var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
		var hours = Math.floor(leave1 / (3600 * 1000));
		//计算相差分钟数
		var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
		var minutes = Math.floor(leave2 / (60 * 1000));

		//计算相差秒数

		var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
		var seconds = Math.round(leave3 / 1000);
		console.log(days + "天 " + hours + "小时 ")
		// return   days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒"
		return days + "天 " + hours + "小时 "
	},

	getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) { return pair[1]; }
		}
		return (false);
	},
	setToken(tokenInfo, int) {
		var newTime = new Date().getTime()
		var ESTime = new Date().getTime() + 1000 * 60 * 60 * 24
		var SEVENTime = new Date().getTime() + 1000 * 60 * 60 * 24 * 7
		if (int === 1) {
			localStorage['Expiration' + this.getQueryVariable('TenantId')] = ESTime
		}
		if (int === 7) {
			localStorage['Expiration' + this.getQueryVariable('TenantId')] = SEVENTime
		}
		localStorage['token' + this.getQueryVariable('TenantId')] = tokenInfo
	},
	checkToken() {
		var newTime = new Date().getTime()
		var Time = localStorage['Expiration' + this.getQueryVariable('TenantId')]
		if (newTime > Time) {
			localStorage.removeItem('Expiration' + this.getQueryVariable('TenantId'))
			localStorage.removeItem('token' + this.getQueryVariable('TenantId'))
			setTimeout(() => {
				router.replace({
					path: `/login?TenantId=${this.getQueryVariable('TenantId')}`
				})
			}, 500)
			return
		} else {
			return localStorage.getItem('token' + this.getQueryVariable('TenantId'))
		}
	},
	delToken(status) {
		localStorage.removeItem('Expiration' + this.getQueryVariable('TenantId'))
		localStorage.removeItem('token' + this.getQueryVariable('TenantId'))
		if(status){
			setTimeout(() => {
				router.replace({
					path: `/login?TenantId=${this.getQueryVariable('TenantId')}`
				})
			}, 500)
		}
	}
}
