bike/pages_admin/Qrcode.vue
2024-09-26 18:01:28 +08:00

124 lines
3.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="page">
<u-navbar title="设备二维码" :border-bottom="false" :background="bgc" title-color='#000'
title-size='36' height='45'></u-navbar>
<view class="ewm" style="margin-top: 300rpx;width: 750rpx;display: flex;justify-content: center;">
<canvas id="qrcode" canvas-id="qrcode" style="width: 600rpx;height:700rpx;" />
</view>
<view class="saveQr" @click="saveQrcode()">
保存二维码
</view>
</view>
</template>
<script>
import UQRCode from 'uqrcodejs';
export default {
data() {
return {
sn:'',
deptId:null
}
},
onLoad(e) {
this.sn = e.sn;
this.deptId = uni.getStorageSync('deptId');
this.generateQrcode();
},
methods: {
generateQrcode() {
const qr = new UQRCode();
const qrSizeRpx = 600; // 二维码大小为600rpx
const qrSizePx = uni.upx2px(qrSizeRpx); // 将rpx转换为px
if (this.deptId == 100) {
qr.data = 'https://dche.ccttiot.com?sn=' + this.sn;
} else if (this.deptId == 101) {
qr.data = 'https://dianche.chuantewulian.cn?sn=' + this.sn;
}
qr.size = qrSizePx; // 设置二维码大小为像素值
// 创建 canvas 上下文
const ctx = uni.createCanvasContext('qrcode', this);
// 设置 qr 的 canvas 上下文
qr.canvasContext = ctx;
qr.make(); // 生成二维码数据
// 绘制二维码
qr.drawCanvas();
// 手动绘制二维码的同时添加设备序列号sn
const sn = this.sn ? 'SN: ' + this.sn : 'SN未知';
// 延迟绘制,确保二维码绘制完成后再绘制文字
setTimeout(() => {
// 添加sn到二维码下面
ctx.setFontSize(24); // 设置字体大小
ctx.setFillStyle('black'); // 设置字体颜色
ctx.setTextAlign('center'); // 设置文本居中
// 在二维码下方绘制sn调整y坐标确保sn显示在二维码下方且不被裁剪
ctx.fillText(sn, qrSizePx / 2, qrSizePx + 40);
// 传入 true保留之前绘制的内容
ctx.draw(true);
}, 100); // 延迟100毫秒确保二维码绘制完成
this.showqr = true;
},
saveQrcode() {
uni.canvasToTempFilePath({
canvasId: 'qrcode',
x: 0,
y: 0,
width: uni.upx2px(600),
height: uni.upx2px(700),
success: (res) => {
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
uni.showToast({
title: '保存成功',
icon: 'success'
});
this.showqr = false;
},
fail: (err) => {
uni.showToast({
title: '保存失败',
icon: 'none'
});
}
});
},
fail: (err) => {
uni.showToast({
title: '生成二维码失败',
icon: 'none'
});
}
});
},
}
}
</script>
<style lang="scss">
.page{
.saveQr{
margin: 0 auto;
margin-top: 100rpx;
display: flex;
align-items: center;
justify-content: center;
width: 502rpx;
height: 68rpx;
background: #4C97E7;
border-radius: 10rpx 10rpx 10rpx 10rpx;
font-weight: 500;
font-size: 36rpx;
color: #FFFFFF;
}
}
</style>