ref : 重构令牌管理逻辑,,统一状态码识别

This commit is contained in:
TsMask
2025-04-27 17:23:33 +08:00
parent f76311cf1b
commit 2de9788373
23 changed files with 229 additions and 253 deletions

View File

@@ -4,7 +4,7 @@ import {
CACHE_SESSION_CRYPTO_API,
} from '@/constants/cache-keys-constants';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { removeToken } from '@/plugins/auth-token';
import { delAccessToken, delRefreshToken } from '@/plugins/auth-token';
import { parseUrlPath } from '@/plugins/file-static-url';
import { localGet, localSet } from '@/utils/cache-local-utils';
import { sessionSet } from '@/utils/cache-session-utils';
@@ -94,7 +94,8 @@ const useAppStore = defineStore('app', {
this.bootloader = res.data.bootloader === 'true';
// 引导时
if (this.bootloader) {
removeToken();
delAccessToken();
delRefreshToken();
}
this.loginAuth = res.data.loginAuth !== 'false';
this.cryptoApi = res.data.cryptoApi !== 'false';

View File

@@ -52,7 +52,7 @@ const useNeInfoStore = defineStore('neinfo', {
async fnNelist() {
// 有数据不请求
if (this.neList.length > 0) {
return { code: 1, data: this.neList, msg: 'success' };
return { code: RESULT_CODE_SUCCESS, data: this.neList, msg: 'success' };
}
const res = await listAllNeInfo({
bandStatus: false,
@@ -79,7 +79,7 @@ const useNeInfoStore = defineStore('neinfo', {
async fnNeTaskPerformance() {
// 有数据不请求
if (this.perMeasurementList.length > 0) {
return { code: 1, data: this.perMeasurementList, msg: 'success' };
return { code: RESULT_CODE_SUCCESS, data: this.perMeasurementList, msg: 'success' };
}
const res = await getNePerformanceList();
if (res.code === RESULT_CODE_SUCCESS) {

View File

@@ -5,7 +5,7 @@ import type {
RouteMeta,
RouteRecordRaw,
} from 'vue-router';
import { getRouters } from '@/api/router';
import { getRouter } from '@/api/auth';
import BasicLayout from '@/layouts/BasicLayout.vue';
import BlankLayout from '@/layouts/BlankLayout.vue';
import LinkLayout from '@/layouts/LinkLayout.vue';
@@ -46,7 +46,7 @@ const useRouterStore = defineStore('router', {
* @returns 生成的路由菜单
*/
async generateRoutes() {
const res = await getRouters();
const res = await getRouter();
if (res.code === RESULT_CODE_SUCCESS) {
const buildRoutes = buildRouters(res.data.concat());
this.buildRouterData = buildRoutes;

View File

@@ -1,9 +1,13 @@
import defaultAvatar from '@/assets/images/default_avatar.png';
import useLayoutStore from './layout';
import { login, logout, getInfo } from '@/api/login';
import { setToken, removeToken } from '@/plugins/auth-token';
import { login, logout, getInfo } from '@/api/auth';
import {
delAccessToken,
delRefreshToken,
setAccessToken,
setRefreshToken,
} from '@/plugins/auth-token';
import { defineStore } from 'pinia';
import { TOKEN_RESPONSE_FIELD } from '@/constants/token-constants';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { parseUrlPath } from '@/plugins/file-static-url';
@@ -105,8 +109,8 @@ const useUserStore = defineStore('user', {
async fnLogin(loginBody: Record<string, string>) {
const res = await login(loginBody);
if (res.code === RESULT_CODE_SUCCESS && res.data) {
const token = res.data[TOKEN_RESPONSE_FIELD];
setToken(token);
setAccessToken(res.data.accessToken, res.data.refreshExpiresIn);
setRefreshToken(res.data.refreshToken, res.data.refreshExpiresIn);
if (res.data?.forcePasswdChange) {
this.forcePasswdChange = true;
}
@@ -150,11 +154,6 @@ const useUserStore = defineStore('user', {
this.forcePasswdChange = true;
}
}
// 网络错误时退出登录状态
if (res.code === 0) {
removeToken();
window.location.reload();
}
return res;
},
// 退出系统
@@ -166,7 +165,8 @@ const useUserStore = defineStore('user', {
} finally {
this.roles = [];
this.permissions = [];
removeToken();
delAccessToken();
delRefreshToken();
}
},
},