fix: request plugin 剔除对组件库的依赖

This commit is contained in:
bac-joker 2020-12-22 15:29:29 +08:00
parent 21f4f66504
commit 9398e89822
2 changed files with 10 additions and 26 deletions

View File

@ -15,8 +15,7 @@ export default (api) => {
});
},
default: {
dataField: 'result',
messageUI: 'ant-design-vue'
dataField: 'result'
}
}
});
@ -26,11 +25,10 @@ export default (api) => {
const requestTemplate = readFileSync(join(__dirname, 'template', 'request.js'), 'utf-8');
api.onGenerateFiles(() => {
// 文件写出
const { dataField = '', messageUI } = api.config.request;
const { dataField = '' } = api.config.request;
api.writeTmpFile({
path: absoluteFilePath,
content: requestTemplate
.replace('REPLACE_MESSAGE_UI', messageUI || 'ant-design-vue')
.replace('REPLACE_DATA_FIELD', dataField)
});
});

View File

@ -1,12 +1,10 @@
import axios from 'axios';
// import { debounce } from 'throttle-debounce';
import { message } from 'REPLACE_MESSAGE_UI';
import { ApplyPluginsType, plugin, router } from '@webank/fes';
import {
checkHttpRequestHasBody,
trimObj,
isString,
isFunction,
isObject
} from './helpers';
@ -17,28 +15,15 @@ import {
* {
* errorMessage: '', // 错误地址
* errorPage: '', // 错误页面地址
* showType: 1 // 0 不提示 | 1 警告 | 2 错误 | 9 页面跳转
* }
*/
function errrorHandler(error) {
function _errorHandler(error, customerErrorHandler) {
if (isFunction(error)) {
error();
} else if (isString(error)) {
message.error(error);
} else if (isObject(error)) {
switch (error.showType) {
case 1:
message.warning(error.errorMessage);
break;
case 2:
message.error(error.errorMessage);
break;
case 9:
router.replace(error.errorPage);
break;
default:
throw new Error(error);
}
} else if (error.errorPage) {
router.replace(error.errorPage);
} else {
customerErrorHandler && customerErrorHandler(error);
}
}
@ -66,6 +51,7 @@ function getRequestInstance() {
errorConfig,
requestInterceptors,
responseInterceptors,
errorHandler,
...otherConfigs
} = plugin.applyPlugins({
key: 'request',
@ -100,7 +86,7 @@ function getRequestInstance() {
[
function (response) {
if (isObject(response.data) && response.data.code !== '0') {
errrorHandler(_errorConfig[response.data.code] || response.data.msg || response.data.errorMessage || response.data.errorMsg || '服务异常');
_errorHandler(_errorConfig[response.data.code] || response.data.msg || response.data.errorMessage || response.data.errorMsg || '服务异常', errorHandler);
return Promise.reject(response);
}
@ -110,7 +96,7 @@ function getRequestInstance() {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
if (_errorConfig[error.response.status]) {
errrorHandler(_errorConfig[error.response.status]);
_errorHandler(_errorConfig[error.response.status], errorHandler);
}
}
return Promise.reject(error);