mirror of
https://github.com/certd/certd.git
synced 2026-04-24 12:27:25 +08:00
fix: 修复下载证书时提示token已过期的问题
This commit is contained in:
@@ -49,33 +49,32 @@ function createService() {
|
||||
}
|
||||
|
||||
// 这个状态码是和后端约定的
|
||||
const { code } = dataAxios;
|
||||
// 根据 code 进行判断
|
||||
if (code === undefined) {
|
||||
if (dataAxios?.code === undefined) {
|
||||
// 如果没有 code 代表这不是项目后端开发的接口
|
||||
errorCreate(`非标准返回:${dataAxios}, ${response.config.url}`);
|
||||
return dataAxios;
|
||||
} else {
|
||||
// 有 code 代表这是一个后端接口 可以进行进一步的判断
|
||||
switch (code) {
|
||||
case 0:
|
||||
// [ 示例 ] code === 0 代表没有错误
|
||||
}
|
||||
const { code } = dataAxios;
|
||||
// 有 code 代表这是一个后端接口 可以进行进一步的判断
|
||||
switch (code) {
|
||||
case 0:
|
||||
// [ 示例 ] code === 0 代表没有错误
|
||||
// @ts-ignore
|
||||
return dataAxios?.data;
|
||||
default:
|
||||
// 不是正确的 code
|
||||
const errorMessage = dataAxios.msg || dataAxios.message || "未知错误";
|
||||
// @ts-ignore
|
||||
if (response?.config?.onError) {
|
||||
const err = new CodeError(errorMessage, dataAxios.code, dataAxios.data);
|
||||
// @ts-ignore
|
||||
return dataAxios.data;
|
||||
default:
|
||||
// 不是正确的 code
|
||||
const errorMessage = dataAxios.msg || dataAxios.message || "未知错误";
|
||||
// @ts-ignore
|
||||
if (response?.config?.onError) {
|
||||
const err = new CodeError(errorMessage, dataAxios.code, dataAxios.data);
|
||||
response.config.onError(err);
|
||||
return;
|
||||
}
|
||||
//@ts-ignore
|
||||
const showErrorNotify = response?.config?.showErrorNotify;
|
||||
errorCreate(`${errorMessage}: ${response.config.url}`, showErrorNotify, dataAxios);
|
||||
return dataAxios;
|
||||
}
|
||||
response.config.onError(err);
|
||||
return;
|
||||
}
|
||||
//@ts-ignore
|
||||
const showErrorNotify = response?.config?.showErrorNotify;
|
||||
errorCreate(`${errorMessage}: ${response.config.url}`, showErrorNotify, dataAxios);
|
||||
return dataAxios;
|
||||
}
|
||||
},
|
||||
error => {
|
||||
|
||||
@@ -3,12 +3,12 @@ export default {
|
||||
crud: { i18n: { name: "姓名", city: "城市", status: "状态" } },
|
||||
login: {
|
||||
logoutTip: "确认",
|
||||
logoutMessage: "确定要注销登录吗?"
|
||||
}
|
||||
logoutMessage: "确定要注销登录吗?",
|
||||
},
|
||||
},
|
||||
fs: {
|
||||
rowHandle: {
|
||||
title: "操作列"
|
||||
}
|
||||
}
|
||||
title: "操作列",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -20,7 +20,7 @@ import { useI18n } from "vue-i18n";
|
||||
import { useRouter } from "vue-router";
|
||||
|
||||
defineOptions({
|
||||
name: "FsUserInfo"
|
||||
name: "FsUserInfo",
|
||||
});
|
||||
const userStore = useUserStore();
|
||||
const { t } = useI18n();
|
||||
@@ -38,7 +38,7 @@ function doLogout() {
|
||||
content: t("app.login.logoutMessage"),
|
||||
onOk: async () => {
|
||||
await userStore.logout(true);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -30,7 +30,7 @@ const avatar = computed(() => {
|
||||
});
|
||||
|
||||
async function handleLogout() {
|
||||
userStore.logout(true);
|
||||
await userStore.logout(true);
|
||||
}
|
||||
|
||||
const settingStore = useSettingStore();
|
||||
|
||||
@@ -41,6 +41,12 @@ export async function register(user: RegisterReq): Promise<UserInfoRes> {
|
||||
data: user,
|
||||
});
|
||||
}
|
||||
export async function logout() {
|
||||
return await request({
|
||||
url: "/logout",
|
||||
method: "post",
|
||||
});
|
||||
}
|
||||
|
||||
export async function login(data: LoginReq): Promise<LoginRes> {
|
||||
//如果开启了登录与权限模块,则真实登录
|
||||
|
||||
@@ -108,9 +108,10 @@ export const useUserStore = defineStore({
|
||||
/**
|
||||
* @description: logout
|
||||
*/
|
||||
logout(goLogin = true) {
|
||||
async logout(goLogin = true) {
|
||||
this.resetState();
|
||||
resetAllStores();
|
||||
await UserApi.logout(); //主要是清空cookie
|
||||
goLogin && router.push("/login");
|
||||
mitter.emit("app.logout");
|
||||
},
|
||||
|
||||
@@ -28,7 +28,7 @@ export class LoginController extends BaseController {
|
||||
}
|
||||
|
||||
private writeTokenCookie(token: { expire: any; token: any }) {
|
||||
this.ctx.cookies.set("token", token.token, {
|
||||
this.ctx.cookies.set("certd_token", token.token, {
|
||||
maxAge: 1000 * token.expire
|
||||
});
|
||||
}
|
||||
@@ -72,5 +72,10 @@ export class LoginController extends BaseController {
|
||||
}
|
||||
|
||||
@Post('/logout', { summary: Constants.per.authOnly })
|
||||
public logout() {}
|
||||
public logout() {
|
||||
this.ctx.cookies.set("certd_token", "", {
|
||||
maxAge: 0
|
||||
});
|
||||
return this.ok();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,20 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
token = token.replace('Bearer ', '').trim();
|
||||
if (!token) {
|
||||
//尝试从cookie中获取token
|
||||
token = ctx.cookies.get('token') || '';
|
||||
const cookie = ctx.headers.cookie;
|
||||
if (cookie) {
|
||||
const items = cookie.split(';');
|
||||
for (const item of items) {
|
||||
if (!item || !item.trim()) {
|
||||
continue;
|
||||
}
|
||||
const [key, value] = item.split('=');
|
||||
if (key.trim() === 'certd_token') {
|
||||
token = value.trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!token) {
|
||||
//尝试从query中获取token
|
||||
|
||||
Reference in New Issue
Block a user