fix: 请求过滤拦截处理
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
/**令牌-数据响应字段 */
|
/**令牌-数据响应字段 */
|
||||||
export const TOKEN_RESPONSE_FIELD = 'access_token';
|
export const TOKEN_RESPONSE_FIELD = 'accessToken';
|
||||||
|
|
||||||
/**令牌-请求头标识前缀 */
|
/**令牌-请求头标识前缀 */
|
||||||
export const TOKEN_KEY_PREFIX = 'Bearer ';
|
export const TOKEN_KEY_PREFIX = '';
|
||||||
|
|
||||||
/**令牌-请求头标识 */
|
/**令牌-请求头标识 */
|
||||||
export const TOKEN_KEY = 'Authorization';
|
export const TOKEN_KEY = 'Accesstoken';
|
||||||
|
|
||||||
/**令牌-存放Cookie标识 */
|
/**令牌-存放Cookie标识 */
|
||||||
export const TOKEN_COOKIE = 'AuthAntdv';
|
export const TOKEN_COOKIE = 'AuthOMC';
|
||||||
|
|||||||
@@ -165,12 +165,29 @@ function beforeRequest(options: OptionsType): OptionsType | Promise<any> {
|
|||||||
|
|
||||||
/**请求后的拦截 */
|
/**请求后的拦截 */
|
||||||
function interceptorResponse(res: ResultType): ResultType | Promise<any> {
|
function interceptorResponse(res: ResultType): ResultType | Promise<any> {
|
||||||
//console.log('请求后的拦截', res);
|
console.log('请求后的拦截', res);
|
||||||
|
|
||||||
// 登录失效时,移除授权令牌并重新刷新页面
|
// 登录失效时,移除授权令牌并重新刷新页面
|
||||||
if (res.code === 401) {
|
if (res.code === 401) {
|
||||||
removeToken();
|
removeToken();
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 风格处理
|
||||||
|
if (!Reflect.has(res, 'code')) {
|
||||||
|
return Promise.resolve({
|
||||||
|
code: 1,
|
||||||
|
msg: 'success',
|
||||||
|
data: res,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (Reflect.has(res, 'error')){
|
||||||
|
return Promise.resolve({
|
||||||
|
code: 0,
|
||||||
|
msg: 'error',
|
||||||
|
data: res.error,
|
||||||
|
});
|
||||||
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +227,7 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
|||||||
// console.log('请求结果:', res);
|
// console.log('请求结果:', res);
|
||||||
if (res.status === 500) {
|
if (res.status === 500) {
|
||||||
return {
|
return {
|
||||||
code: 500,
|
code: 0,
|
||||||
msg: '服务器连接出错!',
|
msg: '服务器连接出错!',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -220,7 +237,7 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
|||||||
case 'text': // 文本数据
|
case 'text': // 文本数据
|
||||||
const str = await res.text();
|
const str = await res.text();
|
||||||
return {
|
return {
|
||||||
code: 200,
|
code: 1,
|
||||||
msg: str,
|
msg: str,
|
||||||
};
|
};
|
||||||
case 'json': // json格式数据
|
case 'json': // json格式数据
|
||||||
@@ -243,7 +260,7 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
|||||||
? await res.blob()
|
? await res.blob()
|
||||||
: await res.arrayBuffer();
|
: await res.arrayBuffer();
|
||||||
return {
|
return {
|
||||||
code: 200,
|
code: 1,
|
||||||
msg: '成功',
|
msg: '成功',
|
||||||
data: data,
|
data: data,
|
||||||
status: res.status,
|
status: res.status,
|
||||||
@@ -251,7 +268,7 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
|||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
return {
|
return {
|
||||||
code: 500,
|
code: 0,
|
||||||
msg: '未知响应数据类型',
|
msg: '未知响应数据类型',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -259,7 +276,7 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
|||||||
// 请求被终止时
|
// 请求被终止时
|
||||||
if (error.name === 'AbortError') {
|
if (error.name === 'AbortError') {
|
||||||
return {
|
return {
|
||||||
code: 500,
|
code: 0,
|
||||||
msg: '网络连接超时!',
|
msg: '网络连接超时!',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user