filters.js 2.47 KB
// 为空处理  return ‘-’
const tranNull = value => {
	if (value === null || value === undefined || value === '') return '-'
	return value
}


/*
    @切割字符串
    @str原字符串
    @num字符位数
*/
const cutString = (str, num) => {
	var _str = "";
	if (str == null) {
		return "";
	}
	if (str.length >= num) {
		var strN = str.substring(0, num);
		strN += "...";
		_str = strN;
	} else {
		_str = str;
	}
	return _str;
}

// 钱前面加“¥”
const RMB = (value) => {
	if (value === '--' || value === null || value === undefined) return value
	return `¥${value}`
}

// 处理金额两位小数
const toFixed2 = value => {
	if (!value) {
		return '0.00';
	} else {
		return (value / 100).toFixed(2);
	}

}

// 钱保留两位小数+其他
const ortherToFixed = num => {
	if (num === '0' || num === undefined || num === null || num === '' || num === NaN) {
		return '0.00';
	} else if (num === -1) {
		return '其他';
	} else {
		return (num / 100).toFixed(2);
	}
}


// 数字每三位加个逗号
const toThousands = (num) => {
	var result = [],
		counter = 0;
	num = (num || 0).toString().split('');
	for (var i = num.length - 1; i >= 0; i--) {
		counter++;
		result.unshift(num[i]);
		if (!(counter % 3) && i != 0) {
			result.unshift(',');
		}
	}
	return result.join('');
}

// 1天1小时1分钟1秒
const timeFormatter = value => {
	let time = '';
	if (null != value && "" != value) {
		let timer = parseInt(value);
		if (timer == 0) {
			return '0秒';
		}
		let days = parseInt(timer / (60 * 60 * 24));
		let hours = parseInt((timer % (60 * 60 * 24)) / (60 * 60));
		let minutes = parseInt((timer % (60 * 60)) / (60))
		let seconds = parseInt(timer % (60));		
		if (days > 0) {
			time = time + days +'天'; 
		}
		if (hours > 0) {
			time = time + hours +'小时';
		}
		if (minutes > 0) {
			time = time + minutes +'分钟';
		}
		if (seconds > 0) {
			time = time + seconds +'秒';		
		}
		return time;
	}else{
		return '-'
	}
	
}

// 商户卡券类型
const cardTypeFormate = num => {
	console.log('1111111111111')
 //  1-单次券,2-时长券,3-满减券,4-金额券,5-包天券,6-折扣券
  if(num == '1'){
 		return '单次券'
  }
  if(num == 2){
    return '时长券'
  }
  if(num == 3){
    return '满减券'
  }
  if(num == 4){
    return '金额券'
  }
  if(num == 5){
    return '包天券'
  }
  if(num == 6){
    return '折扣券'
  }

}


export default {
	tranNull,
	RMB,
	cutString,
	toFixed2,
	ortherToFixed,
	toThousands,
	timeFormatter,
  cardTypeFormate
}