修复主页无法引入请求 api 并使用的问题

This commit is contained in:
Hooray Hu 2023-04-25 00:37:46 +08:00
parent 2b205bf5bd
commit 394694bc4c
4 changed files with 11 additions and 27 deletions

View File

@ -2,20 +2,8 @@ import axios from 'axios'
// import qs from 'qs'
import { ElMessage } from 'element-plus'
import router from '@/router/index'
import useUserStore from '@/store/modules/user'
function toLogin() {
useUserStore().logout().then(() => {
router.push({
path: '/login',
query: {
redirect: router.currentRoute.value.path !== '/login' ? router.currentRoute.value.fullPath : undefined,
},
})
})
}
const api = axios.create({
baseURL: (import.meta.env.DEV && import.meta.env.VITE_OPEN_PROXY === 'true') ? '/proxy/' : import.meta.env.VITE_APP_API_BASEURL,
timeout: 1000 * 60,
@ -58,7 +46,7 @@ api.interceptors.response.use(
}
}
else {
toLogin()
useUserStore().logout()
}
return Promise.resolve(response.data)
},

View File

@ -28,11 +28,7 @@ function userCommand(command: 'home' | 'setting' | 'hotkeys' | 'logout') {
eventBus.emit('global-hotkeys-intro-toggle')
break
case 'logout':
userStore.logout().then(() => {
router.push({
name: 'login',
})
})
userStore.logout()
break
}
}

View File

@ -1,5 +1,6 @@
import useRouteStore from './route'
import useMenuStore from './menu'
import router from '@/router'
import apiUser from '@/api/modules/user'
const useUserStore = defineStore(
@ -38,7 +39,7 @@ const useUserStore = defineStore(
failure_time.value = res.data.failure_time
}
// 登出
async function logout() {
async function logout(redirect = router.currentRoute.value.fullPath) {
localStorage.removeItem('account')
localStorage.removeItem('token')
localStorage.removeItem('failure_time')
@ -47,6 +48,12 @@ const useUserStore = defineStore(
failure_time.value = ''
routeStore.removeRoutes()
menuStore.setActived(0)
router.push({
name: 'login',
query: {
...(router.currentRoute.value.path !== '/' && router.currentRoute.value.name !== 'login' && { redirect }),
},
})
}
// 获取我的权限
async function getPermissions() {

View File

@ -52,14 +52,7 @@ function onSubmit() {
type: 'success',
message: '模拟修改成功,请重新登录',
})
userStore.logout().then(() => {
router.push({
name: 'login',
query: {
redirect: route.fullPath,
},
})
})
userStore.logout()
})
}
})