
/**
 * 数字保留两位小数点
 * @param {number} num
 * @return {string}
 */
export function priceFilter(num) {
	if(!num) return '0.00';
	num = Number(num);
	return num.toFixed(2);
}

/**
 * 手机号加密
 * @param {string} val
 * @return {string}
 */
export function phoneFilter(val) {
  if (!val) return '';
  return val.slice(0, 3) + '****' + val.slice(7);
}

/**
 * 日期转 年月日格式 YY-mm-dd
 * @param {string} date
 * @return {string}
 */
export function dateToYYmmdd(date) {
	if(!date) return '';
	return date.slice(0, 10);
}

/**
 * 日期转 年月日格式 YY.mm.dd
 * @param {string} date
 * @return {string}
 */
export function dateToYYmmdd2(date) {
	if(!date) return '';
	let newDate = date.slice(0, 10);
	newDate = newDate.replace(/-/g, '.');
	return newDate;
}

/**
 * 日期转 月日格式
 * @param {string} date
 * @return {string}
 */
export function dateTommdd(date) {
	if(!date) return '';
	return date.slice(5, 10);
}

/**
 * 日期转 时分秒格式
 * @param {string} date
 * @return {string}
 */
export function dateToHHmmss(date) {
	if(!date) return '';
	return date.slice(11, 19);
}

export function priceFilter2(val) {
  if(val === 0) return '面议';
	if(!val) return '-';
	val = Number(val);
	return `¥${val.toFixed(2)}`;
}

/**
 * @param {number} time
 * @returns {string}
 */
export function timeFilter(date) {
  date = new Date(date);
  let time = date.getTime();
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff >= 3600 * 24) {
    return Math.ceil(diff / 3600 / 24) + '天前'
  }
}
