crm-attachment.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * CRM 附件解析与下载 — 门户 MinIO(fileUrl 即存储键/URL,不再走 CRM /resource/oss/listByIds)
  3. */
  4. import {
  5. downloadAttachmentFile,
  6. openAttachmentPreviewInNewTab
  7. } from '@/utils/download-attachment'
  8. export type CrmAttachmentRef = {
  9. ossId: string
  10. category?: string
  11. }
  12. export type CrmAttachmentFile = {
  13. ossId: string
  14. name: string
  15. url: string
  16. category?: string
  17. }
  18. /** 解析 attachmentFiles 字段(JSON 数组或逗号分隔 URL) */
  19. export function parseCrmAttachmentField(
  20. fileStr: string | null | undefined
  21. ): CrmAttachmentRef[] {
  22. const raw = (fileStr || '').trim()
  23. if (!raw) return []
  24. if (raw.startsWith('[')) {
  25. try {
  26. const parsed = JSON.parse(raw)
  27. if (Array.isArray(parsed)) {
  28. return parsed.map((item: unknown) => {
  29. if (typeof item === 'string') {
  30. return { ossId: item, category: 'other' }
  31. }
  32. const obj = item as Record<string, unknown>
  33. return {
  34. ossId: String(obj.ossId || obj.url || ''),
  35. category: (obj.category as string) || 'other'
  36. }
  37. }).filter((a) => a.ossId)
  38. }
  39. } catch {
  40. // fall through
  41. }
  42. }
  43. return raw
  44. .split(',')
  45. .map((s) => s.trim())
  46. .filter(Boolean)
  47. .map((ossId) => ({ ossId, category: 'other' }))
  48. }
  49. /** 从 ossId/url 推导展示文件名(完整 URL 与相对路径均取最后一段) */
  50. export function crmAttachmentDisplayName(ossId: string, index = 0): string {
  51. const id = (ossId || '').trim()
  52. if (!id) return `附件${index + 1}`
  53. // 含路径分隔符时取末段(兼容 http(s) URL 与 MinIO 相对键如 crm/contract/.../uuid.pdf)
  54. if (id.includes('/')) {
  55. const seg = (id.split('/').pop() || '').split('?')[0]
  56. try {
  57. return decodeURIComponent(seg) || `附件${index + 1}`
  58. } catch {
  59. return seg || `附件${index + 1}`
  60. }
  61. }
  62. if (id.length > 12) return `附件_${id.substring(0, 8)}`
  63. return id
  64. }
  65. /** 将解析结果转为 UI 文件项(门户直接存 MinIO URL,无需 listByIds) */
  66. export function resolveCrmAttachmentFiles(
  67. refs: CrmAttachmentRef[]
  68. ): CrmAttachmentFile[] {
  69. return refs.map((att, index) => {
  70. const ossId = att.ossId
  71. return {
  72. ossId,
  73. name: crmAttachmentDisplayName(ossId, index),
  74. url: ossId,
  75. category: att.category || 'other'
  76. }
  77. })
  78. }
  79. /** 兼容旧 loadAttachmentInfos:批量解析并写入 cache Map */
  80. export function fillAttachmentInfoCache(
  81. ossIds: string[],
  82. cache: Map<string, { name: string; url: string }>
  83. ): void {
  84. ossIds.forEach((id, index) => {
  85. const key = (id || '').trim()
  86. if (!key || cache.has(key)) return
  87. cache.set(key, {
  88. name: crmAttachmentDisplayName(key, index),
  89. url: key
  90. })
  91. })
  92. }
  93. export type ContractAttachmentItem = {
  94. uid: string
  95. name: string
  96. ossId?: string
  97. url?: string
  98. localPreview?: string
  99. status: 'uploading' | 'success' | 'error'
  100. category: 'single_seal' | 'double_seal' | 'other'
  101. }
  102. /** 合同附件列表 → 保存 API 用的 JSON 字符串 */
  103. export function serializeContractAttachments(items: ContractAttachmentItem[]): string {
  104. const payload = items
  105. .filter((file) => file.status === 'success' && file.ossId)
  106. .map((file) => ({
  107. ossId: String(file.ossId),
  108. category: file.category || 'other'
  109. }))
  110. return payload.length ? JSON.stringify(payload) : ''
  111. }
  112. /** 合同附件列表 → uploadAttachments 接口 payload */
  113. export function mapContractAttachmentsPayload(items: ContractAttachmentItem[]) {
  114. return items
  115. .filter((file) => file.status === 'success' && file.ossId)
  116. .map((file) => ({
  117. ossId: String(file.ossId),
  118. category: file.category || 'other'
  119. }))
  120. }
  121. /** MinIO 相对路径 → 浏览器可访问 URL */
  122. export function resolveCrmAttachmentUrl(fileUrl: string): string {
  123. const trimmed = String(fileUrl || '').trim()
  124. if (!trimmed) return ''
  125. if (/^https?:\/\//i.test(trimmed)) return trimmed
  126. const base = String(import.meta.env.VITE_MINIO_BASE_URL || '').replace(/\/$/, '')
  127. return base ? `${base}/${trimmed.replace(/^\//, '')}` : trimmed
  128. }
  129. export function isCrmImageFile(file: { name?: string; url?: string; ossId?: string }): boolean {
  130. const name = file.name || ''
  131. if (/\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(name)) return true
  132. const src = file.url || file.ossId || ''
  133. return /\.(jpg|jpeg|png|gif|bmp|webp)(\?|$)/i.test(src)
  134. }
  135. export function isCrmPdfFile(file: { name?: string; url?: string; ossId?: string }): boolean {
  136. const name = file.name || ''
  137. if (/\.pdf$/i.test(name)) return true
  138. const src = file.url || file.ossId || ''
  139. return /\.pdf(\?|$)/i.test(src)
  140. }
  141. export function getCrmFileExt(fileName: string): string {
  142. const ext = fileName.split('.').pop()?.toUpperCase()
  143. return ext || 'FILE'
  144. }
  145. const CRM_ATTACHMENT_CATEGORY_LABELS: Record<string, string> = {
  146. single_seal: '单章附件',
  147. double_seal: '双章附件',
  148. other: '其他附件'
  149. }
  150. /** 附件分类 → 展示文案 */
  151. export function getCrmAttachmentCategoryLabel(category?: string): string {
  152. return CRM_ATTACHMENT_CATEGORY_LABELS[category || 'other'] || CRM_ATTACHMENT_CATEGORY_LABELS.other
  153. }
  154. /** 下载 CRM 附件(fileUrl / ossId 均为 MinIO 路径或完整 URL) */
  155. export async function downloadCrmAttachment(file: {
  156. ossId?: string
  157. url?: string
  158. name?: string
  159. fileName?: string
  160. }) {
  161. const fileUrl = String(file.url || file.ossId || '').trim()
  162. if (!fileUrl) return
  163. const fileName = String(file.fileName || file.name || '').trim() || crmAttachmentDisplayName(fileUrl)
  164. await downloadAttachmentFile({ fileUrl, fileName }, { fullFileUrl: resolveCrmAttachmentUrl })
  165. }
  166. /** CRM 附件在新标签页预览(图片 HTML 内嵌 / PDF 预签名直开) */
  167. export async function openCrmAttachmentPreviewInNewTab(file: {
  168. ossId?: string
  169. url?: string
  170. name?: string
  171. fileName?: string
  172. }) {
  173. const fileUrl = String(file.url || file.ossId || '').trim()
  174. if (!fileUrl) return false
  175. const fileName =
  176. String(file.fileName || file.name || '').trim() || crmAttachmentDisplayName(fileUrl)
  177. const kind = isCrmImageFile({ name: fileName, url: fileUrl, ossId: fileUrl })
  178. ? 'image'
  179. : isCrmPdfFile({ name: fileName, url: fileUrl, ossId: fileUrl })
  180. ? 'pdf'
  181. : 'other'
  182. return openAttachmentPreviewInNewTab(
  183. { fileUrl, fileName },
  184. { fullFileUrl: resolveCrmAttachmentUrl, kind }
  185. )
  186. }
  187. /** el-upload 回显:从 attachmentFiles 字符串构建 file-list */
  188. export function buildUploadFileListFromField(
  189. fileStr: string | null | undefined
  190. ): Array<Record<string, unknown>> {
  191. const refs = parseCrmAttachmentField(fileStr)
  192. return refs.map((att, index) => ({
  193. uid: `existing_${index}_${Date.now()}`,
  194. name: crmAttachmentDisplayName(att.ossId, index),
  195. url: att.ossId,
  196. ossId: att.ossId,
  197. category: att.category || 'other',
  198. status: 'success'
  199. }))
  200. }
  201. /** 从 el-upload file-list 收集 attachmentFiles JSON(供保存 API) */
  202. export function serializeUploadAttachments(
  203. files: Array<{ status?: string; ossId?: string; url?: string; category?: string }>
  204. ): string {
  205. const items = files
  206. .filter((f) => f.status === 'success' && (f.ossId || f.url))
  207. .map((f) => ({
  208. ossId: String(f.ossId || f.url),
  209. category: f.category || 'other'
  210. }))
  211. return items.length ? JSON.stringify(items) : ''
  212. }