uni-app打包web2app的扫码功能(h5)

<template>
</template>

<script>
	export default {
	  data() {
	   return {
	   		barcode:null,//创建扫一扫对象
	   		result:'',//扫描结果
	   		setFlash:false,//是否开启闪光灯
	   	}
	  },
	  onLoad() {
	  	let _this = this;
	  	_this.barcode = null;
	  	if(!_this.barcode){
	  		// 创建Barcode扫码控件,此方法创建扫码识别控件并不会显示在页面中,需要调用plus.webview.Webview窗口对象的append方法将其添加到Webview窗口中才能显示
	  		let top ='44px';
	  	// #ifdef APP-PLUS
	  		top = uni.getSystemInfoSync().statusBarHeight + 44 +'px'
	  		// #endif
	  		_this.barcode = plus.barcode.create('barcode', [plus.barcode.QR], {
	  			top:top ,
	  			left:'0',
	  			width: '100%',
	  			height: '100%',
	  			position: 'static'
	  		});
	  		plus.webview.currentWebview().append(_this.barcode);
	  		_this.barcode.onmarked = onmarked;
	  	}
	  	_this.barcode.start();//开启扫一扫
	  	
	  	//获取扫描结果
	  	function onmarked(type, result) {
	  		_this.result = result;
	  		var text = '未知: ';
	  		switch(type){
	  			case plus.barcode.QR:
	  			text = 'QR: ';
	  			break;
	  			case plus.barcode.EAN13:
	  			text = 'EAN13: ';
	  			break;
	  			case plus.barcode.EAN8:
	  			text = 'EAN8: ';
	  			break;
	  		}
	  		_this.barcode.cancel();//结束对摄像头获取图片数据进行条码识别操作,同时关闭摄像头的视频捕获。 结束后可调用start方法重新开始识别。
	  		_this.barcode.close();//释放控件占用系统资源,调用close方法后控件对象将不可使用。
	  		alert(result)
	  	}
	  },
	  methods: {
	  	//页面跳转
	  	goBack(name) {
	  		if (name == 1) {
	  			this.barcode.cancel();//结束对摄像头获取图片数据进行条码识别操作,同时关闭摄像头的视频捕获。 结束后可调用start方法重新开始识别。
	  			this.barcode.close();//释放控件占用系统资源,调用close方法后控件对象将不可使用。
	  			if(this.result){
	  				uni.reLaunch({
	  					url: '/pages/login/login?result='+this.result,
	  					animationType: 'pop-in',
	  					animationDuration: 300
	  				})
	  			}else{
	  				uni.navigateBack({
	  					delta: name,
	  					animationType: 'pop-out',
	  					animationDuration: 300
	  				});
	  			}
	  			
	  		} else {
	  			uni.navigateTo({
	  				url: name,
	  				animationType: 'pop-in',
	  				animationDuration: 300
	  			})
	  		}
	  	},
	  }
	}
</script>
<template>
	<button @click="goBack()">扫一扫开启</button>
</template>

<script>
	export default {
	  data() {
	    return {
	      url: "https://test.h5yw.dlsf-tech.com" // 是否正在扫一扫
	    }
	  },
	  onLoad() {
	  	
	  },
	  methods:{
		  goBack:function(){
			  uni.navigateTo({
			  	url:'/pages/index/scan'
			  })
		  }
	  }
	}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值