// 格式化日期格式为 xxxx-xx-xx 
function getFullDate(targetDate) {

	var nowDate = new Date()
	var fullYear = nowDate.getFullYear();
	var month = nowDate.getMonth() + 1 // getMonth 方法返回 0-11，代表1-12月

	var D, y, m, d
	if (targetDate) {
		D = new Date(targetDate)
		y = D.getFullYear()
		m = D.getMonth() + 1
		d = D.getDate()
	} else { // 不传参数 则获取今天的日期
		y = fullYear
		m = month
		d = new Date()
		d = d.getDate()
	}
	m = m > 9 ? m : '0' + m
	d = d > 9 ? d : '0' + d
	return y + '-' + m + '-' + d
}
/* 将时间戳格式化为yyyy-MM-dd hh:mm:ss格式，其它格式可自行更改 */
function formatTimeMills(timeMills, sign) {
	var date = new Date(timeMills);
	var timeStr = date.getFullYear() + sign;
	if (date.getMonth() < 9) { // 月份从0开始的
		timeStr += '0';
	}
	timeStr += date.getMonth() + 1 + sign;
	timeStr += date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate();
	timeStr += ' ';
	timeStr += date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours();
	timeStr += ':';
	timeStr += date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes();
	timeStr += ':';
	timeStr += date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds();
	return timeStr;
}
// 获取当月的第一天
function getCurrentMonthFirst() {
	let date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = "01";
	month = month < 10 ? "0" + month : month;
	return year + "-" + month + "-" + day;
}

// 获取本月的最后一天
function getCurrentMonthLast() {
	var date = new Date();
	var currentMonth = date.getMonth();
	var nextMonth = ++currentMonth;
	var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
	var oneDay = 1000 * 60 * 60 * 24;
	var lastTime = new Date(nextMonthFirstDay - oneDay);
	var month = parseInt(lastTime.getMonth() + 1);
	var day = lastTime.getDate();
	if (month < 10) {
		month = '0' + month;
	}
	if (day < 10) {
		day = '0' + day;
	}
	return date.getFullYear() + '-' + month + '-' + day;
}
// 获取一年后的今天
function getOneYearLater() {
	var date = new Date();
	var year = date.getFullYear() + 1; // 减一是算去年时间，+ 1 是明年时间
	var month = date.getMonth() + 1;
	var day = date.getDate();
	if (month < 10) {
		month = '0' + month;
	}
	if (day < 10) {
		day = '0' + day;
	}
	var dateStr = year + "-" + month + "-" + day;
	return dateStr;
}

let num = (a) => {
	if (a != null && a.toString() != "") {
		let r = /^-?(0|[1-9]+\d*|[1-9]+\d*\.\d+|0\.\d+)$/;
		if (r.test(a.toString())) {
			return true;
		}
	}
	return false;
}
//除法
function division(a, b) {
	if (!num(a) || !num(b)) {
		return null;
	}
	let c, d, f, g;
	try {
		c = a.toString().split(".")[1].length;
	} catch (e) {
		c = 0;
	}
	try {
		d = b.toString().split(".")[1].length;
	} catch (e) {
		d = 0;
	}
	f = Number(a.toString().replace(".", ""));
	g = Number(b.toString().replace(".", ""));
	return parseFloat(((f / g) * Math.pow(10, d - c)).toFixed(2))
}



