mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-17 01:14:50 +08:00
初始化api
This commit is contained in:
+1
-1
@@ -1,7 +1,7 @@
|
||||
export default {
|
||||
siteName: '京东羊毛脚本控制面板',
|
||||
apiPrefix: '/api/',
|
||||
authKey: 'whyour',
|
||||
authKey: 'token',
|
||||
|
||||
/* Layout configuration, specify which layout to use for route. */
|
||||
layouts: [
|
||||
|
||||
+17
-7
@@ -1,29 +1,39 @@
|
||||
import { extend } from 'umi-request';
|
||||
import { history } from 'umi';
|
||||
import config from './config';
|
||||
|
||||
const time = Date.now();
|
||||
const errorHandler = function (error: any) {
|
||||
if (error.response) {
|
||||
console.log(error.response)
|
||||
console.log(error.response);
|
||||
} else {
|
||||
console.log(error.message);
|
||||
}
|
||||
|
||||
throw error; // 如果throw. 错误将继续抛出.
|
||||
// return {some: 'data'};
|
||||
};
|
||||
|
||||
const _request = extend({ timeout: 5000, params: { t: time }, errorHandler });
|
||||
|
||||
_request.interceptors.response.use(async response => {
|
||||
const res = await response.clone().text()
|
||||
if (res === '请先登录!') {
|
||||
_request.interceptors.request.use((url, options) => {
|
||||
const token = localStorage.getItem(config.authKey);
|
||||
if (token) {
|
||||
const headers = {
|
||||
Authorization: `Bearer ${token}`,
|
||||
};
|
||||
return { url, options: { ...options, headers } };
|
||||
}
|
||||
return { url, options };
|
||||
});
|
||||
|
||||
_request.interceptors.response.use(async (response) => {
|
||||
const res = await response.clone();
|
||||
if (res.status === 401) {
|
||||
setTimeout(() => {
|
||||
localStorage.removeItem('whyour');
|
||||
history.push('/login');
|
||||
});
|
||||
}
|
||||
return response;
|
||||
})
|
||||
});
|
||||
|
||||
export const request = _request;
|
||||
|
||||
Reference in New Issue
Block a user