管理和服务分离
This commit is contained in:
+24
-13
@@ -3,20 +3,18 @@ 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 } from './utils/auth'
|
||||
const whiteList = ['/', '/blog']
|
||||
import { getToken, getUser } from './utils/auth'
|
||||
import { User } from './entity/index'
|
||||
const whiteList = ['/login']
|
||||
let url = window.location.href
|
||||
console.log(url)
|
||||
if (url != 'http://localhost:3000/') {
|
||||
@@ -44,21 +42,34 @@ router.beforeEach((to, from, next) => {
|
||||
console.log(from.path, to.path)
|
||||
|
||||
const token = getToken()
|
||||
if (to.path === '/login') {
|
||||
if (token) {
|
||||
next('/home')
|
||||
if (typeof token === 'string') {
|
||||
if (to.path === '/login') {
|
||||
if (token) {
|
||||
next('/')
|
||||
}
|
||||
} else if (to.path === '/') {
|
||||
const user = <User>(JSON.parse(getUser() as string))
|
||||
console.log(user)
|
||||
if (user) {
|
||||
router.push({
|
||||
path: '/home',
|
||||
query: user
|
||||
})
|
||||
}
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (whiteList.includes(to.path)) {
|
||||
next()
|
||||
} else {
|
||||
next()
|
||||
if (whiteList.includes(to.path)) {
|
||||
next()
|
||||
} else {
|
||||
next('/login')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user