Commit aa01d58c authored by duanledexianxianxian's avatar duanledexianxianxian 😁

sync code

parent 3c02868d
// import store from './utils/store'; // import store from './utils/store';
export default { export default {
baseUrl: 'http://platform.kuopu.net/9080', baseUrl: 'http://127.0.0.1:8080',
storeNameSpace: 'kim', storeNameSpace: 'kim',
headers: () => ({ headers: () => ({
Authorization: Authorization:
'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjEzMzQ2MDkwMTEsImxpZCI6Im91eWFuZ2JveXUifQ.eriHWClI-ST9CuEJuoU608KTKxIhf4XUxOSslzwT6K8', 'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMzLCJ1c24iOiLmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrofmrKfpmLPljZrlrociLCJzdGEiOjE1NjEzNDQyNDg5ODksImxpZCI6Im91eWFuZ2JveXUifQ.FbY-QQLgq8H9CWSo1FhCPgXZJQtte5lhAPC4W45mHmo',
}), }),
successCode: 'sys.success', // 后台正常返回错误编码
}; };
...@@ -40,10 +40,11 @@ export default { ...@@ -40,10 +40,11 @@ export default {
effects: { effects: {
// 获取列表数据 // 获取列表数据
*getList({ payload }, { call, put }) { *getList({ payload }, { call, put }) {
const { data } = yield call(service.getList, payload); const result = yield call(service.getList, payload);
console.log(result);
yield put({ yield put({
type: 'save', type: 'save',
payload: { ...data }, payload: { ...result.data },
}); });
}, },
// 增加 // 增加
......
...@@ -26,15 +26,15 @@ class HttpRequest { ...@@ -26,15 +26,15 @@ class HttpRequest {
getInsideConfig() { getInsideConfig() {
const config = { const config = {
baseURL: this.options.baseUrl, baseURL: this.options.baseUrl, // baseURL
headers: { timeout: 10000,
responseType: 'json',
'x-requested-with': 'XMLHttpRequest', 'x-requested-with': 'XMLHttpRequest',
withCredentials: false, // default
headers: {
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Accept: 'application/json', Accept: 'application/json',
withCredentials: false, // default
timeout: 10000,
responseType: 'json',
...this.options.headers, ...this.options.headers,
}, },
}; };
...@@ -60,11 +60,13 @@ class HttpRequest { ...@@ -60,11 +60,13 @@ class HttpRequest {
// 响应拦截 // 响应拦截
instance.interceptors.response.use( instance.interceptors.response.use(
res => { res => {
// success
this.destroy(url); this.destroy(url);
const { data, status } = res; const { data, status } = res;
return { data, status, statusText: codeMessage[status] }; return { data, status, statusText: codeMessage[status] };
}, },
error => { error => {
// error
this.destroy(url); this.destroy(url);
const errorResult = JSON.parse(JSON.stringify(error)); const errorResult = JSON.parse(JSON.stringify(error));
const errorInfo = error.response; const errorInfo = error.response;
......
...@@ -20,12 +20,11 @@ const errorHandler = error => { ...@@ -20,12 +20,11 @@ const errorHandler = error => {
} }
}; };
const axios = new HttpRequest({ /**
baseUrl, * 组装url
headers: headers() || {}, * @param {url} url
errorHandler, * @param {配置参数} more
}); */
const mergeApi = (url, more) => { const mergeApi = (url, more) => {
if (more && more.apiPrefix && typeof more.apiPrefix === 'string') { if (more && more.apiPrefix && typeof more.apiPrefix === 'string') {
return `${config.apiPrefix}${url}`; return `${config.apiPrefix}${url}`;
...@@ -36,45 +35,78 @@ const mergeApi = (url, more) => { ...@@ -36,45 +35,78 @@ const mergeApi = (url, more) => {
return url; 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 = {}) => const get = (url, data, more = {}) =>
axios.request({ request(
`${mergeApi(url, more)}`,
{
method: 'get', // default method: 'get', // default
url: `${mergeApi(url, more)}`,
params: data, params: data,
...more, },
}); more,
);
const post = (url, data, more = {}) => const post = (url, data, more = {}) =>
axios.request({ request(
`${mergeApi(url, more)}`,
{
method: 'post', // default method: 'post', // default
url: `${mergeApi(url, more)}`,
data, data,
...more, },
}); more,
);
const put = (url, data, more = {}) => const put = (url, data, more = {}) =>
axios.request({ request(
`${mergeApi(url, more)}`,
{
method: 'put', // default method: 'put', // default
url: `${mergeApi(url, more)}`,
data, data,
...more, },
}); more,
);
const del = (url, data, more = {}) => const del = (url, data, more = {}) =>
axios.request({ request(
`${mergeApi(url, more)}`,
{
method: 'delete', // default method: 'delete', // default
url: `${mergeApi(url, more)}`,
data, data,
...more, },
}); more,
);
const patch = (url, data, more = {}) => const patch = (url, data, more = {}) =>
axios.request({ request(
`${mergeApi(url, more)}`,
{
method: 'patch', // default method: 'patch', // default
url: `${mergeApi(url, more)}`,
data, data,
...more, ...more,
}); },
more,
);
const formDataUpload = (url, data, more = {}) => { const formDataUpload = (url, data, more = {}) => {
const formData = new FormData(); const formData = new FormData();
...@@ -83,10 +115,7 @@ const formDataUpload = (url, data, more = {}) => { ...@@ -83,10 +115,7 @@ const formDataUpload = (url, data, more = {}) => {
formData.append(key, data[key]); formData.append(key, data[key]);
}); });
} }
axios.request({ post(`${mergeApi(url, more)}`, formData, {
method: 'post', // default
url: `${mergeApi(url, more)}`,
data: formData,
headers: { headers: {
'Content-Type': 'multipart/form-data', 'Content-Type': 'multipart/form-data',
}, },
...@@ -100,4 +129,4 @@ const uploadFile = (url, data, type = 'formData', more = {}) => { ...@@ -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 };
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment