欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一些工具方法16进制颜色转rgba,日期转换方法,去掉字段的两端空格,文件上传,返回dom节点……后面会继续补充

程序员文章站 2022-05-26 14:05:42
...

/* 16进制颜色转rgba */
let hexToRgba = (val, opacity) => {
if (!opacity) opacity = 1;
if (/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})KaTeX parse error: Expected '}', got '#' at position 99: … let str = "#̲"; …{res.join(’,’)})`;
}
return val;
};

/**

  • 日期转换方法

  • @param YYYY-MM-DD hh:mm:ss
    /
    let dateFormat = (type, val) => {
    let date = val ? new Date(/1
    $/g.test(val) ? val * 1 : val) : new Date();
    let YYYY = date.getFullYear() + ‘’;
    let m = date.getMonth() + 1;
    let MM = m > 9 ? m + ‘’ : ‘0’ + m;
    let d = date.getDate();
    let DD = d > 9 ? d + ‘’ : ‘0’ + d;
    let h = date.getHours();
    let hh = h > 9 ? h + ‘’ : ‘0’ + h;
    let $m = date.getMinutes();
    let mm = $m > 9 ? $m + ‘’ : ‘0’ + $m;
    let s = date.getSeconds();
    let ss = s > 9 ? s + ‘’ : ‘0’ + s;
    let obj = { YYYY, MM, DD, hh, mm, ss};

    return type.replace(/(YYYY)|(MM)|(DD)|(hh)|(mm)|(ss)/g, (key) => obj[key]);
    };

/**

  • 去掉字段的两端空格

  • @param Object
    /
    let trimObject = (obj) => {
    let res = {};
    for (let name in obj) {
    if (obj.hasOwnProperty(name)) {
    res[name] = obj[name].replace(/(^\s
    )|(\s*$)/g, ‘’);
    }
    }

    return res;
    };

/**

  • 去掉字段的两端空格
  • @param Object
    */
    let fileSize = (size, count) => {
    if (!count) count = 0;
    if (isNaN(size)) return 0;
    var names = [‘byte’, ‘KByte’, ‘MB’, ‘GB’, ‘TB’];
    if (size < 1024) {
    return size + names[count];
    } else {
    return fileSize(parseFloat((size / 1024).toFixed(2)), ++count);
    }
    };

/**

  • 打开新页签

  • @param url [String] 新页签地址

  • @param flag {Boolean} 是否直接用地址打开
    */
    let openTabByUrl = (url, flag) => {
    let el = document.createElement(“a”);
    document.body.appendChild(el);
    el.href = flag ? url : encodeURI(${location.protocol}//${location.host}${location.pathname}#${url});
    el.target = ‘_blank’;

    el.click();
    document.body.removeChild(el);
    };

/**

  • 文件上传,返回dom节点

  • 如果当前平台类型为:LC 需要调用原生的接口,否则调用H5的方法
    */
    let fileUploadNode = (key, cb) => {

    let box = document.createElement(‘div’);
    document.body.appendChild(box);
    box.setAttribute(‘style’, ‘display: block; height: 0; width: 0; overflow: hidden;’);

    let input = document.createElement(‘input’);
    input.setAttribute(‘name’, key);
    input.setAttribute(‘type’, ‘file’);
    input.setAttribute(‘multiple’, ‘multiple’);
    box.appendChild(input);

    input.addEventListener(‘change’, function() {

     let file = null;
     if (input.files && input.files.length > 0) {
         file = input.files;
         cb(file);
     } else {
         cb('ERROR_CODE');
     }
    
     setTimeout(() => {
         document.body.removeChild(box);
     });
    

    });

    input.click();
    };

/**

  • 日期转换方法 时间粒度

  • @param YYYY-MM-DD hh:mm:ss
    /
    let formatTimeGranularity = (type, val) => {
    let date = val ? new Date(/2
    $/g.test(val) ? val * 1 : val) : new Date();
    let y = date.getFullYear();
    let m = date.getMonth();
    let d = date.getDate();
    let h = date.getHours();
    let mm = date.getMinutes();
    mm = Math.ceil(mm/15) * 15;
    let s = date.getSeconds();
    let source = new Date(y, m, d, h, mm, s);

    return dateFormat(type, source);
    };

/**

  • 取小数,不四舍五入
  • @define {Number} dist 目标数据
  • @define {Number} dot 小数数据
    */
    let numberFixed = (dist, dot) => {
    let str = dist + ‘’;
    let idx = str.lastIndexOf(’.’) + 1;
    let dotLen = str.length - idx;
    if (idx > 0) str = str.substring(0, idx + dot);
    if (dotLen < dot) {
    str += ‘0’.repeat(dot - dotLen);
    }
    return str;
    };

export default {
dateFormat,
hexToRgba,
trimObject,
fileSize,
openTabByUrl,
fileUploadNode,
formatTimeGranularity,
numberFixed
};


  1. 0-9 ↩︎

  2. 0-9 ↩︎

相关标签: 工具方法