file.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import axios from 'axios';
  2. import { MessagePlugin } from 'tdesign-vue-next';
  3. import i18n from '../i18n';
  4. const $t = i18n.global.t;
  5. export async function upload(
  6. blob: Blob,
  7. shared = false,
  8. filename = 'thumb.png',
  9. directory = '/大屏/图片/默认',
  10. conflict?:string
  11. ) {
  12. const form = new FormData();
  13. let name = filename;
  14. if(!name.includes('.')) {
  15. name += '.png';
  16. }
  17. form.append('name', name);
  18. form.append('directory', directory);
  19. form.append('shared', shared + '');
  20. form.append('file', blob);
  21. // let ret;
  22. if(conflict) {
  23. // ret = await axios.post('/api/image/upload',{conflict}, form);
  24. form.append('conflict', conflict);
  25. }
  26. const ret = await axios.post('/api/image/upload', form);
  27. if (!ret) {
  28. return;
  29. }
  30. return ret;
  31. }
  32. export async function delImage(image: string) {
  33. const ret: any = await axios.delete('/api' + image);
  34. if (ret.error) {
  35. return false;
  36. }
  37. return true;
  38. }
  39. export async function addImage(image: string) {
  40. const ret: any = await axios.post('/api/user/image', { image });
  41. if (ret.error) {
  42. return '';
  43. }
  44. return ret.id;
  45. }
  46. export function filename(str: string) {
  47. const i = str.lastIndexOf('.');
  48. return str.substring(0, i);
  49. }
  50. const units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  51. export function formatBytes(size: number) {
  52. let l = 0;
  53. let n = size / 1024;
  54. while (n >= 1024 && ++l) {
  55. n = n / 1024;
  56. }
  57. return Math.round(n * 100) / 100 + ' ' + units[l];
  58. }
  59. /**
  60. * 读取文件内容,返回字符串
  61. * @param file 文件
  62. */
  63. export async function readFile(file: Blob) {
  64. return new Promise<string>((resolve, reject) => {
  65. const reader = new FileReader();
  66. reader.onload = () => {
  67. resolve(reader.result as string);
  68. };
  69. reader.onerror = reject;
  70. // readAsText 使 result 一定是字符串
  71. reader.readAsText(file);
  72. });
  73. }
  74. export function dataURLtoBlob(base64: string) {
  75. let arr: any = base64.split(','),
  76. mime = arr[0].match(/:(.*?);/)[1],
  77. bstr = atob(arr[1]),
  78. n = bstr.length,
  79. u8arr = new Uint8Array(n);
  80. while (n--) {
  81. u8arr[n] = bstr.charCodeAt(n);
  82. }
  83. return new Blob([u8arr], { type: mime });
  84. }
  85. /**
  86. * 图片转 Blob
  87. * @param img 图片
  88. */
  89. export function saveToBlob(img: HTMLImageElement): Blob {
  90. const canvas: HTMLCanvasElement = document.createElement('canvas');
  91. canvas.setAttribute('origin-clean', 'false');
  92. canvas.width = img.width;
  93. canvas.height = img.height;
  94. const context: any = canvas.getContext('2d');
  95. context.filter = window.getComputedStyle(img).filter;
  96. context.drawImage(img, 0, 0, canvas.width, canvas.height);
  97. return dataURLtoBlob(canvas.toDataURL());
  98. }
  99. export async function importCSV() {
  100. return new Promise((resolve, reject) => {
  101. const input = document.createElement('input');
  102. input.type = 'file';
  103. input.accept =
  104. '.csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel,application/zip';
  105. input.onchange = (event) => {
  106. const elem: any = event.target;
  107. if (elem.files && elem.files[0].name.indexOf('.csv') > 0) {
  108. //papaparse.js
  109. globalThis.Papa.parse(elem.files[0], {
  110. complete: function(results) {
  111. resolve(results.data); // 输出二维数组
  112. }
  113. });
  114. MessagePlugin.success($t('导入成功!'));
  115. }
  116. };
  117. input.click();
  118. });
  119. }