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 { ...@@ -7,5 +7,8 @@ export default {
Authorization: Authorization:
'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjEzNDQyNDg5ODksImxpZCI6Im91eWFuZ2JveXUifQ.FbY-QQLgq8H9CWSo1FhCPgXZJQtte5lhAPC4W45mHmo', 'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjEzNDQyNDg5ODksImxpZCI6Im91eWFuZ2JveXUifQ.FbY-QQLgq8H9CWSo1FhCPgXZJQtte5lhAPC4W45mHmo',
}), }),
resCodeKey: 'code', // 后台正常返回错误编码
resMessageKey: 'message', // 后台正常返回错误编码
successCode: 'sys.success', // 后台正常返回错误编码 successCode: 'sys.success', // 后台正常返回错误编码
isThrowError: true,
}; };
...@@ -29,12 +29,12 @@ class HttpRequest { ...@@ -29,12 +29,12 @@ class HttpRequest {
baseURL: this.options.baseUrl, // baseURL baseURL: this.options.baseUrl, // baseURL
timeout: 10000, timeout: 10000,
responseType: 'json', responseType: 'json',
'x-requested-with': 'XMLHttpRequest',
withCredentials: false, // default withCredentials: false, // default
headers: { headers: {
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json', 'content-type': 'application/json',
Accept: 'application/json', Accept: 'application/json',
'x-requested-with': 'XMLHttpRequest',
...this.options.headers, ...this.options.headers,
}, },
}; };
...@@ -65,23 +65,24 @@ class HttpRequest { ...@@ -65,23 +65,24 @@ class HttpRequest {
const { data, status } = res; const { data, status } = res;
return { data, status, statusText: codeMessage[status] }; return { data, status, statusText: codeMessage[status] };
}, },
error => { error =>
// error // console.log('error', error);
this.destroy(url); // // error
const errorResult = JSON.parse(JSON.stringify(error)); // this.destroy(url);
const errorInfo = error.response; // const errorResult = JSON.parse(JSON.stringify(error));
if (!errorInfo) { // console.log('errorResult', errorResult);
const { // const errorInfo = error.response;
request: { status }, // if (!errorInfo) {
} = errorResult; // const {
errorResult.statusText = codeMessage[status]; // request: { status },
if (options.errorHandler instanceof Function) { // } = errorResult;
options.errorHandler({ queues: this.queues, error, options }); // 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'; ...@@ -2,7 +2,15 @@ import { notification } from 'antd';
import HttpRequest from './kim-request'; import HttpRequest from './kim-request';
import config from '@/config'; 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({ ...@@ -46,7 +54,20 @@ const axios = new HttpRequest({
* @param {返回请求数据} response * @param {返回请求数据} response
* @param {配置项} more * @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 = {}) { export default function request(url, options = {}, more = {}) {
let newOptions = options; 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