Files
violin-home/src/main.ts
T
2022-04-24 23:13:47 +08:00

66 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from "element-plus";
import 'element-plus/dist/index.css'
import router from './router'
// vue store
import { createPinia } from 'pinia'
const store = createPinia()
let app = createApp(App)
app.use(router)
app.use(ElementPlus)
app.use(store)
import { getToken, getUser } from './utils/auth'
import { User } from './entity/index'
const whiteList = ['/login']
let url = window.location.href
/**
* ルーター監視
* 1、トーケン取得
* 2、トーケンなし場合はログイン画面に遷移
* 3、トーケンあり場合:
*  既にログイン場合はデフォルト /  に遷移
*  
*/
router.beforeEach((to, from, next) => {
console.log(from.path, to.path)
const token = getToken()
if (typeof token === 'string') {
switch (to.path) {
case '/login':
next('/')
break;
case '/':
const user = <User>(JSON.parse(getUser() as string))
if (url.search("edit_blog") != -1) {
next('/BlogEditer')
} else {
router.push({
path: '/home',
query: user
})
}
break;
default:
next()
}
} else {
if (whiteList.includes(to.path)) {
next()
} else {
next('/login')
}
}
})
app.mount('#app')