mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-16 13:17:46 +00:00
fix(utils/request): token 過期 (301) 時重新導向至登入頁面 (#1448)
This commit is contained in:
parent
1c25f11821
commit
5ff8868d3e
1 changed files with 31 additions and 5 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { getCookie } from '@/utils/auth';
|
import { getCookie, doLogout } from '@/utils/auth';
|
||||||
|
import router from '@/router';
|
||||||
|
|
||||||
let baseURL = '';
|
let baseURL = '';
|
||||||
// Web 和 Electron 跑在不同端口避免同时启动时冲突
|
// Web 和 Electron 跑在不同端口避免同时启动时冲突
|
||||||
|
|
@ -21,8 +22,12 @@ const service = axios.create({
|
||||||
|
|
||||||
service.interceptors.request.use(function (config) {
|
service.interceptors.request.use(function (config) {
|
||||||
if (!config.params) config.params = {};
|
if (!config.params) config.params = {};
|
||||||
if (baseURL[0] !== '/' && !process.env.IS_ELECTRON) {
|
if (baseURL.length) {
|
||||||
config.params.cookie = `MUSIC_U=${getCookie('MUSIC_U')};`;
|
if (baseURL[0] !== '/' && !process.env.IS_ELECTRON) {
|
||||||
|
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')) {
|
if (!process.env.IS_ELECTRON && !config.url.includes('/login')) {
|
||||||
|
|
@ -42,8 +47,29 @@ service.interceptors.response.use(
|
||||||
const res = response.data;
|
const res = response.data;
|
||||||
return res;
|
return res;
|
||||||
},
|
},
|
||||||
error => {
|
async error => {
|
||||||
return Promise.reject(error);
|
/** @type {import('axios').AxiosResponse | null} */
|
||||||
|
const response = error.response;
|
||||||
|
const 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' });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue