uniapp实现扫码功能兼容小程序和h5

主要是针对h5端,uniapp自带的扫码方法不支持h5的

对于h5而言需要借助jweixin来实现,也就是微信的扫码功能

实现方式:

可通过npm安装

npm install jweixin-module

引入安装的npm包

import jweixin from 'jweixin-module'

在onLoad方法中加载获取签名的方法

onLoad() {
            this.wx_sanCode()
},

获取签名的方法

wx_sanCode(){
				const thit=this
				var data={
				  url:location.href.split('#')[0]	//当前的地址
				}
                //请求接口根据实际情况替换
				this.$wxhttp.get({url:this.$api.userjsapiticket,data:data}).then(res=>{
					console.log('sdk签名',res)
					jweixin.config({
						debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
						appId: res.data.appId, // 必填,公众号的唯一标识
						timestamp: res.data.timestamp, // 必填,生成签名的时间戳
						nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
						signature: res.data.signature, // 必填,签名
						jsApiList: [	// 必填,需要使用的JS接口列表
							'scanQRCode',
						]
					})
					jweixin.ready(function(){
						console.log('签名成功')
                        //调用扫码方法
						thit.getCheckH5()
					  // config信息验证后会执行 ready 方法,所有接口调用都必须在 config 接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在 ready 函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在 ready 函数中。
					});
					jweixin.error(function(res){
						console.log('签名失败')
					  // config信息验证失败会执行 error 函数,如签名过期导致验证失败,具体错误信息可以打开 config 的debug模式查看,也可以在返回的 res 参数中查看,对于 SPA 可以在这里更新签名。
					});
					
				})
			},
getCheckH5(){
				const thit=this
				jweixin.checkJsApi({
					jsApiList: ['scanQRCode'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
					success: function(res) {
						console.log('返回信息')
						console.log(res)
						// 以键值对的形式返回,可用的api值true,不可用为false
						// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
					}
				})
			},

页面触发扫码方式

let that = this
						//#ifdef H5
						// 判断是否已经登录,H5走这里
						jweixin.scanQRCode({
						  needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
						  scanType: ["qrCode"], // 可以指定扫二维码还是一维码,默认二者都有
						  success: function (res) {
							  console.log(res);
//根据自己的实际需求实现
							that.$wxhttp.get({url:that.$api.getgenerateInfo,data:{qrcodeId:res.resultStr.split('=')[1]}}).then(res=>{
								if(res.code==200){
									uni.navigateTo({
										url: el.path+'?data='+JSON.stringify(res.data)
									});
								}else{
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 2000
									})
									return false
								}
							})
						  }
						});
						console.log('H5走这里')
						//#endif
						//#ifdef MP-WEIXIN
						uni.scanCode({
							success: function (res) {
									that.$wxhttp.get({url:that.$api.getgenerateInfo,data:{qrcodeId:decodeURIComponent(res.path).split('qrcodeId=')[1]}}).then(res=>{
										if(res.code==200){
											uni.navigateTo({
												url: el.path+'?data='+JSON.stringify(res.data)
											});
										}else{
											uni.showToast({
												title: res.msg,
												icon: 'none',
												duration: 2000
											})
											return false
										}
									})
							}
						});
						//#endif

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,uni-app官网不支持H5扫码功能,但是可以通过引用中的manifest.json配置来实现H5扫码功能。具体步骤如下: 1.在manifest.json文件中添加如下配置: ```json "h5": { "devServer": { "https": true } } ``` 2.在需要使用扫码功能的页面中引入js文件: ```html <script src="https://cdn.bootcdn.net/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script> ``` 3.在页面中添加扫码区域: ```html <div id="qrcode"></div> ``` 4.在页面的script标签中编写生成二维码和扫码的逻辑: ```javascript import QRCode from 'qrcodejs2' export default { mounted() { const qrcode = new QRCode('qrcode', { text: 'https://www.example.com', // 二维码内容 width: 256, // 二维码宽度 height: 256, // 二维码高度 colorDark: '#000000', // 二维码颜色 colorLight: '#ffffff', // 二维码背景色 correctLevel: QRCode.CorrectLevel.H // 二维码容错率 }) qrcode._htOption.colorDark = '#000000' // 二维码颜色 qrcode._htOption.colorLight = '#ffffff' // 二维码背景色 const scanner = new Instascan.Scanner({ video: document.getElementById('preview'), mirror: false }) scanner.addListener('scan', function (content) { console.log(content) }) Instascan.Camera.getCameras().then(function (cameras) { if (cameras.length > 0) { scanner.start(cameras[0]) } else { console.error('No cameras found.') } }).catch(function (e) { console.error(e) }) } } ``` 以上代码中,我们使用了qrcodejs2库来生成二维码,使用了Instascan库来实现扫码功能。需要注意的是,Instascan库需要在https环境下才能正常工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值