<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";

/**
 * 应用启动
 */
onLaunch(() => {
  console.log("App Launch");
  checkLoginStatus();
});

/**
 * 应用显示
 */
onShow(() => {
  console.log("App Show");
});

/**
 * 应用隐藏
 */
onHide(() => {
  console.log("App Hide");
});

/**
 * 检查登录状态
 * 如果未登录且当前不在登录页，则跳转到登录页
 */
const checkLoginStatus = () => {
  const token = uni.getStorageSync('haha-token-KuaiyuMan');
  
  // 获取当前页面路径
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const currentPath = currentPage ? currentPage.route : '';
  
  // 如果没有token且不在登录页，跳转到登录页
  if (!token && currentPath !== 'pages/login/login') {
    console.log('未登录，跳转到登录页');
    uni.reLaunch({
      url: '/pages/login/login'
    });
  }
};
</script>
<style></style>
