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" />
相关推荐
天若有情67338 分钟前
前端 vs 后端:入行软件行业,我该如何选择?哪个更“简单”?
前端·后端·软件开发·职业·就业·选择
小画家~1 小时前
第二十八:golang Time.time 时间格式返回定义结构体
java·前端·golang
Cobyte1 小时前
17. Vue3 业务组件库按需加载的实现原理
前端·javascript·vue.js
谢尔登2 小时前
原型理解从入门到精通
开发语言·javascript·原型模式
粥里有勺糖2 小时前
视野修炼-技术周刊第127期 | Valdi
前端·javascript·github
前端世界2 小时前
从零搭建 ASP.NET 单文件 Web 项目:一个能真用的 BookShop 管理页实战
服务器·前端·asp.net
码上成长2 小时前
Vue Router 3 升级 4:写法、坑点、兼容一次讲透
前端·javascript·vue.js
BBB努力学习程序设计2 小时前
响应式页面设计与实现:让网站适配所有设备的艺术
前端·html
IT从业者张某某3 小时前
less 工具 OpenHarmony PC适配实践
前端·microsoft·less
行走的陀螺仪3 小时前
vue3-封装权限按钮组件和自定义指令
前端·vue3·js·自定义指令·权限按钮