chuangte_bike_newxcx/page_shanghu/guanli/Qrcode.vue
2026-01-19 15:27:10 +08:00

246 lines
7.1 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="" style="width: 100%;text-align: center;margin-top: 50rpx;" v-if="type == 1">
此二维码为运营区二维码,用户扫码可输入车牌开锁
</view>
<view class="ewm" style="margin-top: 200rpx;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,
areaId:'',
areaIdname:'',
type:'',
suitId:'', // 套餐id
http:this.$store.state.app.urlConfig
}
},
onLoad(e) {
if(e.sn){
this.type = 2
this.sn = e.sn;
this.deptId = uni.getStorageSync('deptId');
this.generateQrcode();
}else if(e.areaId){
this.type = 1
this.areaId = e.areaId;
this.deptId = uni.getStorageSync('deptId');
this.generateQrcodes();
}else if(e.t){
// 套餐二维码
this.type = 3
this.suitId = e.t;
this.sn = e.s || ''; // 如果有sn参数则使用否则为空
this.deptId = uni.getStorageSync('deptId');
this.generateSuitQrcode();
}
},
methods: {
generateQrcodes() {
const qr = new UQRCode();
const qrSizeRpx = 600; // 二维码大小为600rpx
const qrSizePx = uni.upx2px(qrSizeRpx); // 将rpx转换为px
const qrData = this.http.areaPrefix + '?i=' + this.areaId;
qr.data = qrData;
qr.size = qrSizePx; // 设置二维码大小为像素值
// 打印二维码路径
console.log('二维码数据:', qrData);
console.log('二维码完整路径:', qrData);
// 创建 canvas 上下文
const ctx = uni.createCanvasContext('qrcode', this);
// 设置 qr 的 canvas 上下文
qr.canvasContext = ctx;
qr.make(); // 生成二维码数据
// 绘制二维码
qr.drawCanvas();
// 手动绘制二维码的同时添加设备序列号sn
const sn = this.sn ? ' ' + this.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;
},
generateQrcode() {
const qr = new UQRCode();
const qrSizeRpx = 600; // 二维码大小为600rpx
const qrSizePx = uni.upx2px(qrSizeRpx); // 将rpx转换为px
let qrData = '';
if (this.deptId == 100) {
qrData = this.http.deviceSnPrefix + '?s=' + this.sn;
} else if (this.deptId == 101) {
qrData = this.http.deviceSnPrefix + '?s=' + this.sn;
}
qr.data = qrData;
qr.size = qrSizePx; // 设置二维码大小为像素值
// 打印二维码路径
console.log('二维码数据:', qrData);
console.log('二维码完整路径:', qrData);
// 创建 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;
},
// 生成套餐二维码
generateSuitQrcode() {
const qr = new UQRCode();
const qrSizeRpx = 600; // 二维码大小为600rpx
const qrSizePx = uni.upx2px(qrSizeRpx); // 将rpx转换为px
// 生成二维码数据使用deviceSnPrefix格式?s=sn&t=suitId
let qrData = '';
if (this.http.deviceSnPrefix) {
// sn后面拼套餐id格式?s=sn&t=suitId
qrData = this.http.deviceSnPrefix + '?s=' + (this.sn || '') + '&t=' + this.suitId;
} else {
// 如果没有deviceSnPrefix使用默认格式
qrData = '?s=' + (this.sn || '') + '&t=' + this.suitId;
}
qr.data = qrData;
qr.size = qrSizePx; // 设置二维码大小为像素值
// 打印二维码路径
console.log('二维码数据:', qrData);
console.log('二维码完整路径:', qrData);
// 创建 canvas 上下文
const ctx = uni.createCanvasContext('qrcode', this);
// 设置 qr 的 canvas 上下文
qr.canvasContext = ctx;
qr.make(); // 生成二维码数据
// 绘制二维码
qr.drawCanvas();
// 延迟绘制,确保二维码绘制完成后再绘制文字
setTimeout(() => {
ctx.setFontSize(24); // 设置字体大小
ctx.setFillStyle('black'); // 设置字体颜色
ctx.setTextAlign('center'); // 设置文本居中
// 在二维码下方绘制套餐信息
const suitText = '套餐ID: ' + this.suitId;
ctx.fillText(suitText, 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) => {
// 打印二维码路径
console.log('二维码临时文件路径:', res.tempFilePath);
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
uni.showToast({
title: '保存成功',
icon: 'success'
});
this.showqr = false;
},
fail: (err) => {
uni.showToast({
title: '保存失败',
icon: 'none'
});
}
});
},
fail: (err) => {
console.error('生成二维码路径失败:', 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>