mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-16 05:08:04 +00:00
92 lines
2.3 KiB
JavaScript
92 lines
2.3 KiB
JavaScript
import router from '@/router';
|
|
import { doLogout, getCookie } from '@/utils/auth';
|
|
import axios from 'axios';
|
|
|
|
let baseURL = '';
|
|
// Web 和 Electron 跑在不同端口避免同时启动时冲突
|
|
if (process.env.IS_ELECTRON) {
|
|
if (process.env.NODE_ENV === 'production') {
|
|
baseURL = process.env.VUE_APP_ELECTRON_API_URL;
|
|
} else {
|
|
baseURL = process.env.VUE_APP_ELECTRON_API_URL_DEV;
|
|
}
|
|
} else {
|
|
baseURL = process.env.VUE_APP_NETEASE_API_URL;
|
|
}
|
|
|
|
const service = axios.create({
|
|
baseURL,
|
|
withCredentials: true,
|
|
timeout: 15000,
|
|
});
|
|
|
|
service.interceptors.request.use(function (config) {
|
|
if (!config.params) config.params = {};
|
|
if (baseURL.length) {
|
|
if (
|
|
baseURL[0] !== '/' &&
|
|
!process.env.IS_ELECTRON &&
|
|
getCookie('MUSIC_U') !== null
|
|
) {
|
|
config.params.cookie = `MUSIC_U=${getCookie('MUSIC_U')};`;
|
|
}
|
|
} else {
|
|
console.error("You must set up the baseURL in the service's config");
|
|
}
|
|
|
|
if (!process.env.IS_ELECTRON && !config.url.includes('/login')) {
|
|
config.params.realIP = '211.161.244.70';
|
|
}
|
|
|
|
if (process.env.VUE_APP_REAL_IP) {
|
|
config.params.realIP = process.env.VUE_APP_REAL_IP;
|
|
}
|
|
|
|
const proxy = JSON.parse(localStorage.getItem('settings')).proxyConfig;
|
|
if (['HTTP', 'HTTPS'].includes(proxy.protocol)) {
|
|
config.params.proxy = `${proxy.protocol}://${proxy.server}:${proxy.port}`;
|
|
}
|
|
|
|
return config;
|
|
});
|
|
|
|
service.interceptors.response.use(
|
|
response => {
|
|
const res = response.data;
|
|
return res;
|
|
},
|
|
async error => {
|
|
/** @type {import('axios').AxiosResponse | null} */
|
|
let response;
|
|
let data;
|
|
if (error === 'TypeError: baseURL is undefined') {
|
|
response = error;
|
|
data = error;
|
|
console.error("You must set up the baseURL in the service's config");
|
|
} else if (error.response) {
|
|
response = error.response;
|
|
data = response.data;
|
|
}
|
|
|
|
if (
|
|
response &&
|
|
typeof data === 'object' &&
|
|
data.code === 301 &&
|
|
data.msg === '需要登录'
|
|
) {
|
|
console.warn('Token has expired. Logout now!');
|
|
|
|
// 登出帳戶
|
|
doLogout();
|
|
|
|
// 導向登入頁面
|
|
if (process.env.IS_ELECTRON === true) {
|
|
router.push({ name: 'loginAccount' });
|
|
} else {
|
|
router.push({ name: 'login' });
|
|
}
|
|
}
|
|
}
|
|
);
|
|
|
|
export default service;
|