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