89 lines
2.0 KiB
TypeScript
89 lines
2.0 KiB
TypeScript
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')
|
||
|