index.js 3.1 KB
Newer Older
1 2 3 4 5 6 7 8
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'

9
import Pic from '@/components/menus/MyPic.vue'
10
import Chat from '@/components/menus/MyChat.vue'
11
import Interview from '@/components/menus/MyInterview.vue'
12
import Algorithmic from '@/components/menus/MyAlgorithmic.vue'
13
import AphorismPoetry from '@/components/menus/MyAphorismPoetry.vue'
14
import MyCsdnUser from '@/components/menus/MyCsdnUser.vue'
15
import MyArticleInfo from '@/components/menus/MyArticleInfo.vue'
16
import MyTripletDayInfo from '@/components/menus/MyTripletDayInfo.vue'
17
import MyChatDetail from '@/components/user/MyChatDetail.vue'
18
import AddChatDetail from '@/components/user/AddChatDetail.vue'
19

20 21 22 23 24

Vue.use(VueRouter)

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

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

export default router