// 精确计算加法
export function acc(arg1, arg2) {
	let r1 = deal(arg1)
	let r2 = deal(arg2)
	let m = Math.pow(10, Math.max(r1, r2))
	let res1 = numMulti(arg1, m)
	let res2 = numMulti(arg2, m)
	return (res1 + res2) / m
}

export function deal(arg) {
	var t = 0
	try {
		t = arg.toString().split(".")[1].length
	} catch (e) {}
	return t
}

// 乘法精度计算
export function numMulti(num1, num2) {
	var baseNum = 0
	try {
		baseNum += num1.toString().split(".")[1].length
	} catch (e) {}
	try {
		baseNum += num2.toString().split(".")[1].length
	} catch (e) {}
	return Number(num1.toString().replace(".", "")) * Number(num2.toString().replace(".", "")) / Math.pow(10, baseNum)
}

/**
 * px换算rpx
 * @param {Number} arg px数值
 * @return {Number} rpx数值
 **/
export function toRpx(arg) {
	const res = uni.getSystemInfoSync();
	return (arg * 750) / res.windowWidth;
}

/**
 * rpx换算px
 * @param {Number} arg rpx数值
 * @return {Number} px数值
 **/
export function toPx(arg) {
	const res = uni.getSystemInfoSync();
	return (arg * res.windowWidth) / 750;
}

/**
 * 数值超过万转化为x.xW
 * @param {Number} arg
 * @return {Number}
 */
export function toWan(arg) {
	console.log(arg);
	return arg >= 10000 ? ((arg / 10000).toFixed(1) + 'w') : arg
}

//瀑布流数据处理
export function pubuList(list) {
	let arr1 = list.filter((item, index) => {
		if (index % 2 === 0) {
			return item
		}
	})
	let arr2 = list.filter((item, index) => {
		if (index % 2 === 1) {
			return item
		}
	})
	let arr = arr1.concat(arr2)
	if (arr.length % 2 !== 0) {
		arr.push({
			empty: true
		})
	}
	return arr
}
// 销量过滤器
export function saleFilter(v) {
	let count = 0
	if (v < 10000) {
		count = v
	} else {
		count = toFixedCus(v / 10000, 1) + 'w+'
	}
	return count
}

function toFixedCus(num, decimal) {
	num = num.toString();
	let index = num.indexOf('.');
	if (index !== -1) {
		num = num.substring(0, decimal + index + 1)
	} else {
		num = num.substring(0)
	}
	return parseFloat(num).toFixed(decimal)
}