From cd17dd9d5f1d6b3d72a91889036f64b3cf32ca3a Mon Sep 17 00:00:00 2001 From: quu Date: Sun, 4 May 2025 15:34:00 +0900 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=90=8C=E6=89=93=E5=8D=B0=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/crm/contract/index.ts | 7 +++++- src/views/crm/contract/index.vue | 43 +++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/api/crm/contract/index.ts b/src/api/crm/contract/index.ts index 72d02aa16..3f500505c 100644 --- a/src/api/crm/contract/index.ts +++ b/src/api/crm/contract/index.ts @@ -147,9 +147,14 @@ export const getOrgInfo = async (params) => { // 打印合同 export const printContract = async (id: number) => { - return await request.put({ url: `/crm/contract/print?id=${id}` }) + return await request.get({ + url: `/crm/contract/print`, + params: { contractId: id }, + responseType: 'blob' // 必须加这个才能下载文件 + }) } + // ==================== 子表(CRM 合同产品关联) ==================== // 获得CRM 合同产品关联列表 diff --git a/src/views/crm/contract/index.vue b/src/views/crm/contract/index.vue index 0d79790bd..cea1bd788 100644 --- a/src/views/crm/contract/index.vue +++ b/src/views/crm/contract/index.vue @@ -446,10 +446,45 @@ const handleSubmit = async (row: ContractApi.ContractVO) => { } const onPrintContract = async (row: ContractApi.ContractVO) => { - await message.confirm(`您确定打印【${row.name}】合同吗?`) - await ContractApi.printContract(row.id) - message.success('提交审核成功!') - await getList() + try { + await message.confirm(`您确定打印【${row.name}】合同吗?`) + const res = await ContractApi.printContract(row.id) + + if (!res) { + throw new Error('返回的合同文件为空') + } + + const contentType = res.type.toLowerCase() + let fileType = 'application/msword' // 默认使用 .doc 的 MIME 类型 + let extension = '.doc' + + if (contentType.includes('wordprocessingml')) { + fileType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' // 对应 .docx 文件的 MIME 类型 + extension = '.docx' + } + // 创建 blob 对象并设置文件类型 + const blob = new Blob([res], { type: fileType }) + + const url = window.URL.createObjectURL(blob) + const link = document.createElement('a') + link.href = url + const disposition = res.headers['content-disposition'] + let fileName = '合同.docx' + if (disposition && disposition.includes('filename=')) { + const match = disposition.match(/filename="?([^"]+)"?/) + if (match?.[1]) { + fileName = decodeURIComponent(match[1]) + } + } + link.click() + window.URL.revokeObjectURL(url) + + message.success('提交审核成功!') + await getList() + } catch (error) { + console.error('合同下载失败', error) + } + } /** 查看审批 */