Explorar el Código

fix: 打包base修改,验证过期重试

liutian hace 1 año
padre
commit
f65a1e8c9d

+ 8 - 2
packages/core/rollup.config.js

@@ -6,7 +6,7 @@ import json from '@rollup/plugin-json';
 
 export default {
   input: "./src/index.js",
-  // input: "./math.js",
+  // input: "./webviewer.js",
   output: [
     {
       format: "es",
@@ -14,6 +14,12 @@ export default {
       file: "../webview/lib/webview.min.js",
       sourcemap: false
     }
+    // {
+    //   format: "es",
+    //   name: 'ComPDFKitViewer',
+    //   file: "./dist/webviewer.js",
+    //   sourcemap: false
+    // }
   ],
   plugins: [
     json(),
@@ -22,7 +28,7 @@ export default {
       mainFields: ['browser']
     }),
     commonjs(),
-    // (uglify()),
+    (uglify()),
     // (process.env === 'production' && uglify()),
     babel({
       exclude: 'node_modules/**', // 防止打包node_modules下的文件

+ 83 - 32
packages/core/src/index.js

@@ -115,6 +115,7 @@ class ComPDFKitViewer {
           const verified = this._decrypt(resp.data)
           return verified
         } else {
+          alert(resp.message)
           return false
         }
       })
@@ -166,8 +167,20 @@ class ComPDFKitViewer {
     this.eventBus.dispatch("toolChanged", { tool, color });
   }
 
+  addEventListener (eventName, listener) {
+    if (this.eventBus) {
+      this.eventBus.on(eventName, listener)
+    }
+  }
+
+  removeEventListener (eventName, listener) {
+    if (this.eventBus) {
+      this.eventBus.of(eventName, listener)
+    }
+  }
+
   initAnnotations (annotations) {
-    console.log(annotations)
+    // console.log(annotations)
     for (const type in annotations) {
       const annotation = annotations[type]
       if (Array.isArray(annotation)) {
@@ -181,7 +194,7 @@ class ComPDFKitViewer {
         this.handleAnnotations(annotation, true)
       }
     }
-    console.log(this.annotations)
+    // console.log(this.annotations)
     this.eventBus.dispatch('annotationChanged', { annotations: this.annotations })
   }
 
@@ -306,24 +319,27 @@ class ComPDFKitViewer {
             },
             body: formData
           }
-          await fetch(this.optionUrl.webviewBaseUrl + this.optionUrl.uploadUrl, data)
-            .then((res) => {
-              return res.json()
-            })
-            .then((resp) => {
-              if (resp.code === "200") {
-                const data = resp.data
-                this._pdfId = data.pdfId
-                const annotations = data.annotateJson && JSON.parse(data.annotateJson)
-                this.initAnnotations(annotations)
-              } else if (resp.code === "319") {
-                
-              }
-            })
-            .catch((error) => {
-              console.log(error)
-              return { error: true, message: error.message }
-            });
+          const uploadFile = async () => {
+            await fetch(this.optionUrl.webviewBaseUrl + this.optionUrl.uploadUrl, data)
+              .then((res) => {
+                return res.json()
+              })
+              .then((resp) => {
+                if (resp.code === "200") {
+                  const data = resp.data
+                  this._pdfId = data.pdfId
+                  const annotations = data.annotateJson && JSON.parse(data.annotateJson)
+                  this.initAnnotations(annotations)
+                } else {
+                  this.handleExpiredToken(resp.code, uploadFile)
+                }
+              })
+              .catch((error) => {
+                console.log(error)
+                return { error: true, message: error.message }
+              });
+          }
+          await uploadFile()
         })
         this.load(pdfDocument);
       },
@@ -715,18 +731,53 @@ class ComPDFKitViewer {
         annotateHandles
       }),
     }
