<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 pollingActive = uni.getStorageSync('shoppingPollingActive');
  if (pollingActive === 'true') {
    console.log('应用隐藏，清理购物页面轮询状态');
    uni.removeStorageSync('shoppingPollingActive');
  }
});

/**
 * 检查登录状态
 * 如果未登录且当前不在登录页，则跳转到登录页
 */
const checkLoginStatus = () => {
  const token = uni.getStorageSync('accessToken');

  // 获取当前页面路径
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const currentPath = currentPage ? currentPage.route : '';

  console.log('[App] 检查登录状态 - token:', token ? '已存在' : '不存在', ', 当前页面:', currentPath);

  // 如果没有token且不在登录页，跳转到登录页
  if (!token && currentPath !== 'pages/login/login') {
    console.log('[App] 未登录，跳转到登录页');
    uni.reLaunch({
      url: '/pages/login/login'
    });
  }
};
</script>
<style>
/* 全局动画样式 */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(40rpx); } to { opacity: 1; transform: translateY(0); } }
@keyframes slideDown { from { opacity: 0; transform: translateY(-40rpx); } to { opacity: 1; transform: translateY(0); } }
@keyframes scaleIn { from { opacity: 0; transform: scale(0.9); } to { opacity: 1; transform: scale(1); } }
@keyframes pulse { 0%, 100% { transform: scale(1); opacity: 0.3; } 50% { transform: scale(1.05); opacity: 0.5; } }
@keyframes bounce { 0% { transform: scale(0); opacity: 0; } 50% { transform: scale(1.1); } 100% { transform: scale(1); opacity: 1; } }
@keyframes skeleton { 0%, 100% { background-color: #F5F5F5; } 50% { background-color: #EEEEEE; } }
</style>
