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.
 
 
 
 
 
bililive_webapp/src/router.ts

79 lines
2.6 KiB

import {createRouter, createWebHashHistory, useRoute} from "vue-router";
import Email from "./components/Email.vue";
import Config from "./components/Config.vue";
import {useStore} from "vuex";
import {onMounted} from "vue";
import Password from "./components/Password.vue";
import TelegramBot from "./components/TelegramBot.vue";
import Login from "./components/Login.vue";
import Register from "./components/Register.vue";
import {checkAdmin, JSONResponse} from "./request";
// @ts-ignore
import crumbs from 'crumbsjs';
import {token_cookie_key} from "./global";
const backend = '/backend'
export const registerPath = `${backend}/register`
export const loginPath = `${backend}/login`
export const configPath = `${backend}/config`
export const telegramPath = `${configPath}/telegram`
export const managerEmailPath = `${backend}/email`
export const managerPasswordPath = `${backend}/password`
const routes = [
{path: backend, redirect: configPath},
{path: registerPath, component: Register},
{path: configPath, component: Config},
{path: telegramPath, component: TelegramBot},
{path: managerEmailPath, component: Email},
{path: managerPasswordPath, component: Password},
{path: loginPath, component: Login}
]
export const router = createRouter({
history: createWebHashHistory(),
routes
})
//路由导航守卫
router.beforeEach(async (to, from) => {
if (crumbs.get(token_cookie_key)) {
if ([registerPath, loginPath].includes(to.fullPath)) {
console.info('存在cookie,访问的路由是登录或注册,重定向到主页')
return '/'
} else {
console.info('放通路由')
return true
}
} else {
let res: JSONResponse = await checkAdmin().then(res => res.json())
if (res.body) {
if (to.fullPath === loginPath) {
console.info('放通路由')
return true
} else {
console.info('存在管理员,访问的不是登录路由,重定向到登录')
return loginPath
}
} else {
if (to.fullPath === registerPath) {
console.info('放通路由')
return true
} else {
console.info('不存在管理员,访问的不是注册路由,重定向到注册')
return registerPath
}
}
}
})
//更新状态里的路由
export function updateRoute() {
const store = useStore()
const route = useRoute()
onMounted(() => {
store.commit('updateRoute', route.fullPath)
})
}