import axios from 'axios';
|
import apiConfig from './baseurl';
|
|
export const customRequest = (params) => {
|
const { file, onSuccess, onError } = params;
|
uploadObs(file)
|
.then((ret) => {
|
onSuccess(ret);
|
})
|
.catch((ret) => {
|
onError();
|
});
|
};
|
|
export const customUploadRequest = ({ file, onSuccess, onError, action, headers }) => {
|
// 默认 action 为 apiConfig.imgUrl
|
const uploadUrl = action || apiConfig.imgUrl;
|
// 默认 headers 带上 Authorization
|
const uploadHeaders = {
|
Authorization: sessionStorage.getItem('token') || '',
|
...headers
|
};
|
const formData = new FormData();
|
formData.append('file', file);
|
|
axios.post(uploadUrl, formData, { headers: uploadHeaders })
|
.then(res => {
|
onSuccess(res.data);
|
})
|
.catch(err => {
|
onError(err);
|
});
|
};
|
|
export function getFullUrl(url) {
|
if (!url) return '';
|
if (/^https?:\/\//.test(url)) return url;
|
return apiConfig.showImgUrl + url;
|
}
|