From 95db3ac1352e1f98ba934da9e86fca00e661abe9 Mon Sep 17 00:00:00 2001 From: zjk <852185829@qq.com> Date: 星期五, 12 九月 2025 15:01:07 +0800 Subject: [PATCH] feat: IP地址判断 --- laboratory/src/utils/utils.js | 48 ++++++++++++++++++++++++++++++------------------ 1 files changed, 30 insertions(+), 18 deletions(-) diff --git a/laboratory/src/utils/utils.js b/laboratory/src/utils/utils.js index ee15529..14aea06 100644 --- a/laboratory/src/utils/utils.js +++ b/laboratory/src/utils/utils.js @@ -1,5 +1,5 @@ -import axios from 'axios'; -import apiConfig from './baseurl'; +import axios from "axios"; +import apiConfig from "./baseurl"; export const customRequest = (params) => { const { file, onSuccess, onError } = params; @@ -12,30 +12,37 @@ }); }; -export const customUploadRequest = ({ file, onSuccess, onError, action, headers }) => { +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 + Authorization: sessionStorage.getItem("token") || "", + ...headers, }; const formData = new FormData(); - formData.append('file', file); + formData.append("file", file); - axios.post(uploadUrl, formData, { headers: uploadHeaders }) - .then(res => { + axios + .post(uploadUrl, formData, { headers: uploadHeaders }) + .then((res) => { onSuccess(res.data); }) - .catch(err => { + .catch((err) => { onError(err); }); }; export function getFullUrl(url) { - if (!url) return ''; + if (!url) return ""; if (/^https?:\/\//.test(url)) return url; - return apiConfig.showImgUrl + url; + return getAllocateIp() + url; } /** @@ -44,19 +51,19 @@ * @param {string} name 下载保存的文件名(可选) */ export function downloadFileByUrl(url, name) { - console.log('22222222222222222222',url) + console.log("22222222222222222222", url); if (!url) return; // 处理相对路径 const fullUrl = getFullUrl(url); - console.log('fullUrl fullUrl',fullUrl) - const a = document.createElement('a'); + console.log("fullUrl fullUrl", fullUrl); + const a = document.createElement("a"); a.href = fullUrl; if (name) { a.download = name; } else { - a.download = ''; + a.download = ""; } - a.style.display = 'none'; + a.style.display = "none"; document.body.appendChild(a); a.click(); document.body.removeChild(a); @@ -69,5 +76,10 @@ export function openFileInNewTab(url) { if (!url) return; const fullUrl = getFullUrl(url); - window.open(fullUrl, '_blank'); -} \ No newline at end of file + window.open(fullUrl, "_blank"); +} + +export function getAllocateIp() { + const userInfo = JSON.parse(sessionStorage.getItem("userInfo") || "{}"); + return userInfo.allocateIp ? userInfo.userInfo : apiConfig.showImgUrl; +} -- Gitblit v1.7.1