关于uniapp使用uni.scanCode扫码时IOS出现空白页面

关于uniapp使用uni.scanCode扫码时IOS出现空白页面

最近在使用uni.scanCode进行扫码读取商品信息时发现在IOS端出现空白页,但是把条码对准摄像头却又能扫出来,怀疑是uniapp封装的uni.scanCode和ios不兼容的问题了,于是自己参照网上的实例用源生的html5写了一个。

效果图

在这里插入图片描述

uniapp的写法

scanCode(type) { //调起客户端扫码界面进行扫码,然后进入商品详情页
	var current="";
	if (type == 1) {
		current = "b2c";
	} else {
		current = "o2o";
	}
	uni.scanCode({
		//onlyFromCamera: true,// 只允许通过相机扫码
		success: function(res) {
			Tool.post('uni/home/getProductByScanCode', {
				"barcode": res.result //商品69码
			}, function(res) {
				if (res.data.success) {
					uni.navigateTo({
						url: '/pages/list/list?keyword=' + res.data.data.name + '&plus=flase&current=' + current
					});
				} else {
					uni.showModal({
						title: '扫描结果',
						content: '商品不存在,是否重新扫描?',
						success(ret) {
							if (ret.confirm) {
								that.scanCode();
							}
						}
					});
					return;
				}
			});
		}
});

HTML中的barcode

1、首先新建一个vue

<template>
	<view>
	</view>
</template>
<script>
	import Tool from '../../common/tool.js';
	var barcode = null;
	export default {
		data() {
			return {
				flash: false, //是否打开摄像头
				type: ''
			};
		},
		onLoad(d) {
			this.type = d.type;
			var pages = getCurrentPages();
			var page = pages[pages.length - 1];
			// #ifdef APP-PLUS
			plus.navigator.setFullscreen(true); //全屏
			var currentWebview = page.$getAppWebview();
			this.createBarcode(currentWebview); //创建二维码窗口
			this.createView(currentWebview); //创建操作按钮及tips界面
			// #endif
		},
		methods: {
			// 扫码成功回调
			onmarked(type, result) {
				this.scanning(result);
			},
			scanning(result) {
				var that = this
				var iso2o = "";
				if (that.type == 1) {
					iso2o = "b2c";
				} else {
					iso2o = "o2o";
				}
				uni.navigateBack({
					delta: 2
				});
				setTimeout(function() {
					Tool.post('uni/home/getProductByScanCode', {
						"barcode": result //商品69码
					}, function(res) {
						if (res.data.success) {
							uni.navigateTo({
								url: '/pages/list/list?keyword=' + res.data.data.name + '&plus=flase&current=' + iso2o
							});
						} else {
							uni.showModal({
								title: '扫描结果',
								content: '商品不存在,是否重新扫描?',
								success(ret) {
									if (ret.confirm) {
										uni.navigateTo({
											url: '/pages/home/scanning?type=' + that.type
										});
									} else {
										barcode.close();
									}
								}
							});
						}
					});
				}, 1000);
			},
			// 创建二维码窗口
			createBarcode(currentWebview) {
				barcode = plus.barcode.create('barcode', [plus.barcode.QR, plus.barcode.EAN13], {
					top: '0',
					left: '0',
					width: '100%',
					height: '100%',
					scanbarColor: '#1DA7FF',
					position: 'static',
					frameColor: '#1DA7FF'
				});
				barcode.onmarked = this.onmarked;
				barcode.setFlash(this.flash);
				currentWebview.append(barcode);
				barcode.start();
			},
			// 创建操作按钮及tips
			createView(currentWebview) {
				var temp = this;
				// 创建返回原生按钮
				var backVew = new plus.nativeObj.View('backVew', {
						top: '0px',
						left: '0px',
						height: '40px',
						width: '30%'
					},
					[{
						tag: 'img',
						id: 'backBar',
						src: 'static/backBar.png',
						position: {
							top: '2px',
							left: '3px',
							width: '35px',
							height: '35px'
						}
					}]);
				// 创建打开手电筒的按钮
				var scanBarVew = new plus.nativeObj.View('scanBarVew', {
						top: '60%',
						left: '40%',
						height: '10%',
						width: '20%'
					},
					[{
							tag: 'img',
							id: 'scanBar',
							src: 'static/scanBar.png',
							position: {
								width: '28%',
								left: '36%',
								height: '30%'
							}
						},
						{
							tag: 'font',
							id: 'font',
							text: '轻触照亮',
							textStyles: {
								size: '10px',
								color: '#ffffff'
							},
							position: {
								width: '80%',
								left: '10%'
							}
						}
					]);
				// 创建展示类内容组件
				var content = new plus.nativeObj.View('content', {
						top: '0px',
						left: '0px',
						height: '100%',
						width: '100%'
					},
					[{
							tag: 'font',
							id: 'scanTitle',
							text: '二维码/条码',
							textStyles: {
								size: '18px',
								color: '#ffffff'
							},
							position: {
								top: '0px',
								left: '0px',
								width: '100%',
								height: '40px'
							}
						},
						{
							tag: 'font',
							id: 'scanTips',
							text: this.name,
							textStyles: {
								size: '14px',
								color: '#ffffff',
								whiteSpace: 'normal'
							},
							position: {
								top: '90px',
								left: '10%',
								width: '80%',
								height: 'wrap_content'
							}
						}
					]);
				// 创建相册按钮
				var album = new plus.nativeObj.View('album', {
						top: '0px',
						right: '0px',
						height: '100%',
						width: '30%'
					},
					[{
							tag: 'font',
							id: 'scanTitle',
							text: '相册',
							textStyles: {
								size: '18px',
								color: '#ffffff'
							},
							position: {
								top: '3px',
								right: '1px',
								width: '80px',
								height: '35px'
							}
						},
						{
							tag: 'font',
							id: 'scanTips',
							text: this.name,
							textStyles: {
								size: '14px',
								color: '#ffffff',
								whiteSpace: 'normal'
							},
							position: {
								top: '90px',
								left: '10%',
								width: '80%',
								height: 'wrap_content'
							}
						}
					]);
				backVew.interceptTouchEvent(true);
				scanBarVew.interceptTouchEvent(true);
				currentWebview.append(content);
				currentWebview.append(album);
				currentWebview.append(scanBarVew);
				currentWebview.append(backVew);
				backVew.addEventListener("click", function(e) { //返回按钮
					uni.navigateBack({
						delta: 1
					});
					barcode.close();
					plus.navigator.setFullscreen(false);
				}, false);
				album.addEventListener("click", function(e) { //相册按钮
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
						sourceType: ['album'], //从相册选择
						success: function(res) {
							plus.barcode.scan(res.tempFilePaths[0], function(type, result) {
								temp.scanning(result);
							}, function(e) {
								uni.navigateBack({
									delta: 2
								});
								uni.showModal({
									title: '扫描结果',
									content: '未发现指定条码,是否重新扫描?',
									success(ret) {
										if (ret.confirm) {
											uni.navigateTo({
												url: '/pages/home/scanning?type=' + temp.type
											});
										} 
									}
								}); 
							});
						},
						fail:function(fres){
							uni.navigateBack({
								delta: 2
							});
							uni.showToast({
								title: fres.message,
								icon: 'none',
								duration: 1000
							})
							barcode.close();
						}
					});
					//plus.navigator.setFullscreen(false);
				}, false);
				scanBarVew.addEventListener("click", function(e) { //点亮手电筒
					temp.flash = !temp.flash;
					if (temp.flash) {
						scanBarVew.draw([{
								tag: 'img',
								id: 'scanBar',
								src: 'static/yellow-scanBar.png',
								position: {
									width: '28%',
									left: '36%',
									height: '30%'
								}
							},
							{
								tag: 'font',
								id: 'font',
								text: '轻触照亮',
								textStyles: {
									size: '10px',
									color: '#ffffff'
								},
								position: {
									width: '80%',
									left: '10%'
								}
							}
						]);
					} else {
						scanBarVew.draw([{
								tag: 'img',
								id: 'scanBar',
								src: 'static/scanBar.png',
								position: {
									width: '28%',
									left: '36%',
									height: '30%'
								}
							},
							{
								tag: 'font',
								id: 'font',
								text: '轻触照亮',
								textStyles: {
									size: '10px',
									color: '#ffffff'
								},
								position: {
									width: '80%',
									left: '10%'
								}
							}
						])
					}
					if (barcode) {
						barcode.setFlash(temp.flash);
					}
				}, false)
			}
		},
		onBackPress() {
			// #ifdef APP-PLUS
			// 返回时退出全屏
			barcode.close();
			plus.navigator.setFullscreen(false);
			// #endif
		},
		onUnload() {
			plus.navigator.setFullscreen(false);
		}
	};
