Просмотр исходного кода

feat: 解密iframe逻辑优化

cuiyalong 1 год назад
Родитель
Сommit
2759fea3ab
2 измененных файлов с 39 добавлено и 20 удалено
  1. 0 0
      apps/mobile/public/decrypt-js.html
  2. 39 20
      packages/vue-anti/src/decrypt/decrypt-iframe.js

+ 0 - 0
apps/bigmember_pc/public/decrypt-js.html → apps/mobile/public/decrypt-js.html


+ 39 - 20
packages/vue-anti/src/decrypt/decrypt-iframe.js

@@ -5,7 +5,7 @@ class DecryptIframe {
 
   iframeOrigin = 'https://jybx2-webtest.jydev.jianyu360.com'
   // iframeOrigin = 'http://localhost'
-  iframePath = '/page_big_pc/decrypt-js.html'
+  iframePath = '/jy_mobile/decrypt-js.html'
   iframeEl = null // iframe的dom引用
   iframeName = '' // iframe的name
   iframeReady = false // 子页面是否初始化完成
@@ -29,6 +29,9 @@ class DecryptIframe {
   get iframeUrl() {
     return `${this.iframeOrigin}${this.iframePath}`
   }
+  get iframeUrlBackup() {
+    return this.iframePath
+  }
 
   createIframe() {
     const iframe = document.createElement('iframe')
@@ -98,39 +101,55 @@ class DecryptIframe {
   }
 
   initEvents() {
-    // load
+    this.initIframeEvents()
+    this.initMessageEvents()
+  }
+
+  initIframeEvents() {
+    // iframe相关事件
     const iframe = this.iframeEl
     iframe.addEventListener('load', () => {
       // iframe 中的 JavaScript 已经加载完成
       this.iframeReady = true
-      if (this._cacheWaitingReadyPromise) {
+      if (this._cacheWaitingReadyPromise.resolve) {
         this._cacheWaitingReadyPromise.resolve()
       }
       console.log('iframe JavaScript loaded!');
     })
-
-    const onMessage = e => {
-      const id = e.data.id
-      if (e.data.type !== 'after-decrypt') {
-        if (this._cacheMessagePromiseMap[id]) {
-          this._cacheMessagePromiseMap[id].reject()
-        }
-        return
+    iframe.addEventListener('error', (e) => {
+      console.log(e)
+      if (iframe.src === this.iframeUrl) {
+        iframe.src = this.iframePath
+      } else {
+        throw new Error('解密iframe加载失败')
       }
-      if (window === e.source) {
-        if (this._cacheMessagePromiseMap[id]) {
-          this._cacheMessagePromiseMap[id].reject()
-        }
-        return
+    })
+  }
+
+  onMessage(e) {
+    const id = e.data.id
+    if (e.data.type !== 'after-decrypt') {
+      if (this._cacheMessagePromiseMap[id]) {
+        this._cacheMessagePromiseMap[id].reject()
       }
-      const result = e.data
+      return
+    }
+    if (window === e.source) {
       if (this._cacheMessagePromiseMap[id]) {
-        this._cacheMessagePromiseMap[id].resolve(result)
+        this._cacheMessagePromiseMap[id].reject()
       }
+      return
     }
-    window.removeEventListener('message', onMessage)
+    const result = e.data
+    if (this._cacheMessagePromiseMap[id]) {
+      this._cacheMessagePromiseMap[id].resolve(result)
+    }
+  }
+
+  initMessageEvents() {
+    window.removeEventListener('message', this.onMessage.bind(this))
     // 接收解密后的结果
-    window.addEventListener('message', onMessage)
+    window.addEventListener('message', this.onMessage.bind(this))
   }
 
   sendMessage(payload = {}) {