feat: add query-login

This commit is contained in:
2025-03-22 00:52:58 +08:00
parent 3d45a83129
commit 4b16ec8499
17 changed files with 530 additions and 650 deletions

View File

@@ -1,7 +1,9 @@
import { Query } from '@kevisual/query/query';
import { getConfig } from './get-config.ts';
import { QueryLoginNode, storage } from '@kevisual/query-login/node';
const config = getConfig();
export const baseURL = config?.baseURL || 'https://envision.xiongxiao.me';
export const baseURL = config?.baseURL || 'https://kevisual.cn';
export { storage };
export const getBaseURL = () => {
if (typeof config?.dev === 'undefined') {
return baseURL;
@@ -23,10 +25,29 @@ export const query = new Query({
query.beforeRequest = async (config) => {
if (config.headers) {
const token = await getConfig()?.token;
const token = await storage.getItem('token');
if (token) {
config.headers['Authorization'] = 'Bearer ' + token;
}
}
return config;
};
query.afterResponse = async (response, ctx) => {
if (response.code === 401) {
if (query.stop) {
return {
code: 500,
message: '登录已过期',
};
}
query.stop = true;
const res = await queryLogin.afterCheck401ToRefreshToken(response, ctx);
query.stop = false;
return res;
}
return response as any;
};
export const queryLogin = new QueryLoginNode({
query: query as any,
onLoad: async () => {},
});