<script setup lang="ts">
import {onHide, onLaunch, onShow} from "@dcloudio/uni-app";
import {getCurrentInstance} from "vue";
import {useSysStore} from "@/store/sysStore";
import Auth from "@/api/Auth";
import {useWsStore} from "@/store/WsStore";
import app_upgrade from '@/uni_modules/app-upgrade/js_sdk/index.js'
import packageJson from '../package.json'
import update from '../update.json'

const wsStore = useWsStore();
onLaunch(() => {
  uni.getSystemInfo({
    success: function (e) {
      //@ts-ignore
      const {proxy} = getCurrentInstance();
      // #ifndef MP
      proxy.statusBar = e.statusBarHeight;
      if (e.platform == 'android') {
        //@ts-ignore
        proxy.customBar = e.statusBarHeight + 50;
      } else {
        //@ts-ignore
        proxy.customBar = e.statusBarHeight + 45;
      }
      ;
      // #endif

      // #ifdef MP-WEIXIN
      proxy.statusBar = e.statusBarHeight;
      //@ts-ignore
      let custom = wx.getMenuButtonBoundingClientRect();
      proxy.Custom = custom;
      //@ts-ignore
      proxy.customBar = custom.bottom + custom.top - e.statusBarHeight;
      // #endif

      // #ifdef MP-ALIPAY
      proxy.statusBar = e.statusBarHeight;
      //@ts-ignore
      proxy.customBar = e.statusBarHeight + e.titleBarHeight;
      // #endif
      // #ifdef APP-PLUS
      //自动更新 安卓
      if (e.platform === 'android') {
        app_upgrade(async () => {
          //查询是否更新
          const {statusCode, data} = await uni.request({
            url: update.checkUrl,
            method: 'GET'
          })
          const version = packageJson.version
          if (statusCode === 200) {
            return {
              changelog: data.changelog,
              status: data.android > version ? 1 : 0, // 0 无新版本 | 1 有新版本
              path: data.url // 新apk地址
            }
          }
        })
      }
      // #endif
    }
  })
});
onShow(() => {
  if (Auth.getToken()) {
    if (wsStore.wsRequest.socket?.readyState === 1) {
      wsStore.checkStatus()
    } else {
      wsStore.close(true)
      wsStore.init()
    }
  }
  useSysStore().setShow(true)
});
onHide(() => {
  useSysStore().setShow(false)
});

</script>
<style>
@import "colorui/main.css";
@import "colorui/icon.css";
@import "static/css/font.css";

page {
  height: 100%;
}
.tab-bar-mar {
  margin-bottom: 100upx
}
</style>
