diff --git a/src/app.js b/src/app.js new file mode 100644 index 0000000000000000000000000000000000000000..a12847b63779036231cc4974221b55b3cdffb745 --- /dev/null +++ b/src/app.js @@ -0,0 +1,8 @@ +export const dva = { + config: { + onError(e) { + console.log(e); + e.preventDefault(); + }, + }, +}; diff --git a/src/config.js b/src/config.js index 2e189ec2f57937a40a6664db9934e81d5f66fa07..8649a6b96133b7f7e9dffab6a13fd24a50c206fe 100644 --- a/src/config.js +++ b/src/config.js @@ -7,5 +7,8 @@ export default { Authorization: 'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjEzNDQyNDg5ODksImxpZCI6Im91eWFuZ2JveXUifQ.FbY-QQLgq8H9CWSo1FhCPgXZJQtte5lhAPC4W45mHmo', }), + resCodeKey: 'code', // 后台正常返回错误编码 + resMessageKey: 'message', // 后台正常返回错误编码 successCode: 'sys.success', // 后台正常返回错误编码 + isThrowError: true, }; diff --git a/src/utils/kim-request.js b/src/utils/kim-request.js index 529c995d88f0a83ab3f5744930b8b0fc30646e0f..a2f89e4e52d788e677b658ee4560ffe7033bd95c 100644 --- a/src/utils/kim-request.js +++ b/src/utils/kim-request.js @@ -29,12 +29,12 @@ class HttpRequest { baseURL: this.options.baseUrl, // baseURL timeout: 10000, responseType: 'json', - 'x-requested-with': 'XMLHttpRequest', withCredentials: false, // default headers: { 'Access-Control-Allow-Origin': '*', - 'Content-Type': 'application/json', + 'content-type': 'application/json', Accept: 'application/json', + 'x-requested-with': 'XMLHttpRequest', ...this.options.headers, }, }; @@ -65,23 +65,24 @@ class HttpRequest { const { data, status } = res; return { data, status, statusText: codeMessage[status] }; }, - error => { - // error - this.destroy(url); - const errorResult = JSON.parse(JSON.stringify(error)); - 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 }); - } - } + 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 }); + // } + // } - return Promise.reject(errorResult); - }, + Promise.reject(error), ); } diff --git a/src/utils/request.js b/src/utils/request.js index f13f355af304a108e13992950f1097e899a08db2..c89f28b88f850f2c4e2981254af1bfd2cc71b55f 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -2,7 +2,15 @@ import { notification } from 'antd'; import HttpRequest from './kim-request'; 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({ * @param {返回请求数据} response * @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 = {}) { let newOptions = options;