function BeforeNavigateTo(params, needLogin) { //页面跳转勾子 
	if (needLogin) {
		if (uni.getStorageSync('xj-storage_token')) {
			uni.navigateTo({
				animationType: 'pop-in',
				animationDuration: 300,
				...params,
				fail: (e) => {
					plus.nativeUI.toast(e.errMsg);
				}
			})
		} else {
			uni.navigateTo({
				animationType: 'pop-in',
				animationDuration: 300,
				url: '/pages/native/login'
			})

		}
	} else {
		uni.navigateTo({
			animationType: 'pop-in',
			animationDuration: 300,
			...params,
		})
	}
}

function BeforeSwitchTab(params) { //页面跳转勾子 针对tab
	if (true) {
		uni.switchTab({
			animationType: 'pop-in',
			animationDuration: 300,
			...params,
		})
	} else {
		alert('未登录')
	}
}

function utf16toEntities(str = "") {
	var patt = /[\ud800-\udbff][\udc00-\udfff]/g;
	str = str.replace(patt, function(char) {
		var H, L, code;
		if (char.length === 2) {
			H = char.charCodeAt(0);
			L = char.charCodeAt(1);
			code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00;
			return "&#" + code + ";";
		} else {
			return char;
		}
	});
	return str;
}

function format(data,fmt) {
		const o = {
			"y+": data.getFullYear(),
			"M+": data.getMonth()+1,
			"d+": data.getDate(),
			"H+": data.getHours(),
			"m+": data.getMinutes(),
			"s+": data.getSeconds(),
			"S+": data.getMilliseconds(),
			"q+": Math.floor(data.getMonth()/3) + 1,
			"h+": (()=>{
				const hour=data.getHours()%12;
				return hour==0?12:hour;
			})(),
			"E+": (()=>{
				const week = {"0":"Sunday","1":"Monday","2":"Tuesday","3":"Wednesday","4":"Thursday","5":"Friday","6":"Saturday"}; 
				return week[data.getDay()+""];
			})(),
			"x1": (()=>{
				const week = {"0":"周日","1":"周一","2":"周二","3":"周三","4":"周四","5":"周五","6":"周六"}; 
				return week[data.getDay()+""];
			})(),
			"x2": (()=>{
				const hour = ["凌晨","早上","下午","晚上"];
				const h=data.getHours();
				if(h==12) return "中午";
				return hour[parseInt(h/6)];
			})(),
		}
		for(var k in o) {
			if(new RegExp("("+k+")", "g").test(fmt)) {
				const len = RegExp.$1.length;
				fmt = fmt.replace(RegExp.$1, len==1?o[k]:("00"+o[k]).substr(-len));
			}
		}
		return fmt;
	}
function toWeiXinString(data) {
		let str;
		const now = new Date();
		const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
		const yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate()-1);
		const beforeYesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate()-2);
		const monday = new Date(today);
		monday.setDate(today.getDate()-(today.getDay()?today.getDay()-1:6));
		if(data.getTime() > today.getTime()) {
			str = "";
		} else if(data.getTime() > yesterday.getTime()) {
			str = "昨天";
		} else if(data.getTime() > beforeYesterday.getTime()) {
			str = "前天";
		} else if(data.getTime() > monday.getTime()) {
			const week = {"0":"周日","1":"周一","2":"周二","3":"周三","4":"周四","5":"周五","6":"周六"}; 
			str = week[data.getDay()+""];
		} else {
			const hour = ["凌晨","早上","下午","晚上"];
			const h=data.getHours();
			if(h==12) str = "中午";
			else str = hour[parseInt(h/6)];
			str = format(data,"MM月dd ") + str;
		}
		str += format(data,"HH:mm");
		return str;
	}
export {
	BeforeNavigateTo,
	BeforeSwitchTab,
	utf16toEntities,
	toWeiXinString
}
