<template>
	<div>
		<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>
		<br/>
		<button type="primary" @click="setCenterModeFunc(0)">区域解码</button>
		<br/>
		<button type="primary" @click="setCenterModeFunc(1)">中心解码</button>
		<br/>
		<p>设置扫描输出模式</p>
		<button type="primary" @click="setOutputModeFunc(0)">焦点</button>
		<button type="primary" @click="setOutputModeFunc(1)">广播</button>
		<button type="primary" @click="setOutputModeFunc(2)">模拟按键</button>
		<p>部分旧版扫描软件不支持以下模式，设置后会导致软件异常</p>
		<button type="primary" @click="setOutputModeFunc(3)">复制</button>
		<button type="primary" @click="setOutputModeFunc(4)">焦点&广播</button>
		<br/>
		<button type="primary" @click="setLockScanKeyFunc()">启用扫码功能</button>
		<br/>
		<button type="primary" @click="setUnLockScanKeyFunc()">禁用扫码功能</button>
		<br/>
		<button type="primary" @click="setMultiBarcodeEnableFunc()">启用多条码识别功能</button>
		<br/>
		<button type="primary" @click="setMultiBarcodeDisableFunc()">禁用多条码识别功能</button>
		<br/>
		<p>设置多条码识别数量</p><input type="number" v-model="mulNumber" placeholder="个数">
		<button type="primary" @click="setMultiBarcodePreciseStatusEnableFunc()">启用多条码精准识别功能</button>
		<br/>
		<button type="primary" @click="setMultiBarcodePreciseStatusDisableFunc()">禁用多条码精准识别功能</button>
		<br/>
		
		<button type="primary" @click="goTestFunc()">打开新页面</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) {
				console.log("收到条码：" + JSON.stringify(e));
				modal.toast({
					message: "收到条码：" + JSON.stringify(e),
					duration: 1.5
				});
				//连续扫描如果间隔时间比较短，会出现toast不提示的情况，数据可以正常接收到，建议查看控制台输出
				console.log(JSON.stringify(e));
			});
			barcodeModel.initScan((ret) => {
				modal.toast({
					message: ret,
					duration: 1.5
				});
			});
		},
		onUnload() {
			barcodeModel.closeScan((ret) => {
				modal.toast({
					message: ret,
					duration: 1.5
				});
			});
			globalEvent.removeEventListener('iDataBarcodeEvent');
		},
		data(){
		        return{
		            invTime:"500",
					mulNumber:"3"
		        }
		},
		methods: {
			goTestFunc(){
				uni.navigateTo({
					url: './scantest2'
				});
			},
			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
					});
				});
			},
			setCenterModeFunc(mode) {
				// 区域/中心扫码设置
				barcodeModel.setCenterMode(mode,
					(ret) => {
						modal.toast({
							message: ret,
							duration: 1.5
						});
					});
			},
			setOutputModeFunc(mode) {
				// 扫描输出模式设置
				barcodeModel.setOutputMode(mode,
					(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
					});
				});
				globalEvent.removeEventListener('iDataBarcodeEvent');
			},
			setLockScanKeyFunc() {
				//启用扫码按键扫码功能，启用后按扫描键会自动触发扫码功能
				barcodeModel.setScanKeyEnable(true,(ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
			},
			setUnLockScanKeyFunc() {
				//禁用扫码按键扫码功能，禁用后按扫描键不会自动触发扫码功能，支持自行监听按键触发功能
				barcodeModel.setScanKeyEnable(false,(ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
			},
			setMultiBarcodeEnableFunc() {
				//启用多条码识别功能，启用后按扫描键会一次性识别多个条码并返回对应的条码数据，每个数据一行，分隔符为'\n'
				barcodeModel.setMultiBarcodeEnable(true,(ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
			},
			setMultiBarcodeDisableFunc() {
				//禁用多条码识别功能，禁用后按扫描键不会识别多个条码，仅识别一个条码
				barcodeModel.setMultiBarcodeEnable(false,(ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
			},
			setMultiBarcodePreciseStatusEnableFunc() {
				//启用多条码精准模式，配合多条码数据接口，识别到配置的多条码数量的条码后才会返回条码数据
				barcodeModel.setMultiBarcodePreciseStatus(true,(ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
				barcodeModel.setMultiBarcodeNumber(this.mulNumber,(ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
			},
			setMultiBarcodePreciseStatusDisableFunc() {
				//禁用多条码精准模式
				barcodeModel.setMultiBarcodePreciseStatus(false,(ret) => {
					modal.toast({
						message: ret,
						duration: 1.5
					});
				});
			}
		}
	}
	
	
</script>

<style>

</style>
