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" />
相关推荐
下雪天的夏风11 分钟前
TS - tsconfig.json 和 tsconfig.node.json 的关系,如何在TS 中使用 JS 不报错
前端·javascript·typescript
青稞儿17 分钟前
面试题高频之token无感刷新(vue3+node.js)
vue.js·node.js
diygwcom23 分钟前
electron-updater实现electron全量版本更新
前端·javascript·electron
volodyan26 分钟前
electron react离线使用monaco-editor
javascript·react.js·electron
^^为欢几何^^35 分钟前
lodash中_.difference如何过滤数组
javascript·数据结构·算法
Hello-Mr.Wang39 分钟前
vue3中开发引导页的方法
开发语言·前端·javascript
程序员凡尘1 小时前
完美解决 Array 方法 (map/filter/reduce) 不按预期工作 的正确解决方法,亲测有效!!!
前端·javascript·vue.js
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
北岛寒沫5 小时前
JavaScript(JS)学习笔记 1(简单介绍 注释和输入输出语句 变量 数据类型 运算符 流程控制 数组)
javascript·笔记·学习
everyStudy5 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript