attachment.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * 从审批 / 业务详情的原始接口数据中,兼容多种附件字段来源,
  3. * 解析出文件 ID 数组(去重、字符串化)。
  4. *
  5. * 兼容的字段来源(按常见命名):
  6. * - attachmentIds 字符串(逗号分隔)或字符串数组
  7. * - attachmentFileIds 字符串(逗号分隔)或字符串数组
  8. * - approvalDocumentFileId 单个 ID 或逗号分隔的字符串或数组
  9. * - attachmentFileId 同上
  10. * - resumeAttachmentFileIds 同上
  11. * - files / attachments 对象数组,元素含 fileId / id,或本身就是 ID 字符串
  12. *
  13. * 这样无论后端接口用哪种字段返回附件,前端都能正确拿到可预览的文件 ID 列表。
  14. */
  15. export function parseAttachmentIds(raw) {
  16. raw = raw || {};
  17. const ids = [];
  18. const collect = (value) => {
  19. if (value == null || value === '') return;
  20. const list = Array.isArray(value)
  21. ? value
  22. : String(value)
  23. .split(',')
  24. .map((s) => s.trim());
  25. list.forEach((item) => {
  26. if (item == null || item === '') return;
  27. const id =
  28. typeof item === 'object' ? item.fileId || item.id : String(item).trim();
  29. if (id && !ids.includes(String(id))) ids.push(String(id));
  30. });
  31. };
  32. collect(raw.attachmentIds);
  33. collect(raw.attachmentFileIds);
  34. collect(raw.approvalDocumentFileId);
  35. collect(raw.attachmentFileId);
  36. collect(raw.resumeAttachmentFileIds);
  37. if (Array.isArray(raw.files)) collect(raw.files);
  38. if (Array.isArray(raw.attachments)) collect(raw.attachments);
  39. return ids;
  40. }