//上传文件大小转换
byteChange(byte) {
if (!byte) return;
let size = "";
if (byte < 0.1 * 1024) {
// 小于0.1KB 则转化成B
size = byte.toFixed(2) + "B";
} else if (byte < 0.1 * 1024 * 1024) {
// 小于0.1MB 则转换成KB
size = (byte / 1024).toFixed(2) + "KB";
} else if (byte < 0.1 * 1024 * 1024 * 1024) {
// 小于0.1GB 则转换成MB
size = (byte / (1024 * 1024)).toFixed(2) + "MB";
} else if (byte < 0.1 * 1024 * 1024 * 1024 * 1024) {
// 小于0.1TB 则转换成GB
size = (byte / (1024 * 1024 * 1024)).toFixed(2) + "GB";
} else if (byte < 0.1 * 1024 * 1024 * 1024 * 1024 * 1024) {
// 小于0.1PB 则转换成TB
size = (byte / (1024 * 1024 * 1024 * 1024)).toFixed(2) + "TB";
} else if (byte < 0.1 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024) {
// 小于0.1EB 则转换成PB
size = (byte / (1024 * 1024 * 1024 * 1024 * 1024)).toFixed(2) + "PB";
} else if (byte < 0.1 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024) {
// 小于0.1ZB 则转换成EB
size =
(byte / (1024 * 1024 * 1024 * 1024 * 1024 * 1024)).toFixed(2) + "EB";
} else if (
byte <
0.1 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024
) {
// 小于0.1YB 则转换成ZB
size =
(byte / (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)).toFixed(2) +
"ZB";
}
return size;
},
下载图片
downloadImage(imgsrc, name) {
let image = new Image();
// 解决跨域 Canvas 污染问题
image.setAttribute("crossOrigin", "anonymous");
image.onload = function () {
let canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
let context = canvas.getContext("2d");
context.drawImage(image, 0, 0, image.width, image.height);
let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
let a = document.createElement("a"); // 生成一个a元素
let event = new MouseEvent("click"); // 创建一个单击事件
a.download = name || "photo"; // 设置图片名称
a.href = url; // 将生成的URL设置为a.href属性
a.dispatchEvent(event); // 触发a的单击事件
};
//将资源链接赋值过去,才能触发image.onload 事件
image.src = imgsrc;
},
前端导出element ui表格
// 导出 html-tabel
tableToExcel() {
let tabelStr;
// element table中有两个表格一个是浮动的一个是不浮动的,如果没有设置浮动就不会有浮动的表格,就会只有一个表格
let fixedDom = document.querySelector(".html-tabel .el-table__fixed");
let tableDom = document.querySelector(".html-tabel");
if (fixedDom) tabelStr = fixedDom.outerHTML;
if (tableDom) tabelStr = tableDom.outerHTML;
//根据正则表达式,把style='mso-number-format:"\@"' 塞入td中,将数据转成String
tabelStr = tabelStr.replace(/<td/g, "<td STYLE='MSO-NUMBER-FORMAT:\\@'");
//Worksheet名
let worksheet = "乡镇专管员配置履职情况";
let uri = "data:application/vnd.ms-excel;base64,";
// 真正要导出(下载)的HTML模板
let exportTemplate = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
<x:Name>${worksheet}</x:Name>
<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
</head>
<body>
${tabelStr}
</body>
</html>`;
//通过创建a标签实现
let link = document.createElement("a");
link.href = uri + this.base64(exportTemplate);
//对下载的文件命名
link.download = `${worksheet}.xls`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
},
//输出base64编码
base64(s) {
return window.btoa(unescape(encodeURIComponent(s)));
},
限制只能输入小数,整数,小数将保留两位
oninput="value=value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
前端计算浮点数问题
//加法函数
function accAdd(arg1, arg2) {
var r1, r2, m;
try {
r1 = arg1.toString().split(".")[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = arg2.toString().split(".")[1].length;
} catch (e) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2));
return (arg1 * m + arg2 * m) / m;
} //给Number类型增加一个add方法,使用时直接用 .add 即可完成计算。
Number.prototype.add = function (arg) {
return accAdd(arg, this);
};
// console.log(0.1.add(0.2).add(0.3));//等价于0.1+0.2+0.3,输出0.6
// console.log(0.1 + 0.2 + 0.3);//输出0.6000000000000001
//减法函数
function Subtr(arg1, arg2) {
var r1, r2, m, n;
try {
r1 = arg1.toString().split(".")[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = arg2.toString().split(".")[1].length;
} catch (e) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2)); //动态控制精度长度
n = (r1 >= r2) ? r1 : r2;
return parseFloat(((arg1 * m - arg2 * m) / m).toFixed(n));
}
Number.prototype.sub = function (arg) {
return Subtr(this, arg);
};
// console.log(0.6.sub(0.2).sub(0.3));//等价于0.6-0.2-0.3 输出0.1
// console.log(0.6 - 0.2 - 0.3);//输出:0.09999999999999998
//乘法函数
function 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);
}
Number.prototype.mul = function (arg) {
return accMul(arg, this);
};
// console.log(0.1.mul(0.2).mul(0.3)); //等价于0.1 * 0.2 * 0.3 输出0.006
// console.log(0.1 * 0.2 * 0.3); //输出:0.006000000000000001
//除法函数
function accDiv(arg1, arg2) {
var t1 = 0,
t2 = 0,
r1, r2;
try {
t1 = arg1.toString().split(".")[1].length;
} catch (e) { }
try {
t2 = arg2.toString().split(".")[1].length;
} catch (e) { }
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
return accMul((r1 / r2), Math.pow(10, t2 - t1));
}
Number.prototype.div = function (arg) {
return accDiv(this, arg);
};
// console.log(0.6.div(0.2).div(0.1)); //等价于0.6 / 0.2 / 0.1 输出30
// console.log(0.6 / 0.2 / 0.1); //输出:29.999999999999993