</script>

<style scoped>
</style>

2、在pages.json中添加如下内容

{
	 "path" : "pages/home/scanning",
	  "style" : {
		"navigationBarTitleText" : "二维码/条码",
		      "app-plus":{
			"titleNView":false
		}
	  }
},

3、在需要扫码的事件里判断一下平台

scanCode(type) { //调起客户端扫码界面进行扫码,然后进入商品详情页
	var that = this;
	//#ifdef APP-PLUS
		uni.navigateTo({
			url: '/pages/home/scanning?type='+type
		});
	//#endif
	//#ifdef MP-WEIXIN
		var current="";
		if (type == 1) {
			current = "b2c";
		} else {
			current = "o2o";
		}
		uni.scanCode({
			//onlyFromCamera: true,// 只允许通过相机扫码
			success: function(res) {
				Tool.post('uni/home/getProductByScanCode', {
					"barcode": res.result //商品69码
				}, function(res) {
					if (res.data.success) {
						uni.navigateTo({
							url: '/pages/list/list?keyword=' + res.data.data.name + '&plus=flase&current=' + current
						});
					} else {
						uni.showModal({
							title: '扫描结果',
							content: '商品不存在,是否重新扫描?',
							success(ret) {
								if (ret.confirm) {
									that.scanCode();
								}
							}
						});
						return;
					}
				});
			}
		});
	//#endif
},
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值