<script>
	// 平台检测和plus API兼容性处理
	let main = null;
	
	// 检查是否为App端，避免H5端调用plus API报错
	// #ifdef APP-PLUS
	if (typeof plus !== 'undefined' && plus.android) {
		main = plus.android.runtimeMainActivity();
		// ！！！为了保证UHF正常关闭下电，延迟退出APP！！！
		if (uni.getSystemInfoSync().platform == 'android') {
			plus.runtime.quit = function() {
				setTimeout(() => {
					main.finish()
				}, 10)
			};
		}
	}
	// #endif
	
	var hhwUHFController;
	export default {
		globalData: {
			// 原生插件只在App端可用，H5端需要做兼容处理
			hhwUHFController: null,
			globalEvent: null
		},
		onLaunch: function() {
			console.log('App Launch')
			
			// 初始化原生插件，只在App端有效
			// #ifdef APP-PLUS
			this.globalData.hhwUHFController = uni.requireNativePlugin('HL-HHWUHFController');
			this.globalData.globalEvent = uni.requireNativePlugin('globalEvent');
			// #endif
			
			hhwUHFController = this.globalData.hhwUHFController;
			const token = uni.getStorageSync('token');
			
			if (!token) {
				uni.reLaunch({ url: '/pages/login/login' });
				console.log('App Launch token1234',token)
			} else {
				uni.reLaunch({ url: '/pages/index/index' });
			}
		},
		onShow: function() {
			console.log('App Show')
			
			// UHF控制器只在App端有效，H5端跳过相关操作
			// #ifdef APP-PLUS
			if (hhwUHFController) {
				// 开启插件java接口的日志打印
				hhwUHFController.setDebuggable(true, result => {
					console.log("App Show", "setDebuggable: " + result);
				});
				// 初始化超高频（UHF），上电
				var enterTime = Date.now();
				var outTime = enterTime;
				hhwUHFController.initUhf(result => {
					outTime = Date.now();
					console.log("App Show", "initUHF: " + result, "cusTime: " + (outTime - enterTime));
					uni.showToast({
						title: '初始化成功',
						icon: "none",
						duration: 1000
					});
				});
			}
			// #endif
			
			// #ifdef H5
			console.log('H5端：跳过UHF初始化操作');
			// #endif
		},
		onHide: function() {
			console.log('App Hide')
			
			// UHF控制器只在App端有效，H5端跳过相关操作
			// #ifdef APP-PLUS
			if (hhwUHFController) {
				// 关闭超高频（UHF），下电
				hhwUHFController.closeUhf(result => {
					console.log("App Hide", "closeUhf: " + result);
					uni.showToast({
						title: '关闭成功',
						icon: "none",
						duration: 1000
					});
				});
			}
			// #endif
			
			// #ifdef H5
			console.log('H5端：跳过UHF关闭操作');
			// #endif
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>
