import { getToken } from './token-util'; import { TOKEN_HEADER_NAME } from '@/config/setting'; // 获取图片反显url export function getImageUrl(path) { return `${sessionStorage.filePath}${path}`; // if (process.env.NODE_ENV === 'development') { // return `http://192.168.3.51:18086/main/file/getFile?${TOKEN_HEADER_NAME}=${getToken()}&objectName=${path}`; // } else { // return `${ // sessionStorage.filePath // }${path}`; // } } // 从反显url上获取接口需要path export function getImagePath(url) { if (!url) { return ''; } const match = url.match(/&objectName=([\S\s]*)/); return (match && match[1].split('&')[0]) || ''; } // 图片访问地址,兼容完整地址、服务端相对地址和文件存储相对路径 export function getFileAccessUrl(imageUrl, storePath) { if (imageUrl) { // 完整地址 if (/^(https?:)?\/\//i.test(imageUrl) || imageUrl.startsWith('data:')) { return imageUrl; } // 服务端相对地址,如 /api/main/file/getFile?objectName=xxx if (imageUrl.startsWith('/') && imageUrl.includes('?')) { return `${window.location.origin}${imageUrl}`; } // 文件存储相对路径 return getImageUrl(imageUrl); } return storePath ? getImageUrl(storePath) : ''; } // 下载方法 export function download(data, name) { const a = document.createElement('a'); const url = window.URL.createObjectURL(data); const filename = name; a.href = url; a.download = filename; a.click(); window.URL.revokeObjectURL(url); } export function download1(url, name) { const a = document.createElement('a'); const filename = name; a.href = url; a.download = filename; a.click(); }