App.vue 955 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <script setup lang="ts">
  2. import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
  3. /**
  4. * 应用启动
  5. */
  6. onLaunch(() => {
  7. console.log("App Launch");
  8. checkLoginStatus();
  9. });
  10. /**
  11. * 应用显示
  12. */
  13. onShow(() => {
  14. console.log("App Show");
  15. });
  16. /**
  17. * 应用隐藏
  18. */
  19. onHide(() => {
  20. console.log("App Hide");
  21. });
  22. /**
  23. * 检查登录状态
  24. * 如果未登录且当前不在登录页,则跳转到登录页
  25. */
  26. const checkLoginStatus = () => {
  27. const token = uni.getStorageSync('haha-token-KuaiyuMan');
  28. // 获取当前页面路径
  29. const pages = getCurrentPages();
  30. const currentPage = pages[pages.length - 1];
  31. const currentPath = currentPage ? currentPage.route : '';
  32. // 如果没有token且不在登录页,跳转到登录页
  33. if (!token && currentPath !== 'pages/login/login') {
  34. console.log('未登录,跳转到登录页');
  35. uni.reLaunch({
  36. url: '/pages/login/login'
  37. });
  38. }
  39. };
  40. </script>
  41. <style></style>