|
@@ -87,44 +87,75 @@ const codeMessage = {
|
|
|
|
|
|
/**
|
|
|
* 异常处理程序
|
|
|
+ * {
|
|
|
+ * "name":"BizError", //name 为BizError是因为success为false
|
|
|
+ * "data":{
|
|
|
+ * "error":[{"field":"","message":"账号或密码不匹配。"}],
|
|
|
+ * "token":"90c77e0f-cd3b-4555-89bf-21b69dc8b454"
|
|
|
+ * },
|
|
|
+ * "info":{
|
|
|
+ * "success":false,
|
|
|
+ * "data":null,
|
|
|
+ * "errorCode":"",
|
|
|
+ * "errorMessage":"账号或密码不匹配。",
|
|
|
+ * "showType":2,
|
|
|
+ * "traceId":"-1",
|
|
|
+ * "host":"-1"
|
|
|
+ * }
|
|
|
+ * }
|
|
|
*/
|
|
|
const errorHandler = (error: ResponseError) => {
|
|
|
- // const { response } = error;
|
|
|
- // if (response && response.status) {
|
|
|
- // const errorText = codeMessage[response.status] || response.statusText;
|
|
|
- // const { status, url } = response;
|
|
|
- //
|
|
|
- // notification.error({
|
|
|
- // message: `请求错误 ${status}: ${url}`,
|
|
|
- // description: errorText,
|
|
|
- // });
|
|
|
- // }
|
|
|
- //
|
|
|
- // if (!response) {
|
|
|
- // notification.error({
|
|
|
- // description: '您的网络发生异常,无法连接服务器',
|
|
|
- // message: '网络异常',
|
|
|
- // });
|
|
|
- // }
|
|
|
- // throw error;
|
|
|
+ const { info } = error;
|
|
|
+ if (error.name === 'BizError') {
|
|
|
+ // const { errorCode } = info;
|
|
|
+ // notification.warning({
|
|
|
+ // message: `请求错误`,
|
|
|
+ // description: '111111',
|
|
|
+ // });
|
|
|
+ console.log(info)
|
|
|
+ if(info.errorCode === '403'){
|
|
|
+ message.warning('登录超时请重新登录');
|
|
|
+ history.push('/user/login');
|
|
|
+ }else{
|
|
|
+ message.warning(info.errorMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!info) {
|
|
|
+ notification.error({
|
|
|
+ description: '您的网络发生异常,无法连接服务器',
|
|
|
+ message: '网络异常',
|
|
|
+ });
|
|
|
+ }
|
|
|
+ throw info;
|
|
|
};
|
|
|
|
|
|
export const request: RequestConfig = {
|
|
|
errorHandler,
|
|
|
+ errorConfig: {
|
|
|
+ adaptor: (resData)=>{
|
|
|
+ console.log(resData,'resData')
|
|
|
+ return {
|
|
|
+ success: resData.error && resData.error.length === 0,
|
|
|
+ data: resData.data || null,
|
|
|
+ errorCode: (resData.error && resData.error.length > 0) ? resData.error[0].field : 0,
|
|
|
+ errorMessage: (resData.error && resData.error.length > 0) ? resData.error[0].message : '',
|
|
|
+ showType: (resData.error && resData.error.length > 0) ? (resData.error[0].field === '403' ? 9 : 2) : 0,
|
|
|
+ traceId: '-1',
|
|
|
+ host: '-1',
|
|
|
+ };
|
|
|
+ },
|
|
|
+ errorPage: '/user/login', //showType为9是跳这个地址
|
|
|
+ },
|
|
|
requestInterceptors: [
|
|
|
(url, config) => {
|
|
|
- if (config.method === 'get') {
|
|
|
- url = url + '?' + qs.stringify({
|
|
|
- token: cookies.get('token'),
|
|
|
- });
|
|
|
- } else {
|
|
|
+ if (config.method === 'post') {
|
|
|
config.data = qs.stringify({
|
|
|
...(config.data || {}),
|
|
|
- token: cookies.get('token'),
|
|
|
});
|
|
|
}
|
|
|
config.headers = {
|
|
|
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;'
|
|
|
+ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;',
|
|
|
+ 'X-Requested-With' : 'XMLHttpRequest',
|
|
|
}
|
|
|
return {
|
|
|
url: `${url}`,
|
|
@@ -132,15 +163,15 @@ export const request: RequestConfig = {
|
|
|
};
|
|
|
},
|
|
|
],
|
|
|
+ middlewares: [
|
|
|
+ async function middlewareA(ctx, next) {
|
|
|
+ await next();
|
|
|
+ }
|
|
|
+ ],
|
|
|
responseInterceptors: [
|
|
|
async (config) => {
|
|
|
- const response = await config.clone().json();
|
|
|
- if (response.error && response.error.length > 0 && response.error[0].field === '403') {
|
|
|
- message.warning('登录超时请重新登录');
|
|
|
- history.push('/user/login');
|
|
|
- }
|
|
|
- cookies.set('token',response.token);
|
|
|
return config;
|
|
|
+
|
|
|
}
|
|
|
]
|
|
|
};
|