|
@@ -1,11 +1,12 @@
|
|
|
|
+/* eslint-disable require-atomic-updates */
|
|
/* eslint-disable no-console */
|
|
/* eslint-disable no-console */
|
|
/* eslint-disable no-param-reassign */
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
|
|
import _ from 'lodash';
|
|
import _ from 'lodash';
|
|
import Axios from 'axios';
|
|
import Axios from 'axios';
|
|
import { Util, Error } from 'naf-core';
|
|
import { Util, Error } from 'naf-core';
|
|
-// import { Indicator } from 'mint-ui';
|
|
|
|
-import util from './user-util';
|
|
|
|
|
|
+import { Loading } from 'element-ui';
|
|
|
|
+import UserUtil from './user-util';
|
|
|
|
|
|
const { trimData, isNullOrUndefined } = Util;
|
|
const { trimData, isNullOrUndefined } = Util;
|
|
const { ErrorCode } = Error;
|
|
const { ErrorCode } = Error;
|
|
@@ -42,17 +43,21 @@ export default class AxiosWrapper {
|
|
return this.$request(uri, null, query, options);
|
|
return this.$request(uri, null, query, options);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ $delete(uri, query, options = {}) {
|
|
|
|
+ return this.$request(uri, null, query, { ...options, method: 'delete' });
|
|
|
|
+ }
|
|
|
|
+
|
|
$post(uri, data = {}, query, options) {
|
|
$post(uri, data = {}, query, options) {
|
|
return this.$request(uri, data, query, options);
|
|
return this.$request(uri, data, query, options);
|
|
}
|
|
}
|
|
- $delete(uri, data = {}, router, query, options = {}) {
|
|
|
|
- options = { ...options, method: 'delete' };
|
|
|
|
- return this.$request(uri, data, query, options, router);
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
async $request(uri, data, query, options) {
|
|
async $request(uri, data, query, options) {
|
|
- // TODO: 合并query和options
|
|
|
|
|
|
+ // 过滤key:''的情况
|
|
|
|
+ query = _.pickBy(query, val => val !== '' && val !== 'undefined' && val !== 'null');
|
|
|
|
+ if (!uri) console.error('uri不能为空');
|
|
if (_.isObject(query) && _.isObject(options)) {
|
|
if (_.isObject(query) && _.isObject(options)) {
|
|
- options = { ...options, params: query, method: 'get' };
|
|
|
|
|
|
+ const params = query.params ? query.params : query;
|
|
|
|
+ options = { ...options, params };
|
|
} else if (_.isObject(query) && !query.params) {
|
|
} else if (_.isObject(query) && !query.params) {
|
|
options = { params: query };
|
|
options = { params: query };
|
|
} else if (_.isObject(query) && query.params) {
|
|
} else if (_.isObject(query) && query.params) {
|
|
@@ -61,16 +66,18 @@ export default class AxiosWrapper {
|
|
if (!options) options = {};
|
|
if (!options) options = {};
|
|
if (options.params) options.params = trimData(options.params);
|
|
if (options.params) options.params = trimData(options.params);
|
|
const url = AxiosWrapper.merge(uri, options.params);
|
|
const url = AxiosWrapper.merge(uri, options.params);
|
|
|
|
+
|
|
currentRequests += 1;
|
|
currentRequests += 1;
|
|
- // Indicator.open({
|
|
|
|
- // spinnerType: 'fading-circle',
|
|
|
|
- // });
|
|
|
|
|
|
+ // const loadingInstance = Loading.service({ spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.5)' });
|
|
|
|
|
|
try {
|
|
try {
|
|
const axios = Axios.create({
|
|
const axios = Axios.create({
|
|
baseURL: this.baseUrl,
|
|
baseURL: this.baseUrl,
|
|
});
|
|
});
|
|
- axios.defaults.headers.common.Authorization = util.token;
|
|
|
|
|
|
+ const user = localStorage.getItem('user');
|
|
|
|
+ if (user) {
|
|
|
|
+ axios.defaults.headers.common.Authorization = encodeURI(user);
|
|
|
|
+ }
|
|
let res = await axios.request({
|
|
let res = await axios.request({
|
|
method: isNullOrUndefined(data) ? 'get' : 'post',
|
|
method: isNullOrUndefined(data) ? 'get' : 'post',
|
|
url,
|
|
url,
|
|
@@ -78,15 +85,15 @@ export default class AxiosWrapper {
|
|
responseType: 'json',
|
|
responseType: 'json',
|
|
...options,
|
|
...options,
|
|
});
|
|
});
|
|
- res = res.data;
|
|
|
|
|
|
+ res = res.data || {};
|
|
const { errcode, errmsg, details } = res;
|
|
const { errcode, errmsg, details } = res;
|
|
if (errcode) {
|
|
if (errcode) {
|
|
console.warn(`[${uri}] fail: ${errcode}-${errmsg} ${details}`);
|
|
console.warn(`[${uri}] fail: ${errcode}-${errmsg} ${details}`);
|
|
- return res;
|
|
|
|
|
|
+ return undefined;
|
|
}
|
|
}
|
|
// unwrap data
|
|
// unwrap data
|
|
if (this.unwrap) {
|
|
if (this.unwrap) {
|
|
- res = _.omit(res, ['errmsg', 'details']);
|
|
|
|
|
|
+ res = _.omit(res, ['errcode', 'errmsg', 'details']);
|
|
const keys = Object.keys(res);
|
|
const keys = Object.keys(res);
|
|
if (keys.length === 1 && keys.includes('data')) {
|
|
if (keys.length === 1 && keys.includes('data')) {
|
|
res = res.data;
|
|
res = res.data;
|
|
@@ -96,21 +103,32 @@ export default class AxiosWrapper {
|
|
} catch (err) {
|
|
} catch (err) {
|
|
let errmsg = '接口请求失败,请稍后重试';
|
|
let errmsg = '接口请求失败,请稍后重试';
|
|
if (err.response) {
|
|
if (err.response) {
|
|
- const { status } = err.response;
|
|
|
|
|
|
+ const { status, data = {} } = err.response;
|
|
|
|
+ console.log(err.response);
|
|
if (status === 401) errmsg = '用户认证失败,请重新登录';
|
|
if (status === 401) errmsg = '用户认证失败,请重新登录';
|
|
if (status === 403) errmsg = '当前用户不允许执行该操作';
|
|
if (status === 403) errmsg = '当前用户不允许执行该操作';
|
|
|
|
+ if (status === 400 && data.errcode) {
|
|
|
|
+ const { errcode, errmsg, details } = data;
|
|
|
|
+ console.warn(`[${uri}] fail: ${errcode}-${errmsg} ${details}`);
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+ if (data && data.error) {
|
|
|
|
+ const { status, error, message } = data;
|
|
|
|
+ console.warn(`[${uri}] fail: ${status}: ${error}-${message}`);
|
|
|
|
+ return {
|
|
|
|
+ errcode: status || ErrorCode.SERVICE_FAULT,
|
|
|
|
+ errmsg: error,
|
|
|
|
+ details: message,
|
|
|
|
+ };
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- console.error(
|
|
|
|
- `[AxiosWrapper] 接口请求失败: ${err.config && err.config.url} -
|
|
|
|
- ${err.message}`
|
|
|
|
- );
|
|
|
|
|
|
+ console.error(`[AxiosWrapper] 接口请求失败: ${err.config && err.config.url} - ${err.message}`);
|
|
return { errcode: ErrorCode.SERVICE_FAULT, errmsg, details: err.message };
|
|
return { errcode: ErrorCode.SERVICE_FAULT, errmsg, details: err.message };
|
|
} finally {
|
|
} finally {
|
|
- /* eslint-disable */
|
|
|
|
currentRequests -= 1;
|
|
currentRequests -= 1;
|
|
if (currentRequests <= 0) {
|
|
if (currentRequests <= 0) {
|
|
currentRequests = 0;
|
|
currentRequests = 0;
|
|
- // Indicator.close();
|
|
|
|
|
|
+ // loadingInstance.close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|