// 获取n天后的日期
function getNdaysLater(n) {
	var nowDate = new Date();
	let nDaysLaterDate = this.getFullDate(nowDate.getTime() + 24 * 60 * 60 * 1000 * n); //获取明天日期
	return nDaysLaterDate;
}
// 去除日期中的点或-
function removeDateStr(dateStr, sign, num) {
	let date = '';
	if (num == 0) {
		if (sign == '.') {
			date = dateStr.replace(/\./g, '');
		} else if (sign == '-') {
			date = dateStr.replace(/\-/g, '');
		}
	} else if (num == 1) {
		if (sign == '.') {
			date = dateStr.replace(/\./g, '-');
		} else if (sign == '-') {
			date = dateStr.replace(/\-/g, '.');
		}
	}

	return date;
}
// 金额格式化
function amountFormatting(amount, sign) {
	let changeAmount = 0;
	let resultAmount = 0;
	if (sign == 0) {
		changeAmount = division(amount, 100);
		resultAmount = changeAmount.toFixed(2).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
	} else if (sign == 1) {
		resultAmount = amount.toFixed(0).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
	}
	return resultAmount;
}
// 字符串转日期格式
function stringToDate(Date, sign) {
	let newStr = '';
	let resultStr = '';
	if (sign == '-') {
		newStr = Date.substring(0, 4) + "-" + Date.substring(4);
		resultStr = newStr.substring(0, 7) + "-" + newStr.substring(7);
	} else if (sign == '.') {
		newStr = Date.substring(0, 4) + "." + Date.substring(4);
		resultStr = newStr.substring(0, 7) + "." + newStr.substring(7);
	}
	return resultStr;
}
// 获取今天的零点/最后一刻
function getTodayStartOrEnd(sign) {
	let resultTime = '';
	if (sign == 'start') {
		resultTime = new Date(new Date(new Date().toLocaleDateString()).getTime());
	} else if (sign == 'end') {
		resultTime = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
	}
	return formatTimeMills(resultTime, '-');
}
// 判断门店对象是否为空
let storeObject = null;

function getStore() {
	if (!storeObject) {
		storeObject = uni.getStorageSync('storeObject')
	}
	return storeObject;
}

function setStore(store) {
	storeObject = store;
	uni.setStorageSync('storeObject', storeObject);
}

// 获取今天
function getToday(symbol = '') {
	var taday = new Date()
	var tdyear = taday.getFullYear().toString()
	var tdmonth = taday.getMonth() + 1
	var tdstrDate = taday.getDate()
	if (tdmonth >= 1 && tdmonth <= 9) tdmonth = "0" + tdmonth
	if (tdstrDate >= 0 && tdstrDate <= 9) tdstrDate = "0" + tdstrDate
	return tdyear + symbol + tdmonth.toString() + symbol + tdstrDate
}

// 获取以今天为准的前后多少天的时间
function getOtherToday(num, symbol = '') {
	var taday = new Date()
	var date = new Date(taday.setDate(taday.getDate() + num))
	let year = date.getFullYear().toString()
	let month = (date.getMonth() + 1).toString()
	let day = date.getDate().toString()

	if (month.length < 2) month = '0' + month
	if (day.length < 2) day = '0' + day
	return year + symbol + month + symbol + day
}

// 获取设备号
let deviceNo = null;
function getDeviceNo() {
	// #ifndef H5
	if (!deviceNo) {
		deviceNo = uni.getStorageSync('deviceNo')
	}
	return deviceNo;
	// #endif
	// #ifdef H5
	return '82365aa3f9700ad4'
	// #endif
}
function setDeviceNo(device) {
	deviceNo = device;
	uni.setStorageSync('deviceNo', deviceNo);
}

// 获取baseUrl
let baseUrl = '';
function getBaseUrl() {
	if (!baseUrl) {
		baseUrl = uni.getStorageSync('baseUrl', baseUrl);
	}
	return baseUrl;
}
function setBaseUrl(passBaseUrl) {
	baseUrl = passBaseUrl;
	uni.setStorageSync('baseUrl', baseUrl);
}

exports default {
	getFullDate,
	getCurrentMonthLast,
	getOneYearLater,
	getNdaysLater,
	removeDateStr,
	amountFormatting,
	stringToDate,
	getTodayStartOrEnd,
	getStore,
	setStore,
	getCurrentMonthFirst,
	formatTimeMills,
	getToday,
	getOtherToday,
	getDeviceNo,
	setDeviceNo,
	getBaseUrl,
	setBaseUrl
}
