Newer
Older
Nanping_sponge_GCGL / src / utils / projectTable.js
@liyingjing liyingjing on 25 Oct 2023 4 KB 海绵工程管理
export function downloadBlob(data, fileName) {
    // 1.解析 blob 数据 生成 url
    const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8' })
    const url = window.URL.createObjectURL(blob)
        // 2.创建下载链接+自动下载
    const link = document.createElement('a')
    link.href = url
    link.download = decodeURI(fileName)
    document.body.appendChild(link)
    link.click()
    window.URL.revokeObjectURL(link)
}

export function downLoad(res, filename) {
    // res 调用接口后返回的数据
    // filename自定义文件名
    let blob = new Blob([res.data]); // 将返回的数据通过Blob的构造方法,创建Blob对象
    if ("msSaveOrOpenBlob" in navigator) {
        window.navigator.msSaveOrOpenBlob(blob, filename); // 针对浏览器
    } else {
        const elink = document.createElement("a"); // 创建a标签
        elink.download = filename;
        elink.style.display = "none";
        // 创建一个指向blob的url,这里就是点击可以下载文件的根结
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); //移除链接
        document.body.removeChild(elink); //移除a标签
    }
}


export function downloadUrl(url) {
    const a = document.createElement('a')
    a.href = url
    a.download = formatDate(state.time, 'YYYY-MM') + '-image'; // 下载后文件名
    a.style.display = 'none'
    document.body.appendChild(a)
    console.log(a.download);
    a.click() // 点击下载
    document.body.removeChild(a) // 下载完成移除元素

}

export function formatDate(time = new Date(), dateType = "YYYY-MM-DD hh:mm:ss") {
    const timeStamp = +new Date(time);
    // 如果转换成数字出错
    if (!timeStamp) {
        return time;
    }
    let str;
    // 得到时间字符串
    const dateStr = new Date(timeStamp);
    str = dateType.replace("YYYY", dateStr.getFullYear());
    str = str.replace(
        "MM",
        (dateStr.getMonth() + 1 < 10 ? "0" : "") + (dateStr.getMonth() + 1)
    );
    str = str.replace(
        "DD",
        (dateStr.getDate() < 10 ? "0" : "") + dateStr.getDate()
    );
    // str = str.replace(
    //     "hh",
    //     (dateStr.getHours() < 10 ? "0" : "") + dateStr.getHours()
    // );
    // str = str.replace(
    //     "mm",
    //     (dateStr.getMinutes() < 10 ? "0" : "") + dateStr.getMinutes()
    // );
    // str = str.replace(
    //     "ss",
    //     (dateStr.getSeconds() < 10 ? "0" : "") + dateStr.getSeconds()
    // );

    return str;
}

export function formatMonths(date, type = new Date().getDate()) {
 var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var data = date.getDate();
    if(type >= 15){ 
     month = month > 9 ?     month : "0" + month;
    }else{ 
        month = month > 9 ?     month : "0" +(month-1);
    }
    var nowMonth = year + "-" + month+ "-"+data
    return nowMonth;
}

export function hexify(color) {
    var values = color
      .replace(/rgba?\(/, '')
      .replace(/\)/, '')
      .replace(/[\s+]/g, '')
      .split(',');
    var a = parseFloat(values[3] || 1),
        r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255),
        g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255),
        b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255);
    return "#" +
      ("0" + r.toString(16)).slice(-2) +
      ("0" + g.toString(16)).slice(-2) +
      ("0" + b.toString(16)).slice(-2);
  }

  export function hexToRgb (val) {   
    // 16进制颜色值的正则
    var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    // 把颜色值变成小写
    var color = val.toLowerCase();
    var result = '';
    if (reg.test(color)) {
        // 如果只有三位的值,需变成六位,如:#fff => #ffffff
        if (color.length === 4) {
            var colorNew = "#";
            for (var i = 1; i < 4; i += 1) {
                colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
            }
            color = colorNew;
        }
        // 处理六位的颜色值,转为RGB
        var colorChange = [];
        for (var i = 1; i < 7; i += 2) {
            colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
        }
        result = "(" + colorChange.join(",") + ")";
        return { rgb: result, r: colorChange[0], g: colorChange[1], b: colorChange[2] };
    } else {
        result = '无效';
        return { rgb: result };
    }

}