
function myCache(key, value, seconds = 3600 * 240) {// 默认是24小时
	let nowTime = Date.parse(new Date()) / 1000;
	if (key && value) {
		// 设置缓存
		let expire = nowTime + Number(seconds);
		uni.setStorageSync(key,JSON.stringify(value) + '§' +expire)
	} 
	else if (key && !value) {
		
		if(value==undefined){
			// 获取缓存
			let val = uni.getStorageSync(key);
			if (val) {
				// 缓存存在，判断是否过期
				let temp = val.split('§');
				if (!temp[1] || temp[1] <= nowTime) {
					uni.removeStorageSync(key)
					return '';
				} else {
					return JSON.parse(temp[0]);
				}
			}
			else{
				return '';
			}
		}
		else{
			// 清除缓存
			uni.removeStorageSync(key)
		}
	}
	else{
		console.log('myCache设置缓存失败');
	}
}
function getRemoteFile(path) {
	return path.replace('\r\n','');
}
function getRemoteFile0(path) {
	if (path.includes("/profile/upload/")) {
		return `https://www.sanduolantoyoga.com/yoga${path}`;
	}
	else{
		return `https://www.sanduolantoyoga.com/yoga/profile/upload${path}`;
	}
}
function getRemoteHtmlFile(str) {
	// const reg = new RegExp('"/profile/upload/', 'g'); //g,表示全部替换。
	// str = str.replace(reg, '"https://www.sanduolantoyoga.com/profile/upload/');
	// const reg1 = new RegExp('\'/profile/upload/', 'g'); //g,表示全部替换。
	// str = str.replace(reg1, '\'https://www.sanduolantoyoga.com/profile/upload/');
	return str;
}
function getRemoteHtmlFile0(str) {
	// https://xcfzk.com/
	const reg = new RegExp('"/profile/upload/', 'g'); //g,表示全部替换。
	str = str.replace(reg, '"https://www.sanduolantoyoga.com/profile/upload/');
	const reg1 = new RegExp('\'/profile/upload/', 'g'); //g,表示全部替换。
	str = str.replace(reg1, '\'https://www.sanduolantoyoga.com/profile/upload/');
	return str;
}
function getNowDate() {
	let timeOne = new Date()
	let year = timeOne.getFullYear()
	let month = timeOne.getMonth() + 1
	let day = timeOne.getDate()
	month = month < 10 ? '0' + month : month
	day = day < 10 ? '0' + day : day
	return `${year}-${month}-${day}`;
}
function getMondate(months) {
	let timeOne = new Date()
	timeOne.setMonth(timeOne.getMonth() + months);
	let year = timeOne.getFullYear()
	let month = timeOne.getMonth() + 1
	let day = timeOne.getDate()
	month = month < 10 ? '0' + month : month
	day = day < 10 ? '0' + day : day
	return `${year}-${month}-${day}`;
}
function ifnonet() {
	var ret = false;
	uni.getNetworkType({
		success: function(res) {
			if (res.networkType === 'none') {
				ret = true;
				return ret;
			} else {
				ret = false;
				return ret;
			}
		}
	});
	return ret;
};
function getcartNum() {
	var nowcart=myCache("carts");
	// 加入购物车
	var carts=nowcart?nowcart:[];
	var num=0;
	for(let i=0;i<carts.length;i++){
		var cartList =carts[i].cartList;
		for(let j=0;j<cartList.length;j++){
			if(cartList[j]["status"]==1){
				num=num+1*cartList[j].quantity;
			}
		}
	}
	return num
};
function getcartLoseNum() {
	var nowcart=myCache("carts");
	// 加入购物车
	var carts=nowcart?nowcart:[];
	var num=0;
	for(let i=0;i<carts.length;i++){
		var cartList =carts[i].cartList;
		for(let j=0;j<cartList.length;j++){
			if(cartList[j]["status"]==0){
				num=num+1*cartList[j].quantity;
			}
		}
	}
	return num
};
// function getmessageNum() {
// 	var user=myCache("user"); 
// 	var userid = user.userid? user.userid:'';
// 	var num=0;
// 	if(userid){
// 		var grouplist=myCache("chatlist-"+userid)?myCache("chatlist-"+userid):[];
// 		for(let i=0;i<grouplist.length;i++){
// 			num=num+1*(grouplist[i].sl?grouplist[i].sl:0);
// 		}
// 		return num
// 	}
// 	else{
// 		return num
// 	}
// };
function getmessageNum() { 
	var num= myCache("massageCount")?myCache("massageCount"):0;
	return num;
};
export {
	myCache,
	getRemoteFile,
	getRemoteHtmlFile,
	getNowDate,
	getMondate,
	ifnonet,
	getcartNum,
	getcartLoseNum,
	getmessageNum
};

