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

68 lines
2.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 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";
export const registerPath = '/register'
export const loginPath = '/login'
const routes = [
{path: '/', redirect: '/config'},
{path: registerPath, component: Register},
{path: '/config', component: Config},
{path: '/config/telegram', component: TelegramBot},
{path: '/email', component: Email},
{path: '/password', component: Password},
{path: loginPath, component: Login}
]
export const router = createRouter({
history: createWebHashHistory(),
routes
})
//路由导航守卫
router.beforeEach(async (to, from) => {
let res: JSONResponse = await checkAdmin().then(res => res.json())
if (![registerPath, loginPath].includes(to.fullPath)) {
if (res.body && crumbs.get(token_cookie_key)) {
return true
} else if (res.body) {
alert('请登录')
return loginPath
} else {
alert('请进行应用初始化操作')
return registerPath
}
} else if (to.fullPath === loginPath && crumbs.get(token_cookie_key)) {
return '/'
} else if (to.fullPath === loginPath && !res.body) {
return registerPath
} else if (to.fullPath === registerPath && res.body) {
return loginPath
} else {
return true
}
})
//更新状态里的路由
export function updateRoute() {
const store = useStore()
const route = useRoute()
onMounted(() => {
store.commit('updateRoute', route.fullPath)
})
}