<script setup lang="ts">
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
import { launchInterceptor } from '@/utils/interceptor'
import { getToken, isWeixinBrowser, currRoute, deepClone, setThemeColor } from '@/utils/common'
import useMemberStore from '@/stores/member'
import useConfigStore from '@/stores/config'
import useSystemStore from '@/stores/system'
import { useLogin } from '@/hooks/useLogin'
import { useShare } from '@/hooks/useShare'

onLaunch((data: any) => {

    // 添加初始化拦截器
    launchInterceptor()
    const systemStore = useSystemStore()

    // 初始化全局数据
    const initGlobalData = () => {
        systemStore.systemInfo = uni.getSystemInfoSync();
        systemStore.getMenuButtonInfoFn()
    }

    initGlobalData()

    // #ifdef H5
    systemStore.systemInfo.platform == 'ios' && (uni.setStorageSync('initUrl', location.href))

    // 传输给后台数据
    window.parent.postMessage(JSON.stringify({
        type: 'appOnLaunch',
        message: '初始化加载完成'
    }), '*');

    // 监听父页面发来的消息
    window.addEventListener('message', event => {
        try {
            let data = {
                type: ''
            };
            if (typeof event.data == 'string') {
                data = JSON.parse(event.data)
            } else if (typeof event.data == 'object') {
                data = event.data
            }
            if (data.type && data.type == 'appOnReady') {
                window.parent.postMessage(JSON.stringify({
                    type: 'appOnReady',
                    message: '加载完成'
                }), '*');
                // 更新全局数据
                initGlobalData()
            }
        } catch (e) {
            console.log('uni-app App.vue 接受数据错误', e)
        }
    }, false);


    try {
        uni.hideTabBar() // 隐藏tabbar
    } catch (e) {

    }

    // const { wechatInit } = useShare()
    // wechatInit()
    // #endif

    // #ifdef MP
    const updateManager = uni.getUpdateManager();
    updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
    });

    updateManager.onUpdateReady(function (res) {
        uni.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success(res) {
                if (res.confirm) {
                    // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                    updateManager.applyUpdate();
                }
            }
        });
    });

    updateManager.onUpdateFailed(function (res) {
        // 新的版本下载失败
    });
    // #endif

    // 控制弹窗展示
    uni.setStorageSync('isOnLoad', true); // 存储是页面是否加载完成的状态
})

onShow(() => {

})

onHide(() => {
})
</script>

<style>
uni-page-head {
    display: none !important;
}
</style>
