mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-04-18 18:20:49 +08:00
- 在 electron.vite.config.ts 中启用 Vue DevTools 插件 - 更新 package.json 中多个依赖版本,确保兼容性和性能 - 调整 tsconfig.node.json 的配置,优化模块解析 - 删除不再使用的组件 PlaylistType.vue、RecommendAlbum.vue、RecommendSinger.vue 和 RecommendSonglist.vue - 在请求处理逻辑中改进错误日志输出,使用 console.error 替代 console.log - 在首页视图中替换推荐歌手组件为顶部横幅组件 这些更改旨在提升开发效率和用户体验,确保项目的稳定性和可维护性。
116 lines
3.0 KiB
TypeScript
116 lines
3.0 KiB
TypeScript
import axios, { InternalAxiosRequestConfig } from 'axios';
|
|
|
|
import store from '@/store';
|
|
|
|
import { isElectron } from '.';
|
|
|
|
let setData: any = null;
|
|
const getSetData = () => {
|
|
if (window.electron) {
|
|
setData = window.electron.ipcRenderer.sendSync('get-store-value', 'set');
|
|
}
|
|
};
|
|
getSetData();
|
|
// 扩展请求配置接口
|
|
interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig {
|
|
retryCount?: number;
|
|
}
|
|
|
|
const baseURL = window.electron
|
|
? `http://127.0.0.1:${setData?.musicApiPort}`
|
|
: import.meta.env.VITE_API;
|
|
|
|
const request = axios.create({
|
|
baseURL,
|
|
timeout: 5000
|
|
});
|
|
|
|
// 最大重试次数
|
|
const MAX_RETRIES = 3;
|
|
// 重试延迟(毫秒)
|
|
const RETRY_DELAY = 500;
|
|
|
|
// 请求拦截器
|
|
request.interceptors.request.use(
|
|
(config: CustomAxiosRequestConfig) => {
|
|
getSetData();
|
|
// 只在retryCount未定义时初始化为0
|
|
if (config.retryCount === undefined) {
|
|
config.retryCount = 0;
|
|
}
|
|
|
|
// 在请求发送之前做一些处理
|
|
// 在get请求params中添加timestamp
|
|
config.params = {
|
|
...config.params,
|
|
timestamp: Date.now()
|
|
};
|
|
const token = localStorage.getItem('token');
|
|
if (token) {
|
|
config.params.cookie = config.params.cookie !== undefined ? config.params.cookie : token;
|
|
}
|
|
if (isElectron) {
|
|
const proxyConfig = setData?.proxyConfig;
|
|
if (proxyConfig?.enable && ['http', 'https'].includes(proxyConfig?.protocol)) {
|
|
config.params.proxy = `${proxyConfig.protocol}://${proxyConfig.host}:${proxyConfig.port}`;
|
|
}
|
|
if (setData.enableRealIP && setData.realIP) {
|
|
config.params.realIP = setData.realIP;
|
|
}
|
|
}
|
|
|
|
return config;
|
|
},
|
|
(error) => {
|
|
// 当请求异常时做一些处理
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
const NO_RETRY_URLS = ['暂时没有'];
|
|
|
|
// 响应拦截器
|
|
request.interceptors.response.use(
|
|
(response) => {
|
|
return response;
|
|
},
|
|
async (error) => {
|
|
console.error('error', error);
|
|
const config = error.config as CustomAxiosRequestConfig;
|
|
|
|
// 如果没有配置,直接返回错误
|
|
if (!config) {
|
|
return Promise.reject(error);
|
|
}
|
|
|
|
// 处理 301 状态码
|
|
if (error.response?.status === 301 && config.params.noLogin !== true) {
|
|
// 使用 store mutation 清除用户信息
|
|
store.commit('logout');
|
|
console.error(`301 状态码,清除登录信息后重试第 ${config.retryCount} 次`, config);
|
|
config.retryCount = 3;
|
|
}
|
|
|
|
// 检查是否还可以重试
|
|
if (
|
|
config.retryCount !== undefined &&
|
|
config.retryCount < MAX_RETRIES &&
|
|
!NO_RETRY_URLS.includes(config.url as string)
|
|
) {
|
|
config.retryCount++;
|
|
console.error(`请求重试第 ${config.retryCount} 次`);
|
|
|
|
// 延迟重试
|
|
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
|
|
|
|
// 重新发起请求
|
|
return request(config);
|
|
}
|
|
|
|
console.error(`重试${MAX_RETRIES}次后仍然失败`);
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default request;
|