function getRouter() {
	let pages = getCurrentPages(); //获取加载的页面
	let currentPage = pages[pages.length - 1]; //获取当前页面的对象
	let url = currentPage.route; //当前页面url
	uni.setStorageSync('url', `${url}`)
	let options = currentPage.options;
	console.log("url: " + url);
	console.log("options: " + options);
	if (options) {
		let urlParams = url + '?'
		for (let key in options) {
			let value = options[key]
			urlParams += key + '=' + value + '&'
		}
		urlParams = urlParams.substring(0, urlParams.length - 1); //去掉末尾的&
		uni.setStorageSync('url', `${urlParams}`)
	}
}
//解析链接
function getQueryString(url, name) {
	// console.log("url = " + url);
	// console.log("name = " + name);
	var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i');
	var r = url.substr(1).match(reg);
	if (r != null) {
		// console.log("r = " + r)
		// console.log("r[2] = " + r[2])
		return r[2];
	}
	return null;
}

// 
function money(value) {
	if (typeof value !== 'string') {
		value = String(value)
	}
	if (!value) return '0.0'
	// const intPart = Number(value).toFixed(0) // 获取整数部分
	const intPart = value.split('.')[0]
	const intPartFormat = intPart
		// .toString()
		.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
	let floatPart = '.0' // 预定义小数部分
	const value2Array = value.split('.')
	// =2表示数据有小数位
	if (value2Array.length === 2) {
		floatPart = value2Array[1].toString() // 拿到小数部分
		if (floatPart.length === 1) {
			// 补0,
			return intPartFormat + '.' + floatPart
		} else {
			return intPartFormat + '.' + floatPart[0]
		}
	} else {
		return intPartFormat + floatPart
	}
}

function fansview(num) {
	num = parseInt(num)

	if (num >= 100000000) {
		var t1 = num / 100000000
		var t2 = num % 100000000		
		if (t2 !== 0) {
			return String(t1).substring(0, String(t1).indexOf('.') + 2) + '亿'
		} else return String(t1) + '.0亿'
	} else if (num >= 10000) {
		var t1 = num / 10000
		var t2 = num % 10000
		if (t2 !== 0) {
			// return String(t1).match(/^\d+(?:\.\d{1,2})?/) + "万"
			return String(t1).substring(0, String(t1).indexOf('.') + 2) + '万'
		} else return String(t1) + '.0万'
	} else {
		num = String(num)
	}
	return num
}

function getDaysBetweenDates(date1, date2) {
  // 将日期转为时间戳，单位为毫秒
  var timestamp1 = Date.parse(date1);
  var timestamp2 = Date.parse(date2);

  // 计算时间戳之差
  var timeDiff = (timestamp2 - timestamp1);

  // 将时间戳之差转为天数并返回
  var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
  return diffDays;
}

//截至时间精确到小时
function getDaysAndHoursBetweenDates(date1, date2) {
    // 将两个日期转换为毫秒数
    var time1 = date1.getTime();
    var time2 = date2.getTime();

    // 计算两个日期之间的毫秒数差
    var timeDiff = time2 - time1;

    // 计算总小时数
    var totalHours = Math.ceil(timeDiff / (1000 * 3600));

    // 计算天数
    var days = Math.floor(totalHours / 24);

    // 计算剩余小时数
    var hours = totalHours % 24;

    // 返回包含天数和小时数的对象
    return {
        days: days,
        hours: hours
    };
}


module.exports = {
	getRouter,
	getQueryString,
	money,
	fansview,
	getDaysBetweenDates,
	getDaysAndHoursBetweenDates,
}
