"test/vscode:/vscode.git/clone" did not exist on "15bf3d6297259f622514cf989b150f288c98d62b"
index.js 2.97 KB
Newer Older
LiangLiu's avatar
LiangLiu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Layout from '../views/Layout.vue'
import Generate from '../components/Generate.vue'
import Projects from '../components/Projects.vue'
import Inspirations from '../components/Inspirations.vue'
import Share from '../views/Share.vue'

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login', name: 'Login', component: Login, meta: { requiresAuth: false }
  },
  {
    path: '/share/:shareId', name: 'Share', component: Share, meta: { requiresAuth: false }
  },
  {
    path: '/home',
    component: Layout,
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: '/generate',
        name: 'Generate',
        component: Generate,
        meta: { requiresAuth: true },
        props: route => ({ query: route.query })
      },
      {
        path: '/projects',
        name: 'Projects',
        component: Projects,
        meta: { requiresAuth: true },
        props: route => ({ query: route.query })
      },
      {
        path: '/inspirations',
        name: 'Inspirations',
        component: Inspirations,
        meta: { requiresAuth: true },
        props: route => ({ query: route.query })
      },
      {
        path: '/task/:taskId',
        name: 'TaskDetail',
        component: Projects,
        meta: { requiresAuth: true },
        props: route => ({ taskId: route.params.taskId, query: route.query })
      },
      {
        path: '/template/:templateId',
        name: 'TemplateDetail',
        component: Inspirations,
        meta: { requiresAuth: true },
        props: route => ({ templateId: route.params.templateId, query: route.query })
      },
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/404.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
// router/index.js
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('accessToken')
  console.log('路由守卫 - token:', token)
  console.log('路由守卫 - to.path:', to.path)

  // 如果是不需要登录的页面,直接放行
  if (to.meta.requiresAuth === false) {
    console.log('不需要登录的页面,直接放行')
    // 如果已登录用户访问登录页面,重定向到生成页面
    if (token && to.path === '/login') {
      console.log('已登录用户访问登录页,重定向到生成页')
      next('/generate');
    } else {
      next();
    }
    return;
  }

  // 需要登录的页面
  if (!token) { // 未登录
    console.log('需要登录但未登录,跳转到登录页')
    next('/login');
  } else {
    console.log('已登录')
    if (to.path === '/') { // 已登录且在首页
      console.log('已登录且在首页,跳转到生成页')
      next('/generate');
    } else { // 已登录且不在首页
      console.log('已登录,放行')
      next();
    }
  }
})



export default router;