bxInvoiceThumb.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. export function isImageFileType(fileType) {
  2. return !!(fileType && String(fileType).startsWith('image/'))
  3. }
  4. export function fileExtLabel(name) {
  5. const ext = String(name || '').split('.').pop()
  6. return ext ? ext.toUpperCase() : 'FILE'
  7. }
  8. /** 展示用 YYYY-MM-DD;无有效日期返回 — */
  9. export function formatInvoiceDateYmd(value) {
  10. if (value == null || value === '') return '—'
  11. const s = String(value).trim()
  12. const m = s.match(/^(\d{4}-\d{2}-\d{2})/)
  13. if (m) return m[1]
  14. const d = new Date(s)
  15. if (Number.isNaN(d.getTime())) return '—'
  16. const y = d.getFullYear()
  17. const mo = String(d.getMonth() + 1).padStart(2, '0')
  18. const day = String(d.getDate()).padStart(2, '0')
  19. return `${y}-${mo}-${day}`
  20. }
  21. function invoiceFileList(inv) {
  22. const raw = Array.isArray(inv?.attachments) ? inv.attachments : []
  23. if (raw.length) {
  24. return raw.map((a) => ({
  25. id: String(a.id || a.attachmentId || ''),
  26. fileName: a.fileName || a.file_name || '发票附件',
  27. fileType: a.fileType || a.file_type || ''
  28. }))
  29. }
  30. const aid = inv?.attachmentId || inv?.attachment_id
  31. if (aid) {
  32. return [
  33. {
  34. id: String(aid),
  35. fileName: inv.fileName || inv.file_name || '发票附件',
  36. fileType: inv.fileType || inv.file_type || ''
  37. }
  38. ]
  39. }
  40. return []
  41. }
  42. /**
  43. * 发票列表行 → 附件粒度选项
  44. * @returns {{ key, invoiceId, attachmentId, amount, createTime, fileName, fileType, previewUrl, previewLoading }[]}
  45. */
  46. export function buildAttachItemsFromInvoiceRows(rows) {
  47. const items = []
  48. for (const row of rows || []) {
  49. const createTime = row.createTime || row.create_time || ''
  50. for (const file of invoiceFileList(row)) {
  51. if (!file.id) continue
  52. items.push({
  53. key: `${row.id}:${file.id}`,
  54. invoiceId: String(row.id),
  55. attachmentId: file.id,
  56. amount: row.amount,
  57. createTime,
  58. fileName: file.fileName,
  59. fileType: file.fileType,
  60. previewUrl: '',
  61. previewLoading: false
  62. })
  63. }
  64. }
  65. return items
  66. }
  67. /**
  68. * 确定勾选:已加载项优先;draft 中仍勾选但不在已加载列表的旧已选项保留(防分页冲掉)
  69. */
  70. export function mergeInvoicePicksByDraftKeys(draftKeys, attachItems, previousPicks) {
  71. const set = new Set(draftKeys || [])
  72. const fromLoaded = (attachItems || []).filter((i) => set.has(i.key))
  73. const loadedKeys = new Set(fromLoaded.map((i) => i.key))
  74. const fromPrev = (previousPicks || []).filter(
  75. (p) => set.has(p.key) && !loadedKeys.has(p.key)
  76. )
  77. const byKey = new Map()
  78. for (const p of fromPrev) byKey.set(p.key, { ...p })
  79. for (const i of fromLoaded) {
  80. const prev = byKey.get(i.key)
  81. byKey.set(i.key, {
  82. ...i,
  83. previewUrl: i.previewUrl || prev?.previewUrl || ''
  84. })
  85. }
  86. return Array.from(set)
  87. .map((k) => byKey.get(k))
  88. .filter(Boolean)
  89. }