<script setup lang="ts">
	import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
	import { useStore } from 'vuex'
	import { reactive } from 'vue'
	import WebSocketClass from '@/utils/webSocket'
	import { VUE_APP_WS_URL } from '@/config/app'
	import { autoLoad } from '@/utils/autoload'
	import { clickNavigateTo } from '@/utils/helper'
	import { checkVersion } from '@/utils/version'


	const store = useStore()
	const globalData = reactive({
		socketObj: null
	})
	onLaunch(() => {
		// #ifdef APP
		uni.getSystemInfoSyncX = () => {
			const result = uni.getSystemInfoSync();
			return {
				...result
			};
		}
		// #endif
		// #ifdef H5
		let statusBarHeight = 0;
		window.initPlusReadyCallback?.(() => {
			statusBarHeight = plus.navigator.getStatusbarHeight();
		});
		uni.getSystemInfoSyncX = () => {
			const result = uni.getSystemInfoSync();
			return {
				...result,
				statusBarHeight
			};
		}
		// #endif
		store.commit('init')
		//#ifdef APP-PLUS
		checkVersion()
		//#endif
		if (store.state.app.isLogin) {
			autoLoad()
			//#ifdef APP-PLUS
			plus.navigator.closeSplashscreen();
			addEventListener()
			//#endif
			// 鍙姞杞戒竴娆�
		} else {
			// #ifdef APP-PLUS 
			uni.reLaunch({
				url: "/pages/users/login/index",
				success: () => {
					//璺宠浆瀹岄〉闈㈠悗鍐嶅叧闂惎鍔ㄩ〉
					plus.navigator.closeSplashscreen();
				}
			})
			//#endif
		}
	})
	onShow(() => {
		if (store.state.app.isLogin) {
			// 濡傛灉宸茬粡鏈塻ockt瀹炰緥
			if (globalData.socketObj) {
				// 濡傛灉sockt瀹炰緥鏈繛鎺�
				if (!globalData.socketObj.isConnect) {
					globalData.socketObj.initSocket()
				}
			} else {
				let token = store.state.app.token
				let wsUrl = `${VUE_APP_WS_URL}/ws?type=ent&token=${token}`
				globalData.socketObj = new WebSocketClass(wsUrl, 10)
				globalData.socketObj.initSocket()
			}
		}
		// #ifdef APP-PLUS 
		setTimeout(() => {
			plus.navigator.closeSplashscreen();
		}, 2000)
		// #endif

	})
	onHide(() => { })

	// 娑堟伅鎺ㄩ€侀€昏緫
	const addEventListener = () : void => {
		/* 5+  push 娑堟伅鎺ㄩ€� ps:浣跨敤:H5+鐨勬柟寮忕洃鍚紝瀹炵幇鎺ㄩ€�*/
		let info = plus.push.getClientInfo();
		let cid = info.clientid;
		plus.push.addEventListener(
			'click',
			function (msg) {
				clickNavigateTo(sJSON.parse(JSON.parse(msg.payload)).url);
			},
			false
		);

		plus.push.addEventListener(
			'receive',
			function (msg) {
				if (msg.aps) {
					try {
						let options = {
							cover: false,
							sound: 'system',
							title: msg.payload.title
						};
						plus.push.createMessage(msg.payload.body, 'LocalMSG', options);
					} catch (e) {
						console.log(e);
					}
				} else if (msg.payload === 'LocalMSG') { } else if (msg.payload.type === 'url') {
					clickNavigateTo(msg.payload.url)
				} else {
					try {
						let options = {
							cover: false,
							sound: 'system',
							title: msg.payload.title
						};
						plus.push.createMessage(msg.payload.body, 'LocalMSG', options);
					} catch (e) {
						console.log(e);
					}
				}
			},
			false
		);
	}
</script>

<style lang="scss">
	@import url("@/static/css/base.css");
	@import url("@/static/iconfont/iconfont.css");
	//@import '@/static/uni.scss';
	@import '@/static/css/style.scss';
	/*姣忎釜椤甸潰鍏叡css */
</style>