vue.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // @ts-nocheck
  2. // #ifdef APP-PLUS
  3. import { getLocalFilePath } from '../getLocalFilePath'
  4. // #endif
  5. function isImage(extension : string) {
  6. const imageExtensions = ["jpg", "jpeg", "png", "gif", "bmp", "svg"];
  7. return imageExtensions.includes(extension.toLowerCase());
  8. }
  9. // #ifdef H5
  10. function getSVGFromURL(url: string) {
  11. return new Promise((resolve, reject) => {
  12. const xhr = new XMLHttpRequest();
  13. xhr.open('GET', url, true);
  14. xhr.responseType = 'text';
  15. xhr.onload = function () {
  16. if (xhr.status === 200) {
  17. const svg = xhr.responseText;
  18. resolve(svg);
  19. } else {
  20. reject(new Error(xhr.statusText));
  21. }
  22. };
  23. xhr.onerror = function () {
  24. reject(new Error('Network error'));
  25. };
  26. xhr.send();
  27. });
  28. }
  29. // #endif
  30. /**
  31. * 路径转base64
  32. * @param {Object} string
  33. */
  34. export function pathToBase64(path : string) : Promise<string> {
  35. if (/^data:/.test(path)) return path
  36. let extension = path.substring(path.lastIndexOf('.') + 1);
  37. const isImageFile = isImage(extension)
  38. let prefix = ''
  39. if (isImageFile) {
  40. prefix = 'image/';
  41. if(extension == 'svg') {
  42. extension += '+xml'
  43. }
  44. } else if (extension === 'pdf') {
  45. prefix = 'application/pdf';
  46. } else if (extension === 'txt') {
  47. prefix = 'text/plain';
  48. } else {
  49. // 添加更多文件类型的判断
  50. // 如果不是图片、PDF、文本等类型,可以设定默认的前缀或采取其他处理
  51. prefix = 'application/octet-stream';
  52. }
  53. return new Promise((resolve, reject) => {
  54. // #ifdef H5
  55. if (isImageFile) {
  56. if(extension == 'svg') {
  57. getSVGFromURL(path).then(svg => {
  58. const base64 = btoa(svg);
  59. resolve(`data:image/svg+xml;base64,${base64}`);
  60. })
  61. } else {
  62. let image = new Image();
  63. image.setAttribute("crossOrigin", 'Anonymous');
  64. image.onload = function () {
  65. let canvas = document.createElement('canvas');
  66. canvas.width = this.naturalWidth;
  67. canvas.height = this.naturalHeight;
  68. canvas.getContext('2d').drawImage(image, 0, 0);
  69. let result = canvas.toDataURL(`${prefix}${extension}`)
  70. resolve(result);
  71. canvas.height = canvas.width = 0
  72. }
  73. image.src = path + '?v=' + Math.random()
  74. image.onerror = (error) => {
  75. reject(error);
  76. };
  77. }
  78. } else {
  79. reject('not image');
  80. }
  81. // #endif
  82. // #ifdef MP
  83. if (uni.canIUse('getFileSystemManager')) {
  84. uni.getFileSystemManager().readFile({
  85. filePath: path,
  86. encoding: 'base64',
  87. success: (res) => {
  88. resolve(`data:${prefix}${extension};base64,${res.data}`)
  89. },
  90. fail: (error) => {
  91. console.error({ error, path })
  92. reject(error)
  93. }
  94. })
  95. }
  96. // #endif
  97. // #ifdef APP-PLUS
  98. plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), (entry) => {
  99. entry.file((file : any) => {
  100. const fileReader = new plus.io.FileReader()
  101. fileReader.onload = (data) => {
  102. resolve(data.target.result)
  103. }
  104. fileReader.onerror = (error) => {
  105. console.error({ error, path })
  106. reject(error)
  107. }
  108. fileReader.readAsDataURL(file)
  109. }, reject)
  110. }, reject)
  111. // #endif
  112. })
  113. }