From 42feb0af0ae1d486d0474c76711fdb67c778bcf3 Mon Sep 17 00:00:00 2001 From: 董国庆 <364620639@qq.com> Date: 星期六, 28 六月 2025 10:04:30 +0800 Subject: [PATCH] Merge branch 'main' of http://120.76.84.145:10101/gitblit/r/H5/leshan-laboratory --- culture/src/utils/utils.js | 75 +++++++++++++++++++++++++++++++++++++ 1 files changed, 74 insertions(+), 1 deletions(-) diff --git a/culture/src/utils/utils.js b/culture/src/utils/utils.js index a1019e4..88ec0ff 100644 --- a/culture/src/utils/utils.js +++ b/culture/src/utils/utils.js @@ -1,3 +1,6 @@ +import axios from 'axios'; +import apiConfig from './baseurl'; + export const customRequest = (params) => { const { file, onSuccess, onError } = params; uploadObs(file) @@ -7,4 +10,74 @@ .catch((ret) => { onError(); }); -}; \ No newline at end of file +}; + +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 => { + console.log('qweqweqweqw/////',res); + + try { + onSuccess(res.data); + } catch (e) { + console.error('onSuccess error:', e); + } + }) + .catch(err => { + try { + onError(err, file); + } catch (e) { + console.error('onError error:', e); + } + }); +}; + +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'); +} \ No newline at end of file -- Gitblit v1.7.1