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" />
相关推荐
猪猪拆迁队2 分钟前
基于ECS架构的Canvas画布编辑器
前端
Q_Q19632884754 分钟前
python+vue的在线租房 房屋租赁系统
开发语言·vue.js·spring boot·python·django·flask·node.js
不如喫茶去13 分钟前
VUE查询-历史记录功能
前端·javascript·vue.js
持梦远方17 分钟前
重生之我拿捏Linux——《三、shell脚本使用》
前端·chrome
行走在顶尖25 分钟前
代码截断运行逻辑
前端
武天32 分钟前
说说你对slot的理解?slot使用场景有哪些?
vue.js
一枚前端小能手32 分钟前
「周更第8期」实用JS库推荐:decimal.j
前端·javascript
草莓熊Lotso33 分钟前
《C++ Web 自动化测试实战:常用函数全解析与场景化应用指南》
前端·c++·python·dubbo
武天36 分钟前
vue中,key的原理
vue.js
武天40 分钟前
如何打破scope对样式隔离的限制?
vue.js