png.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import axios from '@/http';
  2. import { parseSvg } from '@meta2d/svg';
  3. import { cdn } from './api';
  4. import { filename } from './file';
  5. export async function getFolders(name: string, isSvg?: boolean) {
  6. const path = name;
  7. const folders: any = await axios.post('/api/assets/folders', {
  8. path,
  9. });
  10. if (!folders || !folders.list) {
  11. return [];
  12. }
  13. const files: any = await axios.post('/api/assets/files', {
  14. path,
  15. });
  16. const results = [];
  17. for (const item of folders.list) {
  18. let name = item;
  19. if (folders.prefix) {
  20. const temp = name.split('/');
  21. name = temp[temp.length - 2];
  22. }
  23. const list = [];
  24. if (files.list) {
  25. for (const f of files.list) {
  26. if (f.indexOf('/'+item+'/') >= 0) {
  27. const temp = f.split('/');
  28. const name = filename(temp[temp.length - 1]);
  29. if (!name) {
  30. continue;
  31. }
  32. const elem: any = {
  33. name,
  34. image: cdn + '/' + f,
  35. isSvg,
  36. };
  37. list.push(elem);
  38. }
  39. }
  40. }
  41. results.push({ name, list });
  42. }
  43. return results;
  44. }
  45. export async function makeSvg(elem: any) {
  46. const svgDom: string = await axios.get(elem.image);
  47. let _svgDom = svgDom.replaceAll('#333;', '#fff;');
  48. elem.svg = _svgDom;
  49. elem.data = parseSvg(_svgDom);
  50. const pens = elem.data;
  51. if(pens.length){
  52. if(pens[0].width>100||pens[0].height>100){
  53. if(pens[0].width>pens[0].height){
  54. pens[0].height = pens[0].height/pens[0].width*100;
  55. pens[0].width = 100;
  56. }else{
  57. pens[0].width = pens[0].width/pens[0].height*100;
  58. pens[0].height = 100;
  59. }
  60. }
  61. }
  62. let le5le = [];
  63. elem.data.forEach((pen) => {
  64. if (pen.name === 'svgPath') {
  65. if (
  66. pen.path.indexOf('-1.18Zm4-1') !== -1 ||
  67. pen.path.indexOf('-1.19Zm4-1') !== -1 ||
  68. pen.path.indexOf('2.85ZM') !== -1 ||
  69. pen.path.indexOf('-1-2.39.3') !== -1
  70. ) {
  71. le5le.push(pen);
  72. }
  73. }
  74. });
  75. if (le5le.length === 3) {
  76. le5le.forEach((pen) => {
  77. (pen as any).svgUrl = elem.image;
  78. });
  79. }
  80. }