diff --git a/src/config.js b/src/config.js index a9d295c77e0140e087f9b68ed4a9df97ccde848a..2e189ec2f57937a40a6664db9934e81d5f66fa07 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 e0a015a0432b419df6578b188cc7b09d94524b2a..85b0ea4256b70f6b056834ea3f8f2150d03d38bb 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 a04d991a2706fc03e28c170dbf2c7e17e9b31fdb..529c995d88f0a83ab3f5744930b8b0fc30646e0f 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 01836c951b508a97ba1ed7203d2e0ec05e8116ad..f13f355af304a108e13992950f1097e899a08db2 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 };