Commit 3c1cb6ed authored by duanledexianxianxian's avatar duanledexianxianxian

。。。

parent aa01d58c
export const dva = {
config: {
onError(e) {
console.log(e);
e.preventDefault();
},
},
};
......@@ -7,5 +7,8 @@ export default {
Authorization:
'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjEzNDQyNDg5ODksImxpZCI6Im91eWFuZ2JveXUifQ.FbY-QQLgq8H9CWSo1FhCPgXZJQtte5lhAPC4W45mHmo',
}),
resCodeKey: 'code', // 后台正常返回错误编码
resMessageKey: 'message', // 后台正常返回错误编码
successCode: 'sys.success', // 后台正常返回错误编码
isThrowError: true,
};
......@@ -29,12 +29,12 @@ class HttpRequest {
baseURL: this.options.baseUrl, // baseURL
timeout: 10000,
responseType: 'json',
'x-requested-with': 'XMLHttpRequest',
withCredentials: false, // default
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'content-type': 'application/json',
Accept: 'application/json',
'x-requested-with': 'XMLHttpRequest',
...this.options.headers,
},
};
......@@ -65,23 +65,24 @@ class HttpRequest {
const { data, status } = res;
return { data, status, statusText: codeMessage[status] };
},
error => {
// error
this.destroy(url);
const errorResult = JSON.parse(JSON.stringify(error));
const errorInfo = error.response;
if (!errorInfo) {
const {
request: { status },
} = errorResult;
errorResult.statusText = codeMessage[status];
if (options.errorHandler instanceof Function) {
options.errorHandler({ queues: this.queues, error, options });
}
}
error =>
// console.log('error', error);
// // error
// this.destroy(url);
// const errorResult = JSON.parse(JSON.stringify(error));
// console.log('errorResult', errorResult);
// const errorInfo = error.response;
// if (!errorInfo) {
// const {
// request: { status },
// } = errorResult;
// errorResult.statusText = codeMessage[status];
// if (options.errorHandler instanceof Function) {
// options.errorHandler({ queues: this.queues, error, options });
// }
// }
return Promise.reject(errorResult);
},
Promise.reject(error),
);
}
......
......@@ -2,7 +2,15 @@ import { notification } from 'antd';
import HttpRequest from './kim-request';
import config from '@/config';
const { baseUrl, apiPrefix, headers } = config;
const {
baseUrl,
apiPrefix,
headers,
resCodeKey,
resMessageKey,
successCode,
isThrowError = true,
} = config;
/**
* 异常处理程序
......@@ -46,7 +54,20 @@ const axios = new HttpRequest({
* @param {返回请求数据} response
* @param {配置项} more
*/
const checkCode = (response, more) => {};
const checkCode = (response, more) => {
const { data } = response;
//
if (`${data[resCodeKey]}` !== `${successCode}`) {
if (isThrowError) {
const errorMessage = data[resMessageKey] || '后端接口返回异常';
const error = new Error(data[resCodeKey]);
error.code = response[resCodeKey];
error.message = errorMessage;
throw error;
}
}
return data;
};
export default function request(url, options = {}, more = {}) {
let newOptions = options;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment