uni_app中使用uQRcode生成二维码并保存到本地

#效果#

1.首先在插件市场引入uQRcode二维码生成插件,下载并导入到项目uQRCode 全端二维码生成插件 支持nvue 支持nodejs服务端 - DCloud 插件市场icon-default.png?t=N7T8https://ext.dcloud.net.cn/plugin?id=1287

2.在项目中新建vue文件,代码如下

<template>
	<view class="code">
		<view class="uqrcode  flex flex-column justify-center  align-center">
			<uqrcode ref="uqrcode" canvas-id="qrcode" :value="businessData" :options="{ margin: 10}" :size="400"
				sizeUnit="rpx">
			</uqrcode>
			<button class="btn" @click="setAuth">保存图片</button>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				businessData: '',//二维码保存的数据
				
			}
		},
		onLoad: function(option) {
			this.businessData = option.item
		},
		methods: {
            //点击保存
			setAuth() {
				let that = this
                //获取用户设置
				uni.getSetting({
					success: res => {
                        //判断当前用户是否有相册权限
						if (res.authSetting['scope.writePhotosAlbum']) {
							console.log('有权限')
							that.save()
						} else {
                            //没有权限则向用户发起授权请求,
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success() {
									that.save()
								},
                                //拒绝授权后弹框提示用户打开权限,点击确定进入微信设置
								fail() {
									setTimeout(() => {
										uni.showModal({
											title: '提示',
											content: '请打开保存相册权限后点击保存图片!',
											success: res => {
												if (res.confirm) {
													console.log('用户点击确定');
													uni.openSetting()
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										})
									}, 1000)
								}
							})
						}
					}
				})
			},
			//调用保存
			save() {
				this.$refs.uqrcode.save({
					success: () => {
						uni.showToast({
							icon: 'success',
							title: '保存成功'
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.code {
		width: 100vw;
		height: 100vh;

		.uqrcode {
			margin-top: 200rpx;

			.info {
				font-size: 40rpx;
				font-weight: bold;
				line-height: 100rpx;
			}
		}

		.btn {
			width: 400rpx;
			margin-top: 40rpx;
			background-color: #3c9cff;
			color: #fff;
		}
	}
</style>

***说明:

*不建议在二维码中存储太多数据,数据过多会造成二维码扫不出来

*根据微信开发者文档的要求,openSetting()无法在回调中执行,调整后“打开小程序设置页”只支持主动触发

方法一:

<button open-type="openSetting" bindopensetting="callback">打开设置页</button>

方法二:

通过点击行为触发opensertting(),我这里是通过uni.showModal()点击触发的打开设置页面

关于二维码的一些具体配置可以查看官方文档------------》 uQRCode 中文文档

要在uniapp生成二维码保存到本地,可以按照以下步骤进行操作: 1. 首先,使用weapp.qrcode生成二维码。这个库可以用于生成二维码的数据。 2. 然后,使用uni.canvasToTempFilePath方法将生成的二维码转换为临时图片文件。 3. 使用canvas绘制二维码。 4. 接下来,使用uni.canvasToTempFilePath方法再次生成图片文件。 5. 最后,使用uni.saveImageToPhotosAlbum方法将图片保存到本地相册。 具体代码示例如下: ```javascript // 1. 使用weapp.qrcode生成二维码 const qrcode = require('weapp.qrcode'); const qrData = 'your qrcode data'; const qrcodeImg = qrcode.createQrCodeImg(qrData); // 2. 使用uni.canvasToTempFilePath生成临时图片文件 uni.canvasToTempFilePath({ canvasId: 'canvas', // canvas的id success: function (res) { const tempFilePath = res.tempFilePath; // 3. 使用canvas绘制二维码 const ctx = uni.createCanvasContext('canvas'); ctx.drawImage(qrcodeImg, 0, 0, 200, 200); // 绘制二维码图片 ctx.draw(false, function () { // 4. 使用uni.canvasToTempFilePath生成图片文件 uni.canvasToTempFilePath({ canvasId: 'canvas', success: function (res2) { const qrCodeFilePath = res2.tempFilePath; // 5. 使用uni.saveImageToPhotosAlbum保存到本地 uni.saveImageToPhotosAlbum({ filePath: qrCodeFilePath, success: function () { console.log('保存成功'); }, fail: function (err) { console.log('保存失败', err); } }); }, fail: function (err2) { console.log('生成二维码图片文件失败', err2); } }); }); }, fail: function (err3) { console.log('生成临时图片文件失败', err3); } }); ``` 通过以上步骤,你就可以在uniapp生成二维码保存到本地相册了。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值