From 3c1cb6ed0b41d5ce7f1398797b7dde7550f40686 Mon Sep 17 00:00:00 2001 From: duanledexianxianxian Date: Tue, 25 Jun 2019 08:39:18 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=82=E3=80=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.js | 8 ++++++++ src/config.js | 3 +++ src/utils/kim-request.js | 37 +++++++++++++++++++------------------ src/utils/request.js | 25 +++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 src/app.js diff --git a/src/app.js b/src/app.js new file mode 100644 index 00000000..a12847b6 --- /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 2e189ec2..8649a6b9 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 529c995d..a2f89e4e 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 f13f355a..c89f28b8 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; -- GitLab