index.js 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
import Vue from 'vue'
import VueRouter from 'vue-router'
import pathArr from '@/router/pathArr.js'

// 导入需要的组件
import Login from '@/components/MyLogin.vue'
import Home from '@/components/MyHome.vue'

import Users from '@/components/menus/MyUsers.vue'
10
import Pic from '@/components/menus/MyPic.vue'
11
import Chat from '@/components/menus/MyChat.vue'
12
import Interview from '@/components/menus/MyInterview.vue'
13
import Algorithmic from '@/components/menus/MyAlgorithmic.vue'
14
import AphorismPoetry from '@/components/menus/MyAphorismPoetry.vue'
15
import UserDetail from '@/components/user/MyUserDetail.vue'
16
import MyChatDetail from '@/components/user/MyChatDetail.vue'
17
import AddChatDetail from '@/components/user/AddChatDetail.vue'
18

19 20 21 22 23

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
24 25 26 27
    {
      path: '/',
      redirect: '/login'
    },
28
    // 登录的路由规则
29 30
    {
      path: '/login',
31 32 33 34
      component: Login,
      meta: {
        title: '登录' // 设置默认标题
      }
35
    },
36 37 38 39
    // 后台主页的路由规则
    {
      path: '/home',
      component: Home,
40
      redirect: '/home/interview',
41
      children: [
42
        {
43
          path: 'interview',
44 45 46 47
          component: Interview,
          meta: {
            title: '面试题' // 设置默认标题
          }
48 49
        },
        {
50
          path: 'algorithmic',
51 52 53 54
          component: Algorithmic,
          meta: {
            title: '算法题' // 设置默认标题
          }
55 56
        },
        {
57
          path: 'aphorismpoetry',
58 59 60 61
          component: AphorismPoetry,
          meta: {
            title: '名言警句' // 设置默认标题
          }
62 63
        },
        {
64
          path: 'chat',
65 66 67 68 69
          component: Chat,
          meta: {
            title: 'chatgpt记录' // 设置默认标题
          }
        }, {
70
          path: 'pic',
71 72 73 74 75
          component: Pic,
          meta: {
            title: '图片' // 设置默认标题
          }
        }, {
76
          path: 'users',
77 78 79 80 81
          component: Users,
          meta: {
            title: '用户管理' // 设置默认标题
          }
        },
82
        // 用户详情页的路由规则
83 84 85
        {
          path: 'userinfo/:id',
          component: UserDetail,
86 87 88 89
          props: true,
          meta: {
            title: '用户信息' // 设置默认标题
          }
90 91 92
        }, {
          path: 'chatinfo/:id',
          component: MyChatDetail,
93 94 95 96
          props: true,
          meta: {
            title: 'chat信息' // 设置默认标题
          }
97 98
        }, {
          path: 'addChat',
99 100 101 102
          component: AddChatDetail,
          meta: {
            title: '添加chat信息' // 设置默认标题
          }
103
        },
104 105 106 107 108 109
      ]
    }
  ]
})

// 全局前置守卫
110
router.beforeEach(function (to, from, next) {
111 112 113 114 115 116 117 118 119 120 121 122 123
  if (pathArr.indexOf(to.path) !== -1) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router