vue封装公用的方法集合

    //判断设备是移动端还是pc端
    export function isMobile() {
        let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
        return flag;
    }
        // js保留两位小数(不四舍五入bu带¥)
    export  function formatDecimalNull(num, decimal) {
        if(num){
            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)
        }else{
            return '0.00'
        }
    }
    //时间范围,最近一个月,最近三个月
export function getDay(day) {
    var today = new Date();
    var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(targetday_milliseconds);
    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    tMonth = doHandleMonth(tMonth + 1);
    tDate = doHandleMonth(tDate);
    return tYear + "-" + tMonth + "-" + tDate;
}
    function doHandleMonth(month){
      var m = month;
      if(month.toString().length == 1){
        m = "0" + month;
      }
      return m;
    }
    //导出xlxs
    export function exportLists(res, filename) {
    const blob = new Blob([res]);
    const elink = document.createElement('a');
    elink.download = filename;
    elink.style.display = 'none';
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
}
//当前年月日
export function getYears() {
    var day = new Date();
    day.setTime(day.getTime());
    var y = day.getFullYear()
    var m = day.getMonth() + 1
    var d = day.getDate();
    if (m < 10) {
        m = "0" + m
    }
    if (d < 10) {
        d = "0" + d
    }
    var t;
        t = y + "-" + m+'-'+d
    return t
}

    在需要的页面导入即可使用

你可能感兴趣的:(vue)