pyt
2025-07-01 be31adc8150e5b21008aa7d6212fc105fc425818
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
}
 
/**
 * 通用文件下载方法
 * @param {string} url 文件的下载地址(支持相对和绝对路径)
 * @param {string} name 下载保存的文件名(可选)
 */
export function downloadFileByUrl(url, name) {
  console.log('22222222222222222222',url)
  if (!url) return;
  // 处理相对路径
  const fullUrl = getFullUrl(url);
  console.log('fullUrl fullUrl',fullUrl)
  const a = document.createElement('a');
  a.href = fullUrl;
  if (name) {
    a.download = name;
  } else {
    a.download = '';
  }
  a.style.display = 'none';
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
}
 
/**
 * 在新标签页打开文件(预览或下载)
 * @param {string} url 文件地址(支持相对和绝对路径)
 */
export function openFileInNewTab(url) {
  if (!url) return;
  const fullUrl = getFullUrl(url);
  window.open(fullUrl, '_blank');
}