董国庆
2025-06-20 0204d3e40681c2a4f50df373012d8cb226d98966
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
}