Files
violin-home/src/main.ts
T
2022-08-26 00:25:34 +08:00

89 lines
2.0 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, setToken, setUser } from './utils/auth'
import { User } from './entity/index'
import Cookies from 'js-cookie'
import { obtainUserInfo } from './api/user'
const whiteList = ['/login']
let url = window.location.href
async function checktoken(url: string) {
if (url.search("token") != -1) {
let authorizeToken = url.split("token=")[1]
setToken(authorizeToken)
await obtainUserInfo(authorizeToken).then(response => {
setUser(response.data)
})
let base_url = url.split("?token=")[0]
window.open(base_url, "_self")
}
}
checktoken(url)
/**
* ルーター監視
* 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("write") != -1) {
next('/BlogEditer')
} else if (url.search("view") != -1) {
let bid = Cookies.get('bid')
router.push({
path: '/BlogViewer',
query: { bid: bid }
})
// next('/BlogViewer')
console.log(1)
} else {
router.push({
path: '/home',
query: user
})
}
break;
default:
next()
}
} else {
if (whiteList.includes(to.path)) {
next()
} else {
next('/login')
}
}
})
app.mount('#app')