| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- 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();
- }
|