// 时间格式
const commonTimeAgo = num => {
	if (num) {
		var t = new Date().getTime() - Date.parse(num.replace(/-/gi, "/")),
			n = t / 31104e6,
			a = t / 2592e6,
			r = t / 6048e5,
			o = t / 864e5,
			i = t / 36e5,
			u = t / 6e4;
		return n >= 1 ? parseInt(n) + "年前" : a >= 1 ? parseInt(a) + "个月前" : r >= 1 ? parseInt(r) + "周前" : o >= 1 ?
			parseInt(o) + "天前" : i >= 1 ? parseInt(i) + "小时前" : u >= 1 ? parseInt(u) + "分钟前" : "刚刚";
	}
}

//判断闰年代码
function isLeapYear(Year) {
	if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
		return true;
	} else {
		return false;
	}
}

//计算某月份天数 
function TotalDays(year, month) {
	var days = 0;
	switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			days += 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			days += 30;
			break;
		case 2:
			if (isLeapYear(year)) {
				days += 29;
			} else {
				days += 28;
			}
			break;
	}
	return days;
}

/**
 * @param {Object} mobile
 * 手机号加密
 */
function encode(mobile) {
	if (mobile.length == 13) {
		let pre = mobile.slice(0, 3); // 前三位
		let mid = '****';
		let suf = mobile.slice(-4); // 后四位 
		return `${pro}${mid}${suf}`;
	} else {
		return mobile
	}
}

module.exports = {
	commonTimeAgo,
	isLeapYear,
	TotalDays,
	encode
}
