uniapp实现微信H5扫码

在uni-app官网上发现uni-app不支持H5扫码功能,但是下面的提示说明可以通过微信的JS-SDK实现扫码功能,下面这篇文章主要给大家介绍了关于uniapp实现微信H5扫码功能的完整步骤,需要的朋友可以参考下

页面如下:
在这里插入图片描述
首先打开uniapp官网,发现uni-app不支持H5扫码 。
uni.scanCode(OBJECT)
在这里插入图片描述
but,继续往下看:引用微信的SDK去实现扫码
在这里插入图片描述

使用说明
在这里插入图片描述

第一步:引入SDK 文档
1.下载js文件,直接引入到项目里 下载地址

<script>
	import wx from '/common/jweixin.js';
	export default{
		...
	}
</script>

2.通过npm安装,按需引入

npm install weixin-js-sdk --save
<script>
	import wx from 'weixin-js-sdk';
	export default{
		...
	}
</script>

第二步:配置微信config信息

 onLoad: function () { 
   this.getCofig();
 },
methods: {
	getCofig() {
		uni.request({
	        url: '/Common/In?action=get_wxconfig',
	        method: 'post',
	        success: (res) => {
	        	if (res) {
	        		wx.config({
				        debug: true, // 开启调试模式,
				        appId: res.appId, // 必填,企业号的唯一标识
				        timestamp: res.timestamp, // 必填,生成签名的时间戳
				        nonceStr: res.nonceStr, // 必填,生成签名的随机串
				        signature: res.signature, // 必填,签名
				        jsApiList: ['scanQRCode', 'checkJsApi','chooseImage'], // 必填,需使用的JS接口列表
	     			});
		      		wx.ready(() => {
		        		console.log('配置完成,扫码前准备完成')
		      		});
			      	wx.error(function (res) {
			      		//wx.config配置错误,会弹出窗口哪里错误,然后根据微信文档查询即可。
		        		console.log('出错了:' + res.errMsg); 
			      	});
	      		} else {
	            	console.log('获取配置信息返回为空');
	       		}
	   		},
	        fail: error => {
	          console.log(error, '请求获取微信配置失败');
	        },
		});
	},
}

第三步:触发事件

// 点击扫码 区分普通扫码和H5扫码
scan() {
	// #ifndef H5
	uni.scanCode({
		success: function (res) {
			console.log("进来了1")
			console.log('条码res:' + res);
			console.log('条码类型:' + res.scanType);
			console.log('条码内容:' + res.result);  
		},
		fail: error => {
			console.log("暂不支持1") 
		}
	});
   	// #endif
   	// #ifdef H5
   	// this.log("暂不支持H5扫码 走onScan这个方法") 
   	this.onScan()
   	// #endif
},

// h5扫描二维码并解析  
onScan() {
	wx.scanQRCode({
		needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
		scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有
		success: function (res) { 
			var result = res.resultStr; // 当 needResult 为 1 时,扫码返回的结果
			var resultArr = result.split(','); // 扫描结果以逗号分割数组(一维码)
			var codeContent = resultArr[resultArr.length - 1]; // 获取数组最后一个元素,也就是最终的内容 
		},
		fail: function (response) {
			console.log("调用wx.scanQRCode扫码失败");
		},
	});
},

二维码:
在这里插入图片描述
一维码/条形码:
在这里插入图片描述

借鉴:https://www.jb51.net/article/267540.htm

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
UniApp中,可以通过uni.scanCode()方法调用微信小程序的扫码功能。如果需要自定义扫码页面,可以使用uni.previewImage()方法来显示自己的扫码页面,并在页面中使用uni.scanCode()方法来触发扫码功能。 具体步骤如下: 1. 创建自定义扫码页面。 在页面中使用uni.previewImage()方法来显示图片,可以在图片上覆盖一个按钮或者其他交互元素来触发扫码功能。示例代码如下: ``` <template> <view class="container"> <image :src="qrCodeUrl" mode="aspectFill" @tap="scanCode"></image> </view> </template> <script> export default { data() { return { qrCodeUrl: 'xxx' // 扫码页面的图片地址 } }, methods: { scanCode() { uni.scanCode({ success(res) { console.log(res) } }) } } } </script> ``` 2. 在小程序配置文件中设置自定义扫码页面路径。 在微信小程序的app.json文件中,可以设置自定义扫码页面的路径。示例代码如下: ``` { "pages": [ "pages/index/index", "pages/scan/scan" ], "window": { "navigationBarTitleText": "Demo" }, "tabBar": { "list": [{ "pagePath": "pages/index/index", "text": "首页" }, { "pagePath": "pages/scan/scan", "text": "扫码" }] }, "usingComponents": {} } ``` 3. 调用uni.scanCode()方法触发扫码功能。 在自定义扫码页面中,可以使用uni.scanCode()方法来触发扫码功能。成功扫描到二维码后,可以在回调函数中获取二维码的内容。示例代码如下: ``` uni.scanCode({ success(res) { console.log(res) } }) ``` 以上就是在UniApp中自定义微信小程序扫码页面的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值