<template>
	<div>
			<button type="primary" @tap='onAdd' class="span-10 font-md px-0"><view class="iconfont icon-tianjia1 font-md mr d-inline-block"></view>添加物品</button>
		
		<button type="primary" @click="initScanFunc">初始化扫描</button>
		<br/>
		<button type="primary" @click="closeScanFunc">结束扫描</button>
		<br/>
		<button type="primary" @click="scanStartFunc()">开启扫描</button>
		<br/>
		<button type="primary" @click="scanStopFunc()">停止扫描</button>
		<br/>
		<p>设置连续扫描时间间隔</p><input type="number" v-model="invTime" placeholder="时间间隔(单位ms)">
		<button type="primary" @click="continueScanFunc(true)">连续扫描开启</button>
		<br/>
		<button type="primary" @click="continueScanFunc(false)">连续扫描关闭</button>

		<text id="testcontent" class="intro">\n测试步骤：\n1.点击初始化扫描\n2.按设备黄色扫描按键扫描到条码后弹窗提示条码数据\n3.页面关闭前点击结束扫描或调用closeScan()\n4.如需使用连续扫描功能，请点击连续扫描开启或关闭按钮，建议设置连续扫描时间间隔</text>
	</div>
</template>

<script>
	//获取module
	var barcodeModel = uni.requireNativePlugin("iData-BarcodePlugin-BarcodeModule")
	const modal = uni.requireNativePlugin('modal');

	var globalEvent = uni.requireNativePlugin('globalEvent');
	
	export default {
		onLoad() {
			globalEvent.addEventListener('iDataBarcodeEvent', function(e) {
				modal.toast({
					message: "收到条码：" + JSON.stringify(e),
					duration: 1.5
				});
				//连续扫描如果间隔时间比较短，会出现toast不提示的情况，数据可以正常接收到，建议查看控制台输出
				console.log(JSON.stringify(e));
			});
		},
		onUnload() {
			barcodeModel.closeScan((ret) => {
				modal.toast({
					message: ret,
					duration: 1.5
				});
			});
		},
		data(){
		        return{
		            invTime:"500"
		        }
		},
		methods: {
			initScanFunc() {
				barcodeModel.initScan((ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
			},
			scanStartFunc() {
				barcodeModel.scanStart((ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
			},
			scanStopFunc() {
				barcodeModel.scanStop((ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
			},
			continueScanFunc(enable) {
				// 连续扫描设置：先设置扫描间隔时间，然后开启扫描开关
				if(enable){
					//设置连续扫描间隔时间
					barcodeModel.intervalSet(this.invTime,
						(ret) => {
							modal.toast({
								message: ret,
								duration: 1.5
							});
						});
				}
					
				//设置连续扫描
				barcodeModel.continueScan(enable,
					(ret) => {
						modal.toast({
							message: ret,
							duration: 1.5
						});
					});
			},
			closeScanFunc() {
				barcodeModel.closeScan((ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
			}
		}
	}
	
	
</script>

<style>

</style>
