file.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { getToken } from './token-util';
  2. import { TOKEN_HEADER_NAME } from '@/config/setting';
  3. // 获取图片反显url
  4. export function getImageUrl(path) {
  5. if (!path) {
  6. return;
  7. }
  8. let fileNames = path.split('/');
  9. let url =
  10. window.location.origin +
  11. '/api/main/file/getFile?objectName=' +
  12. path +
  13. '&fullfilename=' +
  14. fileNames[fileNames.length - 1];
  15. return url;
  16. // return `${sessionStorage.filePath}${path}`;
  17. // if (process.env.NODE_ENV === 'development') {
  18. // return `http://192.168.3.51:18086/main/file/getFile?${TOKEN_HEADER_NAME}=${getToken()}&objectName=${path}`;
  19. // } else {
  20. // return `${
  21. // sessionStorage.filePath
  22. // }${path}`;
  23. // }
  24. }
  25. // 从反显url上获取接口需要path
  26. export function getImagePath(url) {
  27. if (!url) {
  28. return '';
  29. }
  30. const match = url.match(/&objectName=([\S\s]*)/);
  31. return (match && match[1].split('&')[0]) || '';
  32. }
  33. // 下载方法
  34. export function download(data, name) {
  35. const a = document.createElement('a');
  36. const url = window.URL.createObjectURL(data);
  37. const filename = name;
  38. a.href = url;
  39. a.download = filename;
  40. a.click();
  41. window.URL.revokeObjectURL(url);
  42. }