App.vue 1.92 KB
<script setup>
import { onLaunch, onShow } from '@dcloudio/uni-app';

// 应用启动时初始化
onLaunch(() => {
  console.log('App Launch');
  // 清空登录重定向残留(可选)
  uni.removeStorageSync('loginRedirectPath');
});

// 监听所有页面显示(核心:检测登录状态)
onShow(() => {
  checkLoginStatus();
});

// 登录检测核心函数
const checkLoginStatus = () => {
  // 1. 获取当前页面信息(避免登录页循环跳转)
  const pages = getCurrentPages();
  if (pages.length === 0) return; // 无页面时跳过
  const currentPage = pages[pages.length - 1];
  const currentPath = currentPage.route; // 当前页面路径(如:pages/plan/index)

  // // 2. 白名单:无需登录的页面(登录页必须加,其他按需加)
  // const whiteList = ['pages/login/index', 'pages/register/index']; // 可追加首页等
  // if (whiteList.includes(currentPath)) return;

  // 3. 检测登录状态(仅判断token是否存在,极简版)
  const token = uni.getStorageSync('jcss_token'); // 登录成功后需存token
  if (!token) {
    // 记录当前页面(登录后可返回)
    uni.setStorageSync('loginRedirectPath', currentPath);
    // 跳转到登录页(用redirectTo避免返回栈残留)
    uni.redirectTo({
      url: '/pages/login/index'
    });
  }
};
</script>

<style lang="scss">
/* 注意要写在第一行,注意不能引入至uni.scss,同时给style标签加入lang="scss"属性 */
@import "@/uni_modules/uview-plus/index.scss";

/* 你的全局样式 */
page {
  background-color: #f8f8f8;
  height: 100%;
}

// 底部按钮样式
.fixed-bottom-btn-wrap {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 999;
  padding: 0;
  /* #ifdef MP-WEIXIN */
  //padding-bottom: constant(safe-area-inset-bottom);
  //padding-bottom: env(safe-area-inset-bottom);
  /* #endif */
}
.commonPageLRpadding{
  padding-left: 15px;
  padding-right: 15px;
}
</style>