<script setup>
import config from "./config";
import { getToken, setToken, removeToken } from "@/utils/auth";

import { defineComponent, getCurrentInstance, inject, nextTick, onMounted, watchEffect } from "vue";
import { onLoad, onShow, onHide, onLaunch, onReady } from "@dcloudio/uni-app";

let tab = inject("$tab");
const { proxy } = getCurrentInstance();

function initApp() {
  //初始化获取安全区高度
  proxy.$settingStore.systemHeightTop();
  //隐藏自带tabbar
  uni.hideTabBar();

  //#ifdef APP-PLUS
  plus.screen.lockOrientation("portrait-primary"); //设置不可横屏
  pushListener(); //开启消息推送监听
  proxy.$setting.clearBadge(0); // 清除数字
  //#endif
}

/**
 * @消息推送监听
 */
function pushMessage() {
  uni.onPushMessage((res) => {
    const platform = uni.getSystemInfoSync().platform;
    console.log("收到推送消息：", res); //监听推送消息

    if (res.type === "click") {
      if (platform == "android") {
      } else {
      }
    } else if (res.type === "receive") {
      if (platform == "android") {
      } else {
      }
    }
  });
}

/**
 * @消息推送监听
 */
function pushListener() {
  //获取应用的CID
  uni.getPushClientId({
    success: (res) => {
      console.log(res.cid);
    },
    fail(err) {
      console.log(err);
    },
  });

  const platform = uni.getSystemInfoSync().platform;
  // 点击推送信息
  plus.push.addEventListener("click", (res) => {
    console.log(res);
    if (res.aps) {
      if ("url" in res.payload) {
      } else {
      }
      console.log(res.payload);
    }
  });
  // 接收推送信息  在线
  plus.push.addEventListener("receive", (res) => {
    console.log(res);
    if (platform == "android") {
    } else {
      uni.createPushMessage({
        title: res.title,
        content: res.content,
      });
    }
  });
}

watchEffect(() => {
  //#ifdef APP-PLUS || MP-WEIXIN
  if (uni.getStorageSync("serveUrl")) {
    config.baseUrl = "http://" + uni.getStorageSync("serveUrl") + "/prod-api";
  }
  //#endif
});

onLaunch(() => {
  console.log("App Launch");

  //初始化默认主题
  if (!proxy.$settingStore.$state.themeColor) {
    proxy.$settingStore.SET_THEMECOLOR(proxy.$constData.themeList[0]);
  } else {
    proxy.$settingStore.SET_THEMECOLOR(proxy.$settingStore.$state.themeColor);
  }

  initApp();
});

onLoad(() => {});

onReady(() => {});

onShow(() => {
  console.log("App Show");
});

onHide(() => {
  console.log("App Hide");
});
</script>

<style lang="scss">
@import "@/static/scss/index.scss";
</style>

<style lang="scss">
@import "@/uni_modules/uview-plus/index.scss";

uni-page-body,
uni-page-refresh {
  height: 100%;
}

//默认返回按钮样式
:deep(.uni-page-head-btn) {
  background-color: rgba(0, 0, 0, 0) !important;
}

//默认头部导航title样式
:deep(.uni-page-head__title) {
  font-size: $uni-font-size-lg;
  font-weight: bold;
}

//uview-plus导航栏title样式
:deep(.u-navbar__content__title) {
  font-size: $uni-font-size-lg;
  font-weight: bold;
}

:deep(.u-nav-slot .u-navbar__content__title) {
  font-size: 18px;
}
</style>
