<script>
	var main, receiver, filter;
	var _codeQueryTag = false;
export default {
	onLaunch(options) {
		// #ifdef APP-PLUS
		// 重置app更新信息
		uni.setStorageSync('update', false);
		uni.setStorageSync('downloadApkParams', null);
		console.log('onLaunch')
		// 检测客户端更新
		this.$store.dispatch('update/update', 0)
		// 初始化蓝牙
		this.$store.dispatch('ble/initBle')
		// #endif
	},
	onShow() {
		// #ifdef APP-PLUS
		this.initScan();
		this.startScan();
		this.setBROADCAST()
		this.setBROADCASTACTION()
		// #endif
		// getApp().globalData.uploadUrl = this.$UPLOAD_URL
		// getApp().globalData.img_url = this.$IMG_URL
	},
	onHide() {
		this.stopScan();
	},
	// 应用退出，断开连接
	onExit(){
		// #ifdef APP-PLUS
		this.$store.dispatch('ble/closeBluetooth')
		// #endif
	},
	// globalData: {
	// 	uploadUrl: '',
	// 	img_url: ''
	// },
	methods: {
		setBROADCAST() {
			var main2 = plus.android.runtimeMainActivity(); //获取acitivity
			var Intent = plus.android.importClass("android.content.Intent");
			var intent2 = new Intent("com.android.scanner.service_settings");
			intent2.putExtra("barcode_send_mode", "BROADCAST");
			main2.sendBroadcast(intent2);
		},
		setBROADCASTACTION() {
			var main2 = plus.android.runtimeMainActivity(); //获取acitivity
			var Intent = plus.android.importClass("android.content.Intent");
			var intent2 = new Intent("com.android.scanner.service_settings");
			intent2.putExtra("action_barcode_broadcast", "com.pdapengyue.scan");
			main2.sendBroadcast(intent2);
		},
		// 监听设备按键
		key() {
			plus.key.addEventListener('keydown', event => {
				console.log("按键：" + event.keyCode);
			});
		},
		// 点击触发激光扫码
		ScanCode() {
			var main2 = plus.android.runtimeMainActivity(); //获取acitivity
			var Intent = plus.android.importClass("android.content.Intent");
			var intent2 = new Intent("com.scan.onStartScan");
			main2.sendBroadcast(intent2);
		},
		initScan() {
			var vendor = plus.device.vendor; // 获取设备制造商
			console.log("厂商："+vendor);
			let _this = this;
			main = plus.android.runtimeMainActivity(); //获取activity  
			var IntentFilter = plus.android.importClass('android.content.IntentFilter');
			filter = new IntentFilter();
			filter.addAction("com.pdapengyue.scan"); // 换你的广播动作  
			receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
				onReceive: function(context, intent) {
					plus.android.importClass(intent);
					let data = intent.getStringExtra("pdapengyuescannerdata"); // 换你的广播标签  
					// console.log("条码信息：" + data)
					uni.$emit('scanFinish',uni.$u.trim(data))
					// uni.showModal({
					// 	title: '收到条码信息',
					// 	content: '' + data,
					// 	success: function(res) {
					// 		if (res.confirm) {
					// 			// 执行确认后的操作
					// 		} else {
					// 			// 执行取消后的操作
					// 		}
					// 	}
					// })
					_this.queryCode(code);
				}
			});
		},
		startScan() {
			main.registerReceiver(receiver, filter);
		
		},
		stopScan() {
			main.unregisterReceiver(receiver);
		},
		queryCode(code) {
			//防重复  
			if (_codeQueryTag) return false;
			_codeQueryTag = true;
			setTimeout(function() {
				_codeQueryTag = false;
			}, 150);
			var id = code
			uni.$emit('scancodedate', {
				code: id
			})
		}
	}
}
</script>

<style lang="scss">
/*每个页面公共css */
@import '@/uni_modules/uview-ui/index.scss';
/* #ifndef APP-NVUE */
@import '@/common/css/public.scss';
/* #endif */
</style>
