/**
 * 时间戳转换成时间
 * @param {*} time
 */
const formDate = (time, formate = "yyyy-mm-dd hh:mm:ss") => {
	var padDate = function(va) {
		va = va < 10 ? "0" + va : va;
		return va;
	};
	if (time) {
		var value = new Date(time * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		// var value = new Date(parseFloat(time))
		var year = value.getFullYear();
		var month = padDate(value.getMonth() + 1);
		var day = padDate(value.getDate());
		var hour = padDate(value.getHours());
		var minutes = padDate(value.getMinutes());
		var seconds = padDate(value.getSeconds());
		let res = "";
		switch (formate) {
			case "mm-dd": {
				res = month + "-" + day;
				break;
			}
			case "yyyy-mm-dd": {
				res = year + "-" + month + "-" + day;
				break;
			}
			case "yyyy-mm": {
				res = year + "-" + month;
				break;
			}
			case "mm月dd日": {
				res = month + "月" + day + "日";
				break;
			}
			case "yyyy年mm月dd日": {
				res = year + "年" + month + "月" + day + "日";
				break;
			}
			case "yyyy年mm月": {
				res = year + "年" + month + "月";
				break;
			}
			case "hh:mm": {
				res = hour + ":" + minutes;
				break;
			}
			case "yyyy-mm-dd hh:mm": {
				res = year + "-" + month + "-" + day + " " + hour + ":" + minutes;
				break;
			}
			case "yyyy.mm.dd":
				res = year + "." + month + "." + day;
				break;
			case "yyyy-mm-dd hh:mm:ss":
			default: {
				res =
					year +
					"-" +
					month +
					"-" +
					day +
					" " +
					hour +
					":" +
					minutes +
					":" +
					seconds;
				break;
			}
		}
		return res;
	}
	return "--";
};

const formDateStr = (time) => {
	var padDate = function(va) {
		va = va < 10 ? "0" + va : va;
		return va;
	};
	if (time) {
		var value = new Date(parseFloat(time));
		var year = value.getFullYear();
		var month = padDate(value.getMonth() + 1);
		var day = value.getDate();
		var hour = padDate(value.getHours());
		var minutes = padDate(value.getMinutes());
		var seconds = padDate(value.getSeconds());
		let res = "";
		if (day == new Date().getDate()) {
			res = "今天" + hour + ":" + minutes;
			return res;
		}
		if (year == new Date().getFullYear()) {
			res = month + "-" + padDate(day) + " " + hour + ":" + minutes;
			return res;
		}
		res = year + "-" + month + "-" + padDate(day);
		return res;
	}
	return "--";
};

const formatRichText = (html) => {
	//控制小程序中图片大小
	let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
		match = match.replace(/style="[^"]+"/gi, "").replace(/style='[^']+'/gi, "");
		match = match.replace(/width="[^"]+"/gi, "").replace(/width='[^']+'/gi, "");
		match = match
			.replace(/height="[^"]+"/gi, "")
			.replace(/height='[^']+'/gi, "");
		return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
		match = match
			.replace(/width:[^;]+;/gi, "max-width:100%;")
			.replace(/width:[^;]+;/gi, "max-width:100%;");
		return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, "");
	newContent = newContent.replace(
		/\<img/gi,
		'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"'
	);
	return newContent;
};
// 金额格式化
const formatPrice = (price) => {
	price = (price || 0).toLocaleString(
		"zh-CN",
		(undefined, {
			minimumFractionDigits: 2,
			maximumFractionDigits: 2,
		})
	);
	return price;
}
export default {
	formDate,
	formDateStr,
	formatRichText,
	formatPrice
};