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

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