Commit ac1f46f4 authored by duanledexianxianxian's avatar duanledexianxianxian 😁

sync

parent 5c414d78
...@@ -5,7 +5,7 @@ export default { ...@@ -5,7 +5,7 @@ export default {
storeNameSpace: 'kim', storeNameSpace: 'kim',
headers: () => ({ headers: () => ({
Authorization: Authorization:
'1eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjE0MzAzMzI4NzQsImxpZCI6Im91eWFuZ2JveXUifQ.209scGXfvjPj7KgvXhzrI4ciHg6aTDt8LQ1Z2Nw-ZxA', 'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjE0MzAzMzI4NzQsImxpZCI6Im91eWFuZ2JveXUifQ.209scGXfvjPj7KgvXhzrI4ciHg6aTDt8LQ1Z2Nw-ZxA',
}), }),
resCodeKey: 'code', // 后台正常返回错误编码 resCodeKey: 'code', // 后台正常返回错误编码
resMessageKey: 'message', // 后台正常返回错误编码 resMessageKey: 'message', // 后台正常返回错误编码
......
...@@ -44,14 +44,17 @@ class HttpRequest { ...@@ -44,14 +44,17 @@ class HttpRequest {
res => { res => {
// success // success
this.destroy(url); this.destroy(url);
if (options.successHandler instanceof Function) {
options.successHandler({ queues: this.queues, res, options });
}
const { data, status } = res; const { data, status } = res;
return { data, status }; return { data, status };
}, },
error => { error => {
// error // error
this.destroy(url); this.destroy(url);
if (error.response) { if (options.errorHandler instanceof Function) {
console.log(error.response); options.errorHandler({ queues: this.queues, error, options });
} }
return Promise.reject(error); return Promise.reject(error);
}, },
......
...@@ -2,6 +2,10 @@ import { notification } from 'antd'; ...@@ -2,6 +2,10 @@ import { notification } from 'antd';
import HttpRequest from './kim-request'; import HttpRequest from './kim-request';
import config from '@/config'; import config from '@/config';
/**
* todo 还需处理国际化
*/
const codeMessage = { const codeMessage = {
200: '服务器成功返回请求的数据。', 200: '服务器成功返回请求的数据。',
201: '新建或修改数据成功。', 201: '新建或修改数据成功。',
...@@ -55,41 +59,48 @@ const axios = new HttpRequest({ ...@@ -55,41 +59,48 @@ const axios = new HttpRequest({
* @param {配置项} more * @param {配置项} more
*/ */
const handleResponse = (response, more) => { const handleResponse = (response, more) => {
console.log('handleResponse', response);
const { data } = response; const { data } = response;
// //
if (`${data[resCodeKey]}` !== `${successCode}`) { if (`${data[resCodeKey]}` !== `${successCode}`) {
if (isThrowError) { if (isThrowError) {
const errorMessage = data[resMessageKey] || '后端接口返回异常'; const errorMessage = data[resMessageKey] || '后端接口返回异常';
const error = new Error(data[resCodeKey]); let error = new Error();
error = { ...error, ...data };
error.code = data[resCodeKey]; error.code = data[resCodeKey];
error.message = errorMessage; error.message = errorMessage;
return Promise.reject(error); return Promise.reject(error);
} }
} }
// success
return data; return data;
}; };
const handleError = error => { const handleError = error => {
console.log('handleError');
const { response } = error; const { response } = error;
console.log('handleError', response); // 如果response存在
if (response && response.status) { if (response && response.status) {
// const errorText = codeMessage[response.status] || response.statusText; const errorText = codeMessage[response.status] || response.statusText;
// const { const {
// status, status,
// config: { url }, config: { url },
// } = response; } = response;
console.log('什么情况'); notification.error({
message: `请求错误 ${status}: ${url}`,
description: errorText,
});
} else {
const { code, message } = error;
notification.error({ notification.error({
message: '请求错误 ', key: `notification_${code ? message : code}`,
description: '111', message: '请求错误',
duration: 100, description: message,
}); });
} }
throw error; if (isThrowError) {
throw error;
} else {
return error;
}
}; };
export default function request(url, options = {}, more = {}) { export default function request(url, options = {}, more = {}) {
......
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