Commit 5c414d78 authored by duanledexianxianxian's avatar duanledexianxianxian 😁

sync code

parent 3c1cb6ed
......@@ -40,10 +40,10 @@ const plugins: IPlugin[] = [
},
}
: false,
dll: {
include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
exclude: ['@babel/runtime', 'netlify-lambda'],
},
// dll: {
// include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
// exclude: ['@babel/runtime', 'netlify-lambda'],
// },
},
],
[
......
......@@ -5,7 +5,7 @@ export default {
storeNameSpace: 'kim',
headers: () => ({
Authorization:
'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjEzNDQyNDg5ODksImxpZCI6Im91eWFuZ2JveXUifQ.FbY-QQLgq8H9CWSo1FhCPgXZJQtte5lhAPC4W45mHmo',
'1eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjE0MzAzMzI4NzQsImxpZCI6Im91eWFuZ2JveXUifQ.209scGXfvjPj7KgvXhzrI4ciHg6aTDt8LQ1Z2Nw-ZxA',
}),
resCodeKey: 'code', // 后台正常返回错误编码
resMessageKey: 'message', // 后台正常返回错误编码
......
import axios from 'axios';
const codeMessage = {
200: '服务器成功返回请求的数据。',
201: '新建或修改数据成功。',
202: '一个请求已经进入后台排队(异步任务)。',
204: '删除数据成功。',
400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
401: '用户没有权限(令牌、用户名、密码错误)。',
403: '用户得到授权,但是访问是被禁止的。',
404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
406: '请求的格式不可得。',
410: '请求的资源被永久删除,且不会再得到的。',
422: '当创建一个对象时,发生一个验证错误。',
500: '服务器发生错误,请检查服务器。',
502: '网关错误。',
503: '服务不可用,服务器暂时过载或维护。',
504: '网关超时。',
};
class HttpRequest {
constructor(options) {
this.options = options;
......@@ -32,9 +14,9 @@ class HttpRequest {
withCredentials: false, // default
headers: {
'Access-Control-Allow-Origin': '*',
'content-type': 'application/json',
'Content-Type': 'application/json',
Accept: 'application/json',
'x-requested-with': 'XMLHttpRequest',
'X-Requested-With': 'XMLHttpRequest',
...this.options.headers,
},
};
......@@ -63,26 +45,16 @@ class HttpRequest {
// success
this.destroy(url);
const { data, status } = res;
return { data, status, statusText: codeMessage[status] };
return { data, status };
},
error => {
// error
this.destroy(url);
if (error.response) {
console.log(error.response);
}
return Promise.reject(error);
},
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 });
// }
// }
Promise.reject(error),
);
}
......
......@@ -2,6 +2,23 @@ import { notification } from 'antd';
import HttpRequest from './kim-request';
import config from '@/config';
const codeMessage = {
200: '服务器成功返回请求的数据。',
201: '新建或修改数据成功。',
202: '一个请求已经进入后台排队(异步任务)。',
204: '删除数据成功。',
400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
401: '用户没有权限(令牌、用户名、密码错误)。',
403: '用户得到授权,但是访问是被禁止的。',
404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
406: '请求的格式不可得。',
410: '请求的资源被永久删除,且不会再得到的。',
422: '当创建一个对象时,发生一个验证错误。',
500: '服务器发生错误,请检查服务器。',
502: '网关错误。',
503: '服务不可用,服务器暂时过载或维护。',
504: '网关超时。',
};
const {
baseUrl,
apiPrefix,
......@@ -12,22 +29,6 @@ const {
isThrowError = true,
} = config;
/**
* 异常处理程序
*/
const errorHandler = error => {
const { response } = error;
if (response && response.status) {
const errorText = response.statusText;
const { status, url, code } = response;
notification.error({
key: `notification_${code}`,
message: `请求错误 ${status}: ${url}`,
description: errorText,
});
}
};
/**
* 组装url
* @param {url} url
......@@ -46,7 +47,6 @@ const mergeApi = (url, more) => {
const axios = new HttpRequest({
baseUrl,
headers: headers() || {},
errorHandler,
});
/**
......@@ -54,28 +54,54 @@ const axios = new HttpRequest({
* @param {返回请求数据} response
* @param {配置项} more
*/
const checkCode = (response, more) => {
const handleResponse = (response, more) => {
console.log('handleResponse', response);
const { data } = response;
//
if (`${data[resCodeKey]}` !== `${successCode}`) {
if (isThrowError) {
const errorMessage = data[resMessageKey] || '后端接口返回异常';
const error = new Error(data[resCodeKey]);
error.code = response[resCodeKey];
error.code = data[resCodeKey];
error.message = errorMessage;
throw error;
return Promise.reject(error);
}
}
return data;
};
const handleError = error => {
console.log('handleError');
const { response } = error;
console.log('handleError', response);
if (response && response.status) {
// const errorText = codeMessage[response.status] || response.statusText;
// const {
// status,
// config: { url },
// } = response;
console.log('什么情况');
notification.error({
message: '请求错误 ',
description: '111',
duration: 100,
});
}
throw error;
};
export default function request(url, options = {}, more = {}) {
let newOptions = options;
newOptions.url = url;
if (more.headers) {
newOptions = { ...options, headers: more.headers };
}
return axios.request(newOptions).then(response => checkCode(response, more));
return axios
.request(newOptions)
.then(response => handleResponse(response, more))
.catch(error => handleError(error));
}
const get = (url, data, more = {}) =>
......@@ -144,6 +170,13 @@ const formDataUpload = (url, data, more = {}) => {
});
};
/**
* 上传文件
* @param {url} url
* @param {data} data
* @param {type} type
* @param {more} more
*/
const uploadFile = (url, data, type = 'formData', more = {}) => {
if (type === 'formData') {
formDataUpload(url, data, 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