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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值