JS功能函数

一、复制文本

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, "二维码生成失败");
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值