实用工具 - 二维码生成
不建议使用纯中文生成二维码,当无法生成二维码时,请检查二维码内容是否超过长度限制。
1. 使用场景
本例介绍如何在宜搭中生成二维码。
2. 实现功能
2.1. 创建自定义页面
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703668242815-eb8a96ed-ece0-4901-903d-cdf5ebf11297.png)
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703668293086-5e16bee8-0132-4c5e-b156-28deff33272a.png)
2.2. 生成二维码
2.2.1. 引入 qrcode.js
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703668414842-2745eeb6-dbfe-47cb-88a7-14c6e10bf2b2.png)
// 当页面渲染完毕后马上调用下面的函数,这个函数是在当前页面 - 设置 - 生命周期 - 页面加载完成时中被关联的。
export function didMount() {
this.utils.loadScript('https://g.alicdn.com/code/lib/qrcodejs/1.0.0/qrcode.min.js');
}
2.2.2. 将二维码生成函数拷贝至页面 JS 中
该函数接受二维码内容(text:String),返回生成的二维码链接(base64 格式)。
// 生成二维码
export function createQrcode(text = '') {
try {
if (!text) {
this.utils.toast({
title: '二维码无内容',
type: 'error',
});
return '';
};
if (!document.getElementById('qrcodeRootDom')) {
// 若没有 qrcodeRootDom 则创建一个
const qrcodeRoot = document.createElement('div');
qrcodeRoot.setAttribute('id', 'qrcodeRootDom');
qrcodeRoot.style.display = 'none';
window.document.body.appendChild(qrcodeRoot);
};
document.getElementById('qrcodeRootDom').innerHTML = ''; // 清空内容
const qrcode = new QRCode(document.getElementById('qrcodeRootDom'), {
text: encodeURI(text),// 二维码内容
width: 220, // 二维码宽度
height: 220, // 二维码高度
colorDark: '#000000',
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.H,
});
const canvas = qrcode._el.querySelector('canvas'); // 获取生成二维码中的canvas,并将canvas转换成base64
const qrcodeUrl = canvas.toDataURL('image/png'); // 获取到生成的二维码的图片链接
return qrcodeUrl;
} catch (error) {
console.error(error);
this.utils.toast({
title: '二维码生成失败',
type: 'error',
});
}
}
2.2.3. 按钮绑定事件
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703668597578-06afe37c-4926-4833-bc24-a8b9303275d0.png)
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703668633485-6fca0ce4-d928-4fbc-92fa-db5fbe7f768f.png)
绑定下述函数:
// 生成二维码
export function onCreateQrcode() {
this.$('image_lhpz5s8n').set('src', this.createQrcode(this.$('textareaField_lhpz5s8j').getValue()));
}
2.3. 复制二维码
2.3.1. 将 base64ToBlob 函数拷贝至页面 JS 中
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703668735240-023189c8-5fbd-4e00-b6f0-e6d49d3aa46f.png)
// base64ToBlob
export function base64ToBlob(dataurl) {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
2.3.2. 按钮绑定事件
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703668767658-5f0f9c2e-785d-41c6-9e50-32969604407d.png)
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703668786891-836901ba-25c1-4874-91ae-bf2da71a21b7.png)
绑定下述函数:
// 复制二维码
export function onCopyQrcode() {
try {
const base64ImgUrl = this.$('image_lhpz5s8n').get('src');
if (!base64ImgUrl) {
this.utils.toast({
title: '请先生成二维码',
type: 'error',
});
return;
};
const blob = this.base64ToBlob(base64ImgUrl);
navigator.clipboard.write([new window.ClipboardItem({ [blob.type]: blob })]);
this.utils.toast({
title: '二维码复制成功',
type: 'success',
});
} catch (error) {
console.error(error);
this.utils.toast({
title: '二维码复制失败',
type: 'error',
});
}
}
2.4. 图片上传组件添加粘贴上传功能
详情参考:
3. 实现效果
3.1. 生成二维码
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703668984731-b0436eac-ba7d-4d0c-8d14-8c3423d4f24d.png)
3.2. 复制二维码
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703669002764-5d23f154-3668-42d8-af5d-572f12f233cc.png)
![](https://yida-support.oss-cn-shanghai.aliyuncs.com/static/png/1703669031235-e2625c71-cf57-449a-adb8-fc2f5cf971a5.png)
4. 在线试玩
本文档对您是否有帮助?