From 94e0abbf193f9e1fc953716ca7868d74cd14a2cd Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期一, 21 九月 2026 11:06:25 +0800
Subject: [PATCH] fix(inspectionTask): 修复连接实验室功能并更新字段标签

---
 src/components/Onlyoffice/onlyoffice.vue |   96 +++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 87 insertions(+), 9 deletions(-)

diff --git a/src/components/Onlyoffice/onlyoffice.vue b/src/components/Onlyoffice/onlyoffice.vue
index 1371068..cfafc9f 100644
--- a/src/components/Onlyoffice/onlyoffice.vue
+++ b/src/components/Onlyoffice/onlyoffice.vue
@@ -1,9 +1,49 @@
 <!--onlyoffice 缂栬緫鍣�-->
 <template>
-  <div id="vabOnlyOffice"></div>
+  <div class="document-preview">
+    <iframe
+      v-if="isPdf"
+      :key="option.url"
+      :src="option.url"
+      class="native-pdf-preview"
+      title="PDF棰勮"
+    ></iframe>
+    <div v-else id="vabOnlyOffice"></div>
+  </div>
 </template>
 
 <script>
+// OnlyOffice 鏂囨。鏈嶅姟鍦板潃锛屾寜鐜閰嶇疆锛涜剼鏈湪鐪熸闇�瑕侀瑙堟椂鎵嶅姞杞�
+const OFFICE_API_BASE = process.env.VUE_APP_ONLYOFFICE_API || "http://192.168.21.53:9001";
+
+// 澶氫釜椤甸潰鍏辩敤缂栬緫鍣紝api.js 鍙姞杞戒竴娆�
+let docsApiPromise = null;
+function loadDocsApi() {
+  if (window.DocsAPI) {
+    return Promise.resolve(window.DocsAPI);
+  }
+  if (!docsApiPromise) {
+    docsApiPromise = new Promise((resolve, reject) => {
+      const script = document.createElement("script");
+      script.src = OFFICE_API_BASE.replace(/\/$/, "") + "/web-apps/apps/api/documents/api.js";
+      script.onload = () => {
+        if (window.DocsAPI) {
+          resolve(window.DocsAPI);
+        } else {
+          reject(new Error("api.js 鍔犺浇瀹屾垚浣嗘湭娉ㄥ唽 DocsAPI"));
+        }
+      };
+      script.onerror = () => reject(new Error("OnlyOffice 鏂囨。鏈嶅姟鑴氭湰鍔犺浇澶辫触锛�" + script.src));
+      document.head.appendChild(script);
+    });
+    // 鍔犺浇澶辫触鍚庡厑璁镐笅娆¢噸璇�
+    docsApiPromise.catch(() => {
+      docsApiPromise = null;
+    });
+  }
+  return docsApiPromise;
+}
+
 export default {
   name: "VabOnlyOffice",
   props: ['options'],
@@ -23,6 +63,11 @@
         editUrl: ""
       }
     };
+  },
+  computed: {
+    isPdf() {
+      return this.isPdfFile(this.option);
+    }
   },
   created() {
     if(this.options){
@@ -50,10 +95,7 @@
     }
   },
   beforeDestroy() {
-    if (this.docEditor !== null) {
-      this.docEditor.destroyEditor();
-      this.docEditor = null;
-    }
+    this.destroyEditor();
   },
   watch: {
     option: {
@@ -70,11 +112,20 @@
     }
   },
   methods: {
-    async setEditor(option) {
+    destroyEditor() {
       if (this.docEditor !== null) {
         this.docEditor.destroyEditor();
         this.docEditor = null;
       }
+    },
+    async setEditor(option) {
+      this.destroyEditor();
+      // PDF 浣跨敤娴忚鍣ㄥ師鐢熼瑙堬紱鍏朵粬鏂囨。鎵嶅垵濮嬪寲 OnlyOffice銆�
+      if (!option || !option.url || this.isPdfFile(option)) {
+        return;
+      }
+      // 绛夊緟 v-else 涓殑 OnlyOffice 瀹瑰櫒瀹屾垚娓叉煋銆�
+      await this.$nextTick();
       this.doctype = this.getFileType(option.fileType);
       let config = {
         document: {
@@ -116,8 +167,25 @@
         token: option.token || ""
       };
 
-      // eslint-disable-next-line no-undef,no-unused-vars
-      this.docEditor = new DocsAPI.DocEditor("vabOnlyOffice", config);
+      try {
+        await loadDocsApi();
+      } catch (error) {
+        console.error(error);
+        this.$message.error("鏂囨。鏈嶅姟鏆傛椂涓嶅彲鐢紝鏃犳硶棰勮");
+        return;
+      }
+      if (this._isDestroyed) {
+        return;
+      }
+      this.docEditor = new window.DocsAPI.DocEditor("vabOnlyOffice", config);
+    },
+    isPdfFile(option) {
+      if (!option) {
+        return false;
+      }
+      const fileType = String(option.fileType || "").toLowerCase();
+      const url = String(option.url || "").split(/[?#]/)[0].toLowerCase();
+      return fileType === "pdf" || url.endsWith(".pdf");
     },
     getFileType(fileType) {
       let docType = "";
@@ -182,7 +250,17 @@
 };
 </script>
 
-<style>
+<style scoped>
+.document-preview,
+.native-pdf-preview,
+#vabOnlyOffice {
+  width: 100%;
+  height: 100%;
+}
+.native-pdf-preview {
+  display: block;
+  border: 0;
+}
 html,
 body {
   height: 100%;

--
Gitblit v1.9.3