用法
import Exceed from '@alife/at-exceed';
const apimap = [
{
id: 'memberSearch',
url: '/api/members/search',
method: 'get',
timeout: 2000, // 设置接口超时时间
},
];
// 对apimap做一次封装,最简只需定义id和url字段
function apimapCreator(apimapArr) {
// 在这里可以对请求做一些统一处理,如加前缀、加headers
return apimapArr.map(option => Object.assign(option, {
name: option.name || option.id,
id: option.id,
method: option.method || 'get',
contentType: option.type || 'application/x-www-form-urlencoded',
urls: {
local: `${window.UILessConfig.host}${option.url}`,
},
timeout: option.timeout || 3000, // 设置接口超时时间
}));
}
const exceed = new Exceed({
ENV: 'local',
timeout: 3000, // 设置默认超时时间
});
exceed.use(apimapCreator(apimap));
exceed.interceptors.request.use((config) => {
if (config && config.headers) {
config.headers['x-requested-with'] = 'XMLHttpRequest';
config.headers['Authorization'] = `Bearer ${window.UILessConfig.accessToken}`;
config.headers['X-Tenant-Id'] = `${window.UILessConfig.orgId}`;
config.headers['X-app-id'] = `${window.UILessConfig.appId}`;
}
return config;
}, (error) => {
return Promise.reject(error);
});
exceed.interceptors.response.use(
(response) => {
response.status = parseInt(response.headers['x-http-status'] || response.status);
const code = parseInt(response.data.code);
if (code > 300 || code < 200) {
return Promise.reject(response);
}
return Promise.resolve(response);
},
(error) => {
return Promise.reject(error);
},
);
export default exceed;
import exceed from 'utils/api';
exceed.fetch({
api: 'memberSearch',
data: {
q,
}
}).then(res => {
console.log(res)
})
注意
如果method
为post
且contentType
为'application/x-www-form-urlencoded'
,可通过qsOtions
设置stringify参数的选项.
const apimap = [
{
id: 'memberSearch',
url: '/api/members/search',
method: 'post',
contentType: 'application/x-www-form-urlencoded',
qsOptions: {
allowDots: true,
},
},
];