utils.ts 953 B

123456789101112131415161718192021222324252627282930313233
  1. import { uploadFileApi, UPLOAD_BIZ_TYPE } from '@/api/minio';
  2. import type { ImageItem } from './types';
  3. export function stringToArray(str?: string): number[] | undefined {
  4. if (!str) return undefined;
  5. return JSON.parse('[' + str + ']');
  6. }
  7. export function unformatImage(file?: string) {
  8. if (!file) return undefined;
  9. const fileData: string[] = JSON.parse(file);
  10. return fileData;
  11. }
  12. const formatImage = async (data: ImageItem) => {
  13. if (!data.file) return data;
  14. const name = data.file.name;
  15. const res = await uploadFileApi({ bizType: UPLOAD_BIZ_TYPE.ATTACHMENT, fileName: name, file: data.file });
  16. return res.url;
  17. };
  18. export const formatImageList = async (data: Array<ImageItem> | undefined) => {
  19. if (!data || data.length === 0) return null;
  20. const res = await Promise.all(
  21. data.map(async (item) => {
  22. if (!item.file) return item.url;
  23. const res = await formatImage(item);
  24. return res;
  25. }),
  26. );
  27. return res;
  28. };