vuefunc.js 1.63 KB
Newer Older
fukai committed
1 2 3 4 5 6 7 8 9 10 11

export function findCodeLabel(codes,val)
{
    if(!codes)
        return val;
    let item = codes.find(item=>item.value==val)
    if(!item)
        return val;
    return item.label
} 

fukai committed
12
export function dateFormat (date, format) {
13 14 15 16 17
    if(!date)
      return date
    if(typeof date == 'string')
      date = new Date(date)  

fukai committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    let _format = format || 'yyyy-MM-dd';
  
    const d = date;
    const o = {
      'M+' : d.getMonth() + 1, // month
      'd+' : d.getDate(), // day
      'h+' : d.getHours(), // hour
      'm+' : d.getMinutes(), // minute
      's+' : d.getSeconds(), // second
      'q+' : Math.floor((d.getMonth() + 3) / 3), // quarter
      'S' : d.getMilliseconds() // millisecond
    };
    if (/(y+)/.test(_format)) {
        _format = _format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length));
      }
    
      for (const k in o) {
        if (o.hasOwnProperty(k) && new RegExp('(' + k + ')').test(_format)) {
          _format = _format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
        }
      }
    
      return _format;
}
wangguangchao committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
// 数字展示成金额
export function moneyFormat(value, precision = 2) {
	//0
	let num = value
	if (num == 0) {
		return num.toLocaleString();
	}
	if (num) {
		num = typeof num == 'string' ? parseFloat(num) : num
		num = num.toFixed(precision);
		num = parseFloat(num);
		num = num.toLocaleString();
		return num;

	} else {
		return num = null;
	}
}
fukai committed
60 61 62
export default {
    install(Vue){
        Vue.prototype.findCodeLabel = findCodeLabel
fukai committed
63
        Vue.prototype.dateFormat = dateFormat
wangguangchao committed
64
        Vue.prototype.moneyFormat = moneyFormat
fukai committed
65 66
    }
}