一、复制文本
const copyText = (text: string) => {
const textArea = document.createElement("textarea");
textArea.style.position = "fixed";
textArea.style.left = "-9999px";
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand("copy");
SetMessage(Type.success, "复制成功");
} catch (err) {
SetMessage(Type.error, "无法复制文本");
}
document.body.removeChild(textArea);
};
二、下载图片
const downLoadImg = (img: string, title: string) => {
const a = document.createElement("a");
a.href = img;
a.download = title + ".png";
a.style.display = "none";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
三、生成二维码
import QRCode from "qrcode";
type QRCodeErrorCorrectionLevel = 'L' | 'M' | 'Q' | 'H';
export async function GenerateQRcode(str: string) {
if (str == null) return false;
try {
const options = {
errorCorrectionLevel: "L" as QRCodeErrorCorrectionLevel, // 设置较低的错误纠正级别
margin: 1, // 减少或保持默认边距,这取决于你需要的效果
scale: 1, // 调整二维码的大小,数值越大二维码越密集
width: 200, // 可选:固定宽度,根据需要设定
color: {
// 可选:颜色设置
dark: "#000000", // 黑色模块的颜色
light: "#ffffff", // 白色背景的颜色
},
};
const dataUrl = await QRCode.toDataURL(str.trim(), options);
return dataUrl;
// const dataUrl = await QRCode.toDataURL(str.trim(), options);
// return dataUrl;
} catch (error) {
SetMessage(Type.error, "二维码生成失败");
}
}