mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-03-01 16:20:25 +00:00
refactor: 整体重构 (#1381)
* feat: pnpm new * Refactor build and release workflows, update dependencies Switch build scripts and workflows from npm to pnpm, update build and artifact paths, and simplify release workflow by removing version detection and changelog steps. Add new dependencies (silk-wasm, express, ws, node-pty-prebuilt-multiarch), update exports in package.json files, and add vite config for napcat-framework. Also, rename manifest.json for framework package and fix static asset copying in shell build config.
This commit is contained in:
64
packages/napcat-webui-frontend/src/utils/request.ts
Normal file
64
packages/napcat-webui-frontend/src/utils/request.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import axios from 'axios';
|
||||
|
||||
import key from '@/const/key';
|
||||
|
||||
export const serverRequest = axios.create({
|
||||
timeout: 5000,
|
||||
});
|
||||
|
||||
export const request = axios.create({
|
||||
timeout: 10000,
|
||||
});
|
||||
|
||||
export const requestServerWithFetch = async (
|
||||
url: string,
|
||||
options: RequestInit
|
||||
) => {
|
||||
const token = localStorage.getItem(key.token);
|
||||
|
||||
if (token) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
Authorization: `Bearer ${JSON.parse(token)}`,
|
||||
};
|
||||
}
|
||||
|
||||
const baseURL = '/api';
|
||||
|
||||
const response = await fetch(baseURL + url, options);
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
serverRequest.interceptors.request.use((config) => {
|
||||
const baseURL = '/api';
|
||||
|
||||
config.baseURL = baseURL;
|
||||
|
||||
const token = localStorage.getItem(key.token);
|
||||
|
||||
if (token) {
|
||||
config.headers['Authorization'] = `Bearer ${JSON.parse(token)}`;
|
||||
}
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
serverRequest.interceptors.response.use((response) => {
|
||||
// 如果是流式传输的文件
|
||||
if (response.headers['content-type'] === 'application/octet-stream') {
|
||||
return response;
|
||||
}
|
||||
if (response.data.code !== 0) {
|
||||
if (response.data.message === 'Unauthorized') {
|
||||
const token = localStorage.getItem(key.token);
|
||||
if (token && JSON.parse(token)) {
|
||||
localStorage.removeItem(key.token);
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
throw new Error(response.data.message);
|
||||
}
|
||||
|
||||
return response;
|
||||
});
|
||||
Reference in New Issue
Block a user