ソースを参照

feat: 报送资料查看下载完成

shiyanyu 1 ヶ月 前
コミット
fa4f1e878b

+ 26 - 3
src/components/task/components/submitData.vue

@@ -318,10 +318,33 @@
             this.$message({ type: 'info', message: '已取消操作' })
           })
       },
-      // 查看下载文件
+      // 查看下载文件(与 taskInfo.vue 一致的 kkFileView 预览)
       handleViewDownload(row) {
-        this.$message.info(`查看下载文件:${row.name}`)
-        // 这里可以添加查看下载文件的逻辑
+        console.log(row, '这一行')
+        try {
+          const filePath =
+            row?.filePath ||
+            row?.filepath ||
+            row?.fileUrl ||
+            row?.url ||
+            row?.path ||
+            ''
+          if (!filePath) {
+            this.$message &&
+              this.$message.warning &&
+              this.$message.warning('未找到可预览的文件路径')
+            return
+          }
+          const encodedUrl = encodeURIComponent(
+            Base64.encode((window.context && window.context.form) + filePath)
+          )
+          window.open(`${host}:8012/onlinePreview?url=${encodedUrl}`)
+        } catch (e) {
+          console.error('文件预览失败: ', e)
+          this.$message &&
+            this.$message.error &&
+            this.$message.error('文件预览失败')
+        }
       },
       // 查看报表
       handleViewReport(row) {

+ 109 - 2
src/components/task/taskInfo.vue

@@ -439,7 +439,7 @@
                         "
                         type="text"
                         size="small"
-                        @click="$emit('handleFileView', scope.row)"
+                        @click="handleFileView(scope.row)"
                       >
                         查看
                       </el-button>
@@ -449,7 +449,7 @@
                         "
                         type="text"
                         size="small"
-                        @click="$emit('handleFileDownload', scope.row)"
+                        @click="handleFileDownload(scope.row)"
                       >
                         下载
                       </el-button>
@@ -919,6 +919,113 @@
       this.initData()
     },
     methods: {
+      // 报送资料-查看(同 taskFillIn)
+      handleFileView(row) {
+        console.log(row, '这一行数据')
+        try {
+          const filePath =
+            row?.filePath ||
+            row?.filepath ||
+            row?.fileUrl ||
+            row?.url ||
+            row?.path ||
+            ''
+          if (!filePath) {
+            this.$message &&
+              this.$message.warning &&
+              this.$message.warning('未找到可预览的文件路径')
+            return
+          }
+          const encodedUrl = encodeURIComponent(
+            Base64.encode((window.context && window.context.form) + filePath)
+          )
+          window.open(`${host}:8012/onlinePreview?url=${encodedUrl}`)
+          // 兼容保留:通知父组件
+          this.$emit('handleFileView', row)
+        } catch (e) {
+          console.error('文件预览失败: ', e)
+          this.$message &&
+            this.$message.error &&
+            this.$message.error('文件预览失败')
+        }
+      },
+      // 报送资料-下载(同 taskFillIn)
+      handleFileDownload(row) {
+        if (!row || !row.fileUrl) {
+          this.$message &&
+            this.$message.warning &&
+            this.$message.warning('该资料暂无文件可下载')
+          return
+        }
+        this.downloadByFetch(row.fileUrl, row.informationName || '下载文件')
+      },
+      async downloadByFetch(rawUrl, fallbackName) {
+        const url = this.normalizeUrl(rawUrl)
+        let loading
+        try {
+          loading = this.$baseLoading
+            ? this.$baseLoading(1, '文件下载中...')
+            : this.$loading({
+                lock: true,
+                text: '文件下载中...',
+                spinner: 'el-icon-loading',
+                background: 'rgba(0,0,0,0.7)',
+              })
+          const res = await fetch(url, { method: 'GET' })
+          if (!res.ok) throw new Error('下载失败')
+          const blob = await res.blob()
+          let fileName =
+            this.extractFileName(res.headers.get('content-disposition')) ||
+            fallbackName ||
+            '下载文件'
+          if (!/\.[a-zA-Z0-9]+$/.test(fileName)) {
+            const extFromUrl = (
+              url.split('?')[0].split('#')[0].split('.').pop() || ''
+            ).toLowerCase()
+            fileName = extFromUrl ? `${fileName}.${extFromUrl}` : fileName
+          }
+          const objectUrl = window.URL.createObjectURL(blob)
+          const link = document.createElement('a')
+          link.style.display = 'none'
+          link.href = objectUrl
+          link.download = fileName
+          document.body.appendChild(link)
+          link.click()
+          document.body.removeChild(link)
+          window.URL.revokeObjectURL(objectUrl)
+          this.$message &&
+            this.$message.success &&
+            this.$message.success('开始下载文件')
+        } catch (e) {
+          this.$message &&
+            this.$message.error &&
+            this.$message.error(e.message || '文件下载失败')
+        } finally {
+          if (loading && loading.close) loading.close()
+        }
+      },
+      normalizeUrl(u) {
+        if (!u) return ''
+        if (/^https?:\/\//i.test(u)) return u
+        const base = (window.context && window.context.form) || ''
+        if (!base) return u
+        if (u.startsWith('/')) return base + u
+        return base.replace(/\/$/, '') + '/' + u
+      },
+      extractFileName(contentDisposition) {
+        if (!contentDisposition) return ''
+        const match = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/i.exec(
+          contentDisposition
+        )
+        if (match && match[1]) {
+          try {
+            return decodeURIComponent(match[1].replace(/['"]/g, ''))
+          } catch (e) {
+            return match[1].replace(/['"]/g, '')
+          }
+        }
+        return ''
+      },
       // 初始化数据
       initData() {
         this.getAllUnitList()

+ 65 - 24
src/views/EntDeclaration/auditTaskManagement/taskFillIn.vue

@@ -1479,18 +1479,7 @@
           this.$message.warning('该文书暂无文件可下载')
           return
         }
-
-        // 创建隐藏的a标签进行下载
-        const link = document.createElement('a')
-        link.style.display = 'none'
-        link.href = row.fileUrl
-        // 设置下载文件名
-        link.download = row.name || '文书文件'
-        document.body.appendChild(link)
-        link.click()
-        document.body.removeChild(link)
-
-        this.$message.success('开始下载文件')
+        this.downloadByFetch(row.fileUrl, row.name || '文书文件')
       },
       // 上传附件
       handleUpload(row) {
@@ -1513,18 +1502,70 @@
           this.$message.warning('该资料暂无文件可下载')
           return
         }
-
-        // 创建隐藏的a标签进行下载
-        const link = document.createElement('a')
-        link.style.display = 'none'
-        link.href = row.fileUrl
-        // 设置下载文件名,优先使用资料名称
-        link.download = row.informationName || '下载文件'
-        document.body.appendChild(link)
-        link.click()
-        document.body.removeChild(link)
-
-        this.$message.success('开始下载文件')
+        this.downloadByFetch(row.fileUrl, row.informationName || '下载文件')
+      },
+      async downloadByFetch(rawUrl, fallbackName) {
+        const url = this.normalizeUrl(rawUrl)
+        let loading
+        try {
+          loading = this.$baseLoading
+            ? this.$baseLoading(1, '文件下载中...')
+            : this.$loading({
+                lock: true,
+                text: '文件下载中...',
+                spinner: 'el-icon-loading',
+                background: 'rgba(0,0,0,0.7)',
+              })
+          const res = await fetch(url, { method: 'GET' })
+          if (!res.ok) throw new Error('下载失败')
+          const blob = await res.blob()
+          let fileName =
+            this.extractFileName(res.headers.get('content-disposition')) ||
+            fallbackName ||
+            '下载文件'
+          if (!/\.[a-zA-Z0-9]+$/.test(fileName)) {
+            const extFromUrl = (
+              url.split('?')[0].split('#')[0].split('.').pop() || ''
+            ).toLowerCase()
+            fileName = extFromUrl ? `${fileName}.${extFromUrl}` : fileName
+          }
+          const objectUrl = window.URL.createObjectURL(blob)
+          const link = document.createElement('a')
+          link.style.display = 'none'
+          link.href = objectUrl
+          link.download = fileName
+          document.body.appendChild(link)
+          link.click()
+          document.body.removeChild(link)
+          window.URL.revokeObjectURL(objectUrl)
+          this.$message.success('开始下载文件')
+        } catch (e) {
+          this.$message.error(e.message || '文件下载失败')
+        } finally {
+          if (loading && loading.close) loading.close()
+        }
+      },
+      normalizeUrl(u) {
+        if (!u) return ''
+        if (/^https?:\/\//i.test(u)) return u
+        const base = (window.context && window.context.form) || ''
+        if (!base) return u
+        if (u.startsWith('/')) return base + u
+        return base.replace(/\/$/, '') + '/' + u
+      },
+      extractFileName(contentDisposition) {
+        if (!contentDisposition) return ''
+        const match = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/i.exec(
+          contentDisposition
+        )
+        if (match && match[1]) {
+          try {
+            return decodeURIComponent(match[1].replace(/['"]/g, ''))
+          } catch (e) {
+            return match[1].replace(/['"]/g, '')
+          }
+        }
+        return ''
       },
       // 报送资料文件上传
       async handleFileUpload(row, acceptFromChild) {