huangye-parking/miniapp/components/QrcodePopup.vue
2026-03-03 23:31:28 +08:00

120 lines
2.5 KiB
Vue

<template>
<view class="popup-mask" v-if="visible" @click.self="close">
<view class="popup-content">
<view class="popup-title">优惠券二维码</view>
<view class="qrcode-area">
<canvas canvas-id="qrcode" id="qrcode" class="qrcode-canvas"></canvas>
<text class="coupon-code">{{ code }}</text>
</view>
<view class="popup-tip">请将二维码出示给商户扫码核销</view>
<button class="btn-close" @click="close">关闭</button>
</view>
</view>
</template>
<script>
import UQRCode from 'uqrcodejs'
export default {
name: 'QrcodePopup',
props: {
visible: { type: Boolean, default: false },
code: { type: String, default: '' }
},
emits: ['close'],
watch: {
visible(val) {
if (val && this.code) {
this.$nextTick(() => {
setTimeout(() => this.drawQrcode(), 100)
})
}
}
},
methods: {
close() {
this.$emit('close')
},
drawQrcode() {
const qr = new UQRCode()
qr.data = this.code
qr.size = 200
qr.make()
const ctx = uni.createCanvasContext('qrcode', this)
// 白色背景
ctx.setFillStyle('#ffffff')
ctx.fillRect(0, 0, 200, 200)
// 绘制二维码模块
const tileSize = qr.moduleSize
for (let row = 0; row < qr.moduleCount; row++) {
for (let col = 0; col < qr.moduleCount; col++) {
if (qr.modules[row][col]) {
ctx.setFillStyle('#333333')
} else {
ctx.setFillStyle('#ffffff')
}
const x = qr.paintingX + col * tileSize
const y = qr.paintingY + row * tileSize
ctx.fillRect(x, y, tileSize, tileSize)
}
}
ctx.draw()
}
}
}
</script>
<style scoped>
.popup-mask {
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 999;
}
.popup-content {
background: #fff;
border-radius: 24rpx;
padding: 40rpx;
width: 560rpx;
text-align: center;
}
.popup-title {
font-size: 34rpx;
font-weight: bold;
color: #333;
margin-bottom: 30rpx;
}
.qrcode-area {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20rpx;
}
.qrcode-canvas {
width: 400rpx;
height: 400rpx;
}
.coupon-code {
font-size: 28rpx;
color: #666;
margin-top: 16rpx;
letter-spacing: 4rpx;
}
.popup-tip {
font-size: 24rpx;
color: #999;
margin-bottom: 30rpx;
}
.btn-close {
background: #f5f5f5;
color: #666;
border-radius: 8rpx;
font-size: 28rpx;
}
</style>