qrCode生成二维码

html 复制代码
<template>
	<view class="flex-c-c">
		<canvas :id="canvasId" :canvas-id="canvasId" :type="canvasType"
			:style="{width: size+'rpx',height:size+'rpx'}" />
	</view>
</template>

<script>
	import UQRCode from 'uqrcodejs'
	import {
		getSystemData
	} from '@/utils/public.js'
	export default {
		name: 'qrCodeCmp',
		props: {
			size: {
				type: Number,
				default: 228
			},
			// 二维码生成内容
			value: {
				type: String,
				default: ''
			},
			canvasId: {
				type: String,
				default: 'qrcode'
			}
		},
		watch: {
			value: {
				handler(val) {
					if (val) {
						this.drawQrcode(val)
					}
				},
				immediate: true
			}
		},
		data() {
			return {
				// #ifdef APP-VUE
				canvasType: undefined,
				// #endif
				// #ifndef APP-VUE
				canvasType: '2d',
				// #endif
			}
		},
		methods: {
			async drawQrcode(val) {
				const qr = new UQRCode();
				qr.data = val;
				qr.size = uni.upx2px(this.size);
				qr.areaColor = 'transparent'
				// #ifdef H5
				this.$nextTick(async () => {
					// #endif
					qr.make();
					let canvasContext = null;
					// #ifdef MP-WEIXIN
					const canvas = (this.canvas = await new Promise(resolve => {
						uni
							.createSelectorQuery()
							.in(this) // 在组件内使用需要
							.select(`#${this.canvasId}`)
							.fields({
								node: true,
								size: true
							})
							.exec(res => {
								resolve(res[0].node);
							});
					}));
					canvasContext = canvas.getContext('2d');
					/* 使用dynamicSize+scale,可以解决小块间出现白线问题,dpr可以解决模糊问题 */
					const dpr = getSystemData().pixelRatio;
					canvas.width = qr.dynamicSize * dpr;
					canvas.height = qr.dynamicSize * dpr;
					canvasContext.scale(dpr, dpr);
					// #endif
					// #ifndef MP-WEIXIN
					canvasContext = this.canvasContext = uni.createCanvasContext(this.canvasId,
						this);
					// #endif
					// 组件内调用需传this,vue3 中 this 为 getCurrentInstance()?.proxy
					qr.canvasContext = canvasContext;
					qr.drawCanvas();
					// #ifdef H5
				})
				// #endif
			}
		}

	}
</script>

使用方法

html 复制代码
<qrCodeCmp :size="228" :value="combiBarCode[item.couponId]" :canvasId="'qrcode-'+item.coundownTime" />
相关推荐
GoodStudyAndDayDayUp9 分钟前
gitlab+portainer 实现Ruoyi Vue前端CI/CD
前端·vue.js·gitlab
程序员阿明15 分钟前
vite运行只能访问localhost解决办法
前端·vue
前端 贾公子18 分钟前
uniapp -- 验证码倒计时按钮组件
前端·vue.js·uni-app
zhengddzz20 分钟前
从卡顿到丝滑:JavaScript性能优化实战秘籍
开发语言·javascript·性能优化
淡笑沐白23 分钟前
AJAX技术全解析:从基础到最佳实践
前端·ajax
Go_going_25 分钟前
ajax,Promise 和 fetch
javascript·ajax·okhttp
龙正哲43 分钟前
如何在Firefox火狐浏览器里-安装梦精灵AI提示词管理工具
前端·firefox
徐徐同学1 小时前
轻量级Web画板Paint Board如何本地部署与随时随地在线绘画分享
前端
LuckyLay1 小时前
Vue百日学习计划Day4-8——Gemini版
前端·vue.js·学习
八戒社1 小时前
WooCommerce短代码Shortcodes使用方法
前端·wordpress·woocommerce