puzhibing
6 天以前 0db8c9b94a9ac655549d5eba5c2a6c92ac386c06
culture/src/utils/utils.js
@@ -39,32 +39,81 @@
      }
    });
};
// export function getFullUrl(url) {
//   if (!url) return "";
//   if (/^https?:\/\//.test(url)) return url;
//   return getAllocateIp() + url;
// }
export function getFullUrl(url) {
  const origin = window.location.origin;
  console.log('图片:', url);
export function getUploadedFilePath(response) {
  const payload = response && response.data && typeof response.data === 'object'
    ? response.data
    : response;
  if (!url) return "";
  if (/^https?:\/\//.test(url)) return url;
  if (typeof payload === 'string') return payload.trim();
  if (!payload || typeof payload !== 'object') return '';
  console.log(origin.replace('8087','11223') + '/images/' + url);
  return origin.replace('8087','11223') + '/images/' + url; //todo 具体端口以线上为准
  // return getAllocateIp() + url;
  const nestedPath = payload.data && typeof payload.data === 'object'
    ? getUploadedFilePath(payload.data)
    : '';
  const candidates = [nestedPath, payload.data, payload.url, payload.fileUrl, payload.path, payload.msg];
  const successMessages = ['操作成功', '上传成功', 'success', 'ok'];
  return candidates.find(value => {
    return typeof value === 'string'
      && value.trim()
      && !successMessages.includes(value.trim().toLowerCase());
  }) || '';
}
export function normalizeFileList(value) {
  let files = value;
  if (typeof files === 'string') {
    const text = files.trim();
    if (!text) return [];
    try {
      files = JSON.parse(text);
    } catch (e) {
      files = [text];
    }
  }
  if (!Array.isArray(files)) files = files ? [files] : [];
  return files.map((file, index) => {
    const source = typeof file === 'string' ? { url: file } : (file || {});
    const url = source.url || source.fileUrl || source.path || source.filePath || '';
    const fallbackName = String(url).split(/[\\/]/).pop() || `附件${index + 1}`;
    return {
      ...source,
      name: source.name || source.fileName || fallbackName,
      url,
      uid: source.uid || source.id || `legacy-file-${index}-${url}`
    };
  }).filter(file => file.url);
}
export function getFullUrl(url) {
  if (!url) return "";
  const value = String(url).trim().replace(/\\/g, '/');
  if (/^(data|blob):/.test(value)) return value;
  const isAbsoluteUrl = /^(https?:)?\/\//.test(value);
  if (isAbsoluteUrl && !/^(https?:)?\/\/(localhost|127\.0\.0\.1)(:\d+)?\//i.test(value)) {
    return value;
  }
  const base = getAllocateIp().replace(/\/+$/, '') + '/';
  let path = value;
  if (isAbsoluteUrl) {
    const parsedUrl = new URL(value, window.location.origin);
    path = `${parsedUrl.pathname}${parsedUrl.search}${parsedUrl.hash}`;
  }
  path = path.replace(/^\/+/, '');
  if (/\/images\/$/.test(base) && path.startsWith('images/')) {
    path = path.slice('images/'.length);
  }
  return base + path;
}
/**
 * 通用文件下载方法
 * @param {string} url 文件的下载地址(支持相对和绝对路径)
 * @param {string} name 下载保存的文件名(可选)
 */
export function downloadFileByUrl(url, name) {
  console.log('22222222222222222222',url)
  if (!url) return;
  // 处理相对路径
  const fullUrl = getFullUrl(url);
@@ -104,6 +153,9 @@
export function getAllocateIp() {
  const userInfo = JSON.parse(sessionStorage.getItem("userInfo") || "{}");
  // return  apiConfig.showImgUrl
  return userInfo.allocateIp ? 'http://'+userInfo.allocateIp +':11223/images/' : apiConfig.showImgUrl;
  const allocateIp = String(userInfo.allocateIp || '').trim();
  const isLoopback = /^(localhost|127\.0\.0\.1)$/i.test(allocateIp);
  return allocateIp && !isLoopback
    ? `http://${allocateIp}:11223/images/`
    : apiConfig.showImgUrl;
}