mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-10-09 03:06:37 +08:00
优化登陆态
This commit is contained in:
@@ -1,5 +1,24 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
|
// 处理token失效的逻辑
|
||||||
|
function handleTokenExpired() {
|
||||||
|
// 清除localStorage中的token
|
||||||
|
window.localStorage.removeItem('token');
|
||||||
|
|
||||||
|
// 跳转到登录页面
|
||||||
|
if (window.location.pathname !== '/login') {
|
||||||
|
window.location.href = '/login';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查响应是否为token失效
|
||||||
|
function isTokenExpired(response) {
|
||||||
|
return response && response.code === 401 &&
|
||||||
|
(response.msg === '未登录或token已过期' ||
|
||||||
|
response.msg === '无效的token或token已过期' ||
|
||||||
|
response.msg === '无法获取用户权限信息');
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
get: function(path = '', data = {}) {
|
get: function(path = '', data = {}) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
@@ -11,10 +30,22 @@ export default {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
|
// 检查是否token失效
|
||||||
|
if (isTokenExpired(response.data)) {
|
||||||
|
handleTokenExpired();
|
||||||
|
return;
|
||||||
|
}
|
||||||
resolve(response.data);
|
resolve(response.data);
|
||||||
})
|
})
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
console.error('GET请求错误:', error);
|
console.error('GET请求错误:', error);
|
||||||
|
|
||||||
|
// 检查是否是401错误(token失效)
|
||||||
|
if (error.response && error.response.status === 401) {
|
||||||
|
handleTokenExpired();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
resolve({"code": -1, "msg": error.message || "网络请求失败"});
|
resolve({"code": -1, "msg": error.message || "网络请求失败"});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -29,10 +60,22 @@ export default {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
|
// 检查是否token失效
|
||||||
|
if (isTokenExpired(response.data)) {
|
||||||
|
handleTokenExpired();
|
||||||
|
return;
|
||||||
|
}
|
||||||
resolve(response.data);
|
resolve(response.data);
|
||||||
})
|
})
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
console.error('POST请求错误:', error);
|
console.error('POST请求错误:', error);
|
||||||
|
|
||||||
|
// 检查是否是401错误(token失效)
|
||||||
|
if (error.response && error.response.status === 401) {
|
||||||
|
handleTokenExpired();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
resolve({"code": -1, "msg": error.message || "网络请求失败"});
|
resolve({"code": -1, "msg": error.message || "网络请求失败"});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import Vue from 'vue';
|
import Vue from 'vue';
|
||||||
import VueRouter from 'vue-router';
|
import VueRouter from 'vue-router';
|
||||||
|
import { isLoggedIn } from '@/utils/auth';
|
||||||
|
|
||||||
// 路由懒加载
|
// 路由懒加载
|
||||||
|
|
||||||
@@ -18,7 +19,13 @@ Vue.use(VueRouter);
|
|||||||
// 路由配置
|
// 路由配置
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
{path: '/', redirect: '/login'},
|
{
|
||||||
|
path: '/',
|
||||||
|
redirect: () => {
|
||||||
|
// 如果用户已登录,重定向到首页,否则重定向到登录页
|
||||||
|
return isLoggedIn() ? '/index' : '/login';
|
||||||
|
}
|
||||||
|
},
|
||||||
{path: '/login', component: Login},
|
{path: '/login', component: Login},
|
||||||
{path: '/change-password', component: ChangeAccountPassword},
|
{path: '/change-password', component: ChangeAccountPassword},
|
||||||
{
|
{
|
||||||
@@ -40,12 +47,26 @@ const router = new VueRouter({
|
|||||||
})
|
})
|
||||||
|
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
if (to.path === '/login' || to.path === '/change-password') return next();
|
// 检查用户是否已登录
|
||||||
const tokenStr = localStorage.getItem('token')
|
const userIsLoggedIn = isLoggedIn();
|
||||||
if (!tokenStr) {
|
|
||||||
return next('/login')
|
// 如果用户已登录且访问登录页面,直接跳转到首页
|
||||||
|
if (userIsLoggedIn && to.path === '/login') {
|
||||||
|
return next('/index');
|
||||||
}
|
}
|
||||||
next()
|
|
||||||
|
// 如果访问登录或修改密码页面,直接放行
|
||||||
|
if (to.path === '/login' || to.path === '/change-password') {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果用户未登录且访问需要登录的页面,跳转到登录页面
|
||||||
|
if (!userIsLoggedIn) {
|
||||||
|
return next('/login');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其他情况正常放行
|
||||||
|
next();
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user