小说网站前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.8 KiB

2 months ago
  1. <template>
  2. <div id="app" :style="{ backgroundColor: route.path.includes('chapter') ? '#e9e4d8' : '#f5f5f5' }">
  3. <AuthProvider>
  4. <AuthorApplicationProvider>
  5. <router-view />
  6. </AuthorApplicationProvider>
  7. </AuthProvider>
  8. </div>
  9. </template>
  10. <script>
  11. import { onMounted, watch } from 'vue';
  12. import { useRoute } from 'vue-router';
  13. import { routerEvents } from '@/router';
  14. import { useMainStore } from './store';
  15. import AuthProvider from '@/components/auth/AuthProvider.vue';
  16. import AuthorApplicationProvider from '@/components/auth/AuthorApplicationProvider.vue';
  17. export default {
  18. name: 'App',
  19. components: {
  20. AuthProvider,
  21. AuthorApplicationProvider
  22. },
  23. setup() {
  24. const route = useRoute();
  25. const store = useMainStore();
  26. // 立即初始化全局变量,确保路由守卫可以使用
  27. window.$store = store;
  28. window.routerEvents = routerEvents;
  29. // 监听路由变化,检查是否需要触发登录弹窗
  30. onMounted(async () => {
  31. try {
  32. // 立即初始化登录状态,确保在路由守卫执行前已经正确恢复用户状态
  33. // 现在会自动获取最新的用户信息
  34. await store.initializeAuth();
  35. // 如果有触发登录的事件,执行它
  36. if (typeof routerEvents.triggerLogin === 'function') {
  37. routerEvents.triggerLogin();
  38. routerEvents.triggerLogin = null;
  39. }
  40. // 输出调试信息
  41. if (window.$debug?.logUserState) {
  42. window.$debug.logUserState();
  43. }
  44. // 只有在用户已登录时才获取未读消息
  45. if (store.isLoggedIn) {
  46. store.getUnreadMessages();
  47. }
  48. } catch (error) {
  49. console.error('[App] 初始化失败:', error);
  50. }
  51. });
  52. // 每次路由变化时也检查一次
  53. watch(route, () => {
  54. if (typeof routerEvents.triggerLogin === 'function') {
  55. routerEvents.triggerLogin();
  56. routerEvents.triggerLogin = null;
  57. }
  58. });
  59. return {
  60. route
  61. }
  62. }
  63. };
  64. </script>
  65. <style lang="scss">
  66. @use '@/assets/styles/global.scss';
  67. html, body {
  68. margin: 0;
  69. padding: 0;
  70. height: 100%;
  71. font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;
  72. }
  73. #app {
  74. display: flex;
  75. flex-direction: column;
  76. min-height: 100vh;
  77. background-color: #f5f5f5;
  78. .main-content {
  79. flex: 1;
  80. padding: 20px;
  81. max-width: 1200px;
  82. margin: 0 auto;
  83. width: 100%;
  84. }
  85. }
  86. </style>