From aa01d58c996fbaf15719677cf03135294a2e2bea Mon Sep 17 00:00:00 2001 From: duanledexianxianxian Date: Mon, 24 Jun 2019 17:35:12 +0800 Subject: [PATCH] sync code --- src/config.js | 9 +- src/pages/system/parameter/models/business.js | 5 +- src/utils/kim-request.js | 12 +- src/utils/request.js | 111 +++++++++++------- 4 files changed, 85 insertions(+), 52 deletions(-) diff --git a/src/config.js b/src/config.js index a9d295c7..2e189ec2 100644 --- a/src/config.js +++ b/src/config.js @@ -1,10 +1,11 @@ // import store from './utils/store'; export default { - baseUrl: 'http://platform.kuopu.net/9080', + baseUrl: 'http://127.0.0.1:8080', storeNameSpace: 'kim', headers: () => ({ - Authorization: - 'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjEzMzQ2MDkwMTEsImxpZCI6Im91eWFuZ2JveXUifQ.eriHWClI-ST9CuEJuoU608KTKxIhf4XUxOSslzwT6K8', - }), + Authorization: + 'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjEzNDQyNDg5ODksImxpZCI6Im91eWFuZ2JveXUifQ.FbY-QQLgq8H9CWSo1FhCPgXZJQtte5lhAPC4W45mHmo', + }), + successCode: 'sys.success', // 后台正常返回错误编码 }; diff --git a/src/pages/system/parameter/models/business.js b/src/pages/system/parameter/models/business.js index e0a015a0..85b0ea42 100644 --- a/src/pages/system/parameter/models/business.js +++ b/src/pages/system/parameter/models/business.js @@ -40,10 +40,11 @@ export default { effects: { // 获取列表数据 *getList({ payload }, { call, put }) { - const { data } = yield call(service.getList, payload); + const result = yield call(service.getList, payload); + console.log(result); yield put({ type: 'save', - payload: { ...data }, + payload: { ...result.data }, }); }, // 增加 diff --git a/src/utils/kim-request.js b/src/utils/kim-request.js index a04d991a..529c995d 100644 --- a/src/utils/kim-request.js +++ b/src/utils/kim-request.js @@ -26,15 +26,15 @@ class HttpRequest { getInsideConfig() { const config = { - baseURL: this.options.baseUrl, + baseURL: this.options.baseUrl, // baseURL + timeout: 10000, + responseType: 'json', + 'x-requested-with': 'XMLHttpRequest', + withCredentials: false, // default headers: { - 'x-requested-with': 'XMLHttpRequest', 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json', Accept: 'application/json', - withCredentials: false, // default - timeout: 10000, - responseType: 'json', ...this.options.headers, }, }; @@ -60,11 +60,13 @@ class HttpRequest { // 响应拦截 instance.interceptors.response.use( res => { + // success this.destroy(url); 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; diff --git a/src/utils/request.js b/src/utils/request.js index 01836c95..f13f355a 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -20,12 +20,11 @@ const errorHandler = error => { } }; -const axios = new HttpRequest({ - baseUrl, - headers: headers() || {}, - errorHandler, -}); - +/** + * 组装url + * @param {url} url + * @param {配置参数} more + */ const mergeApi = (url, more) => { if (more && more.apiPrefix && typeof more.apiPrefix === 'string') { return `${config.apiPrefix}${url}`; @@ -36,45 +35,78 @@ const mergeApi = (url, more) => { return url; }; +const axios = new HttpRequest({ + baseUrl, + headers: headers() || {}, + errorHandler, +}); + +/** + * 正常返回结果处理 + * @param {返回请求数据} response + * @param {配置项} more + */ +const checkCode = (response, more) => {}; + +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)); +} + const get = (url, data, more = {}) => - axios.request({ - method: 'get', // default - url: `${mergeApi(url, more)}`, - params: data, - ...more, - }); + request( + `${mergeApi(url, more)}`, + { + method: 'get', // default + params: data, + }, + more, + ); const post = (url, data, more = {}) => - axios.request({ - method: 'post', // default - url: `${mergeApi(url, more)}`, - data, - ...more, - }); + request( + `${mergeApi(url, more)}`, + { + method: 'post', // default + data, + }, + more, + ); const put = (url, data, more = {}) => - axios.request({ - method: 'put', // default - url: `${mergeApi(url, more)}`, - data, - ...more, - }); + request( + `${mergeApi(url, more)}`, + { + method: 'put', // default + data, + }, + more, + ); const del = (url, data, more = {}) => - axios.request({ - method: 'delete', // default - url: `${mergeApi(url, more)}`, - data, - ...more, - }); + request( + `${mergeApi(url, more)}`, + { + method: 'delete', // default + data, + }, + more, + ); const patch = (url, data, more = {}) => - axios.request({ - method: 'patch', // default - url: `${mergeApi(url, more)}`, - data, - ...more, - }); + request( + `${mergeApi(url, more)}`, + { + method: 'patch', // default + data, + ...more, + }, + more, + ); const formDataUpload = (url, data, more = {}) => { const formData = new FormData(); @@ -83,10 +115,7 @@ const formDataUpload = (url, data, more = {}) => { formData.append(key, data[key]); }); } - axios.request({ - method: 'post', // default - url: `${mergeApi(url, more)}`, - data: formData, + post(`${mergeApi(url, more)}`, formData, { headers: { 'Content-Type': 'multipart/form-data', }, @@ -100,4 +129,4 @@ const uploadFile = (url, data, type = 'formData', more = {}) => { } }; -export { axios as request, get, post, put, del, patch, uploadFile }; +export { request, get, post, put, del, patch, uploadFile }; -- GitLab