一般将全局request写到utils文件中,再services文件夹创建各个模块的请求路径
export const request = (
opt: any = {},
contentType?: string,
// 非关键接口,401错误,也不跳转到登录页面,由其他接口判断,目前Header组件中4个接口
notToLogin?: boolean
): Promise<any> => {
let notificationKey = '';
let nameList: string[] = [];
// 判断是否是下载接口,下载接口需要加进度条
// console.log('opt', opt);
if (
opt.url.includes('/file/download') ||
opt.url.includes('guideEntry/downloadFileByName') ||
opt.url.includes(
'customApplication/exportMyDownload' ||
opt.url.includes('/file/downloadWithWatermark')
)
) {
const requestPayload = opt.data;
// 先获取下载的数据
if (
Array.isArray(requestPayload) &&
!opt.url.includes('customApplication/exportMyDownload')
) {
requestPayload.forEach((item: string) => {
const urls = item.split('/');
nameList.push(
decodeURIComponent(urls[urls.length - 1]).replace(/\+/g, ' ')
);
});
} else if (typeof requestPayload === 'string') {
nameList.push(decodeURIComponent(requestPayload).replace(/\+/g, ' '));
} else if (opt.url.includes('/file/downloadWithWatermark')) {
//水印下载
nameList = [' '];
}
// 下载时,展示进度条
if (Array.isArray(nameList) && nameList.length > 0) {
notificationKey = `${nameList.join('-')}-${new Date().valueOf()}`;
notification.open({
key: notificationKey,
message: intl.formatMessage({ id: 'Downloading' }),
description: `${nameList.join('、')}`,
duration: 0,
placement: 'bottomRight',
className: 'notification',
icon: <ExclamationCircleOutlined />
});
}
}
return axios({
...opt,
params:
String(opt.method).toUpperCase() === 'GET' ?
opt.params || opt.data || {} :
{},
timeout: opt && opt.timeout ? opt.timeout : 1000 * 30 * 60,
headers: {
'Content-Type': contentType || 'application/json',
Authorization: `Bearer ${
(window?.name &&
JSON.parse(decodeURIComponent(window?.name))?.newsToken) ||
getCookie('web__token')
}`,
'Accept-Language': getCookie('web__language') || 'zh-CN',
// 编译时,如果是国内服务器,根据编译指令,注入zh-CN,如果是海外服务器,注入not-zh-CN
'split-separator': BUILD_ENV || 'zh-CN',
// 区分前后台调用
fromFront: true,
'Cache-Control': 'no-cache'
},
onDownloadProgress(progressEvent) {
if (
notificationKey &&
progressEvent.loaded < progressEvent.total &&
nameList.length > 0
) {
// 如果下载未完成,展示进度显示
const percent = Math.floor(
(progressEvent.loaded / progressEvent.total) * 100
);
notification.open({
key: notificationKey,
message: `${intl.formatMessage({ id: 'Downloading' })}-${percent}%`,
description: `${nameList.join('、')}`,
duration: 0,
placement: 'bottomRight',
className: 'notification',
icon: <ExclamationCircleOutlined />
});
} else if (
notificationKey &&
progressEvent.loaded === progressEvent.total &&
nameList.length > 0
) {
// 如果下载完成,清除进度
notification.close(notificationKey);
} else if (progressEvent.loaded > progressEvent.total) {
notification.close(notificationKey);
}
}
}).then(
(result: any) => {
const queryParams = window.location.href.split('/');
const { status, data } = result || {};
if (status === 200 && data.code === -1) {
!window?.name && deleteCookie('web__token');
removeLocalStorage('__userInfo');
if (!notToLogin && !window?.name) {
if (queryParams[2] === 'nb.hahah.com') {
history.push('/NbLogin');
} else {
history.push('/Login');
}
}
return data;
} else if (status === 200) {
return data;
} else {
message.error(
data.msg || intl.formatMessage({ id: 'Network request error' })
);
return data;
}
},
(error: any) => {
const { data, status, statusText } = error.response || {};
const {
status: requestStatus,
response,
responseURL,
statusText: requestStatusText
} = error.request || {};
if (
status === 401 &&
statusText === 'Unauthorized' &&
data &&
(data.code === -1 || data.code === 1)
) {
!window?.name && deleteCookie('web__token');
removeLocalStorage('__userInfo');
if (!notToLogin && !window?.name) {
const queryParams = window.location.href.split('/');
if (queryParams[2] === 'nb.hahah.com') {
history.push('/NbLogin');
} else {
history.push('/Login');
}
}
} else if (
requestStatus === 0 &&
!response &&
!responseURL &&
!requestStatusText &&
error.message &&
error.message.indexOf('timeout') > -1
) {
return {
code: -1,
msg: intl.formatMessage({
id: 'Request timed out. Please check network and then try again'
})
};
} else if (
requestStatus === 0 &&
!response &&
!responseURL &&
!requestStatusText &&
error.message === 'Network Error' &&
error.config.data instanceof FormData
) {
return {
code: -1,
msg: intl.formatMessage({
id: 'Please check that the resource file is available'
})
};
} else if (
requestStatus === 0 &&
!response &&
!responseURL &&
!requestStatusText &&
error.message === 'Network Error'
) {
return {
code: -1,
msg: intl.formatMessage({
id: 'Please check network and then try again'
})
};
}
return data;
}
);
};
import { request } from '@/utils/request';
import { requestBase } from '@/config/config';
// 查询关联banner
export const getBondList = (data: any) => {
return request({
method: 'GET',
url: `${requestBase}/system-svc/centralAnnouncementBanner/queryAnnouncementListBannerId?bannerId=${data}`
});
};