-    fetch(this.optionUrl.webviewBaseUrl + this.optionUrl.editUrl, options)
-      .then((res) => {
-        return res.json()
-      })
-      .then((data) => {
-        if (data.code === "200") {
-          return true
-        } else {
-          return false
-        }
-      })
-      .catch((error) => (console.log(error)));
+    const editPDF = () => {
+      fetch(this.optionUrl.webviewBaseUrl + this.optionUrl.editUrl, options)
+        .then((res) => {
+          return res.json()
+        })
+        .then((data) => {
+          if (data.code === "200") {
+            return true
+          } else {
+            this.handleExpiredToken(data.code, editPDF)
+            return false
+          }
+        })
+        .catch((error) => (console.log(error)));
+    }
+    editPDF()
+  }
+
+  handleExpiredToken (code, callback) {
+    if (code === '319') {
+      const data = {
+        method: 'POST',
+        headers: {
+          'Content-Type': 'application/json'
+        },
+        body: JSON.stringify({
+          license: this._license
+        })
+      }
+      fetch(this.optionUrl.baseUrl + this.optionUrl.verifyUrl, data)
+        .then((res) => {
+          return res.json()
+        })
+        .then((resp) => {
+          if (resp.code === 200) {
+            const verified = this._decrypt(resp.data)
+            callback()
+            return verified
+          } else {
+            alert(resp.message)
+            return false
+          }
+        })
+        .catch((error) => ({ error: true, message: error.message }));
+    } else {
+      alert('Invalid license')
+    }
   }
 
   webViewerSearch (value) {

+ 4 - 1
packages/core/webviewer.js

@@ -19,8 +19,10 @@ const ComPDFKitViewer = {
 class Viewer {
   constructor (options, element, optionsUrl) {
     this.instance = null
-    this.initialDoc = options.initialDoc || null
+    this.options = options
+    this.initialDoc = options.pdfUrl || null
     this.element = element
+    this.optionsUrl = optionsUrl
     element.addEventListener("ready", (function ready() {
       element.removeEventListener("ready", ready)
     }))
@@ -83,6 +85,7 @@ class Viewer {
     this.loadListener = function() {
       var $iframe = self.iframe;
       self.instance = $iframe.contentWindow.instance;
+      self.instance.initOptions(self.options)
       self._trigger("ready");
     },
     iframe.addEventListener("load", this.loadListener),

+ 7 - 8
packages/webview/src/components/DocumentContainer/DocumentContainer.vue

@@ -153,13 +153,11 @@
     useViewer.setCurrentScale(scale)
   }
 
-  onMounted(async () => {
-    const res = await initConfig({
-      license: '3AxOJBuKTqXhp+I9om9P+fvB2DMYXQbYnwfM7uExRRo='
-      // license: 'oWXBw+tacVrMjTGxBuRlrn+BPEU/ndvg2aemjPNOlNM='
-      // license: 'pB3xWyaCvnrPR/fDkBPjh+E1LeA0e+bEj6Z7a5VI1tQ='
-      // license: 'e+L5dBrcDnQJXP98kF7oHEo11SLrWIWse5oWqj5ykMU='
-    })
+  window.instance.initOptions = async (options) => {
+    const res = await initConfig(options)
+    // const res = await initConfig({
+    //   license: '3AxOJBuKTqXhp+I9om9P+fvB2DMYXQbYnwfM7uExRRo='
+    // })
     if (!res) return
     const thumbnailView = document.querySelector('.thumbnail-view')
     const annotationView = document.querySelector('.annotation-view')
@@ -185,7 +183,8 @@
       await handlePdf(initialDoc)
       loading.value = false
     }
-  })
+  }
+  // window.instance.initOptions()
 </script>
 
 <style lang="scss">

+ 1 - 3
packages/webview/vite.config.js

@@ -7,9 +7,7 @@ import { svgBuilder } from 'vite-svg-plugin'
 
 // https://vitejs.dev/config/
 export default defineConfig({
-  build: {
-    sourcemap: true,
-  },
+  base: './',
   plugins: [
     vue(),
     Components(),