公共方法的总结封装

/**

  • 将Json内容生成File
  • @param name 文件名
  • @param data 数据
  • @returns {null|File}
    */
    // const buildFileForJson = (name, data) => {
    // try {
    // const content = JSON.stringify(data);
    // return new File([content], name, {
    // type: ‘text/plain’,
    // });
    // } catch (e) {
    // return null;
    // }
    // };

/**

  • 深拷贝

  • @param data

  • @returns {null|any}
    /
    const deepCopy = (data) => {
    try {
    return JSON.parse(JSON.stringify(data));
    } catch (e) {
    return null;
    }
    };
    /
    *

  • 预处理VinList,判断长度,去重等

  • @param list

  • @returns {any[]}
    */
    const preVinList = (list) => {
    // 去除空格
    list.forEach((val, idx) => {
    list[idx] = val.trim();
    });
    // 判断长度
    list = list.filter((val) => val.length === 17);
    // 去重
    list = […new Set(list)];
    return list;
    };
    // 乘法函数
    const accMul = (arg1, arg2) => {
    var m = 0,
    s1 = arg1.toString(),
    s2 = arg2.toString();
    try {
    m += s1.split(‘.’)[1].length;
    } catch (e) { }
    try {
    m += s2.split(‘.’)[1].length;
    } catch (e) { }
    return (Number(s1.replace(‘.’, ‘’)) * Number(s2.replace(‘.’, ‘’))) / Math.pow(10, m);
    };
    module.exports = {
    // 通过创建a标签下载 有两种形式 第一种是根据地址下载 第二种是根据数据下载
    // type 是url还是data; data 是数据
    // bssID是前缀
    // suffix是后缀
    downloadA(type, data, bssID, suffix) {
    if (type === ‘url’) {
    const link = document.createElement(‘a’);
    link.style.display = ‘none’;
    link.href = data;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    } else {
    // const str = JSON.stringify(data)
    const link = document.createElement(‘a’);
    // const blob = new Blob([str]);
    link.style.display = ‘none’;
    link.href = data;
    link.download = bssID ? bssID + suffix : suffix;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    }
    },
    // 下载二进制流 excel 文件 data是接口返回的二进制流文件 name是传递过来的表格下载名字
    downloadExcel(data, name) {
    const url = window.URL.createObjectURL(new Blob([data], { type: ‘application/octet-stream’ }));
    const link = document.createElement(‘a’);
    link.style.display = ‘none’;
    link.href = url;
    link.setAttribute(‘download’, ${name}.xlsx);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    },
    // 下载二进制流 json 文件 data是接口返回的二进制流文件 name是传递过来的表格下载名字
    downloadTxt(data, name) {
    const url = window.URL.createObjectURL(new Blob([data], { type: ‘application/octet-stream’ }));
    const link = document.createElement(‘a’);
    link.style.display = ‘none’;
    link.href = url;
    link.setAttribute(‘download’, ${name}.json);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    },
    // 返回不带-的时间
    formatDateNOlink(data, type = true) {
    const time = new Date(data);
    const YY = ${time.getFullYear()};
    let MM = ‘’;
    let DD = ‘’;
    let HH = ‘’;
    let mm = ‘’;
    let ss = ‘’;
    if (time.getMonth() + 1 < 10) {
    MM = 0${time.getMonth() + 1};
    } else {
    MM = ${time.getMonth() + 1};
    }
    if (time.getDate() < 10) {
    DD = 0${time.getDate()};
    } else {
    DD = ${time.getDate()};
    }
    if (time.getHours() < 10) {
    HH = 0${time.getHours()};
    } else {
    HH = ${time.getHours()};
    }
    if (time.getMinutes() < 10) {
    mm = 0${time.getMinutes()};
    } else {
    mm = ${time.getMinutes()};
    }
    if (time.getSeconds() < 10) {
    ss = 0${time.getSeconds()};
    } else {
    ss = ${time.getSeconds()};
    }
    if (!type) {
    return ${YY}-${MM}-${DD} ${HH}:${mm}:${ss};
    }
    return ${YY + MM + DD + HH + mm + ss};
    },
    // 返回带-的时间
    formatDatelink(data) {
    const time = new Date(data);
    const YY = ${time.getFullYear()};
    let MM = ‘’;
    let DD = ‘’;
    if (time.getMonth() + 1 < 10) {
    MM = 0${time.getMonth() + 1};
    } else {
    MM = ${time.getMonth() + 1};
    }
    if (time.getDate() < 10) {
    DD = 0${time.getDate()};
    } else {
    DD = ${time.getDate()};
    }
    return ${YY + '-' + MM + '-' + DD};
    },
    // 返回时分秒
    formatDateSecond() {
    const time = new Date(new Date().getTime());
    let HH = ‘’;
    let mm = ‘’;
    let ss = ‘’;
    if (time.getHours() < 10) {
    HH = 0${time.getHours()};
    } else {
    HH = ${time.getHours()};
    }
    if (time.getMinutes() < 10) {
    mm = 0${time.getMinutes()};
    } else {
    mm = ${time.getMinutes()};
    }
    if (time.getSeconds() < 10) {
    ss = 0${time.getSeconds()};
    } else {
    ss = ${time.getSeconds()};
    }
    return ${HH}:${mm}:${ss};
    },
    // 数据对象去重处理 params是传递过来的需要去重的那一项
    repeatData(data, params) {
    const obj = {};
    return data.reduce((item, next) => {
    if (!obj[next[params]]) {
    obj[next[params]] = true;
    item.push(next);
    }
    return item;
    }, []);
    },
    // 获取当前记录的索引
    getCurrentIndex({ index, query: { currentPage = 1, pageSize = 10 } = {} }) {
    if (index === undefined) return ‘’;
    const serial = (currentPage - 1) * pageSize + (+index + 1);
    if (serial < 10) {
    return 00${serial};
    }
    if (serial < 100) {
    return 0${serial};
    }
    return serial;
    },
    // 前端分页
    getCurrentItems(items, { currentPage = 1, pageSize = 10 }) {
    const total = items.length;
    const count = currentPage * pageSize;
    const end = count > total ? total : count;
    return items.slice((currentPage - 1) * pageSize, end);
    },
    // hex to string
    hextoString(hexCharCodeStr) {
    const trimedStr = hexCharCodeStr.trim();
    const rawStr = trimedStr.substr(0, 2).toLowerCase() === ‘0x’ ? trimedStr.substr(2) : trimedStr;
    const len = rawStr.length;
    if (len % 2 !== 0) {
    return ‘’;
    }
    let curCharCode;
    const resultStr = [];
    for (let i = 0; i < len; i += 2) {
    curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
    resultStr.push(String.fromCharCode(curCharCode));
    }
    return resultStr.join(‘’);
    },
    // string to hex
    stringtoHex(str) {
    if (str == null || str === ‘’ || typeof str === ‘undefined’) return ‘’;
    const hexCharCode = [];
    // hexCharCode.push(‘0x’);
    for (let i = 0; i < str.length; i += 1) {
    hexCharCode.push(str.charCodeAt(i).toString(16).toUpperCase());
    }
    return hexCharCode.join(‘’);
    },
    // 获取当前时间
    getCurrentMoment() {
    return moment();
    },
    getYears(end, begin) {
    end = end ? moment(end) : moment().add(20, ‘years’);
    begin = begin ? moment(begin) : this.getCurrentMoment();
    const years = [];
    years.push(end.format(‘YYYY’));
    end.subtract(1, ‘y’);
    while (end >= begin) {
    years.push(end.format(‘YYYY’));
    end = end.subtract(1, ‘y’);
    }
    return years;
    },
    // 计算文件大小
    humanFileSize(bytes, s

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值