瀏覽代碼

fix message userinfo

jihuaqiang 2 年之前
父節點
當前提交
17b370ca80
共有 5 個文件被更改,包括 76 次插入10 次删除
  1. 6 4
      src/http/request.js
  2. 20 2
      src/uilts/chromeExtension.js
  3. 3 1
      src/uilts/help.js
  4. 44 2
      src/uilts/messageCenter/index.js
  5. 3 1
      src/uilts/messageCenter/messageEnum.js

+ 6 - 4
src/http/request.js

@@ -1,7 +1,7 @@
 import axios from 'axios'
 import { getChromeStorageFromExtension } from '@/uilts/chromeExtension.js'
 import { baseAPIUrl, appVersionCode } from '@/http/configAPI.js'
-
+import { getQueryString } from '@/uilts/help.js';
 let userInfo = '';
 let storage_mid = ''
 
@@ -30,11 +30,13 @@ function checkParams(config) {
       delete params.params.pageSource;
     }
 
+    const denetVersionCode = getQueryString('appVersionCode') || appVersionCode;
+
     if (!baseInfo || !baseInfo.token) {
       params['baseInfo'] = {
         token: token,
         mid,
-        appVersionCode,
+        appVersionCode: denetVersionCode,
         loginUid: uid,
         uid,
         appType: 1,
@@ -74,9 +76,9 @@ function checkParams(config) {
 
 // request拦截器
 service.interceptors.request.use(async (config) => {
-  userInfo = await getChromeStorageFromExtension('userInfo') || ''
+  userInfo = await getChromeStorageFromExtension('userInfo').catch((e) => { console.log(e)}) || ''
   if (!storage_mid) {
-    storage_mid = await getChromeStorageFromExtension('mid') || ''
+    storage_mid = await getChromeStorageFromExtension('mid').catch((e) => { console.log(e)}) || ''
   }
   return checkParams(config)
 }, error => {

+ 20 - 2
src/uilts/chromeExtension.js

@@ -1,5 +1,7 @@
 import { pageUrl } from "@/http/configAPI.js"
-import { guid } from "@/uilts/help";
+import { guid, iframeID } from "@/uilts/help";
+import messageCenter from '@/uilts/messageCenter';
+import MESSAGE_ENUM from "@/uilts/messageCenter/messageEnum";
 
 export const LANDING_PAGE = {
     name: 'received_log',
@@ -28,9 +30,25 @@ export function setChromeStorage(params, callback) {
     }
 }
 
-export function getChromeStorageFromExtension(key = '') {
+export async function getChromeStorageFromExtension(key = '') {
     let params = {}
     params[key] = ''
+    return new Promise((res, rej) => { 
+        messageCenter.send({
+            actionType: MESSAGE_ENUM.IFRAME_GET_EXTENSION_STORGE_DATA,
+            data: {
+                iframeID,//用于告诉父窗口会传消息给哪个iframe
+                key,// storage key
+                messageID: new Date().getTime() // 唯一的ID,用于标记回调函数
+            },
+            callback: (data) => {
+                res(data)
+            },
+            failback: (e) => { 
+                rej(e)
+            }
+        })
+    })
 
     // console.log(window.parent.window.atest)
     // window.postMessage({actionType: 'iframe_test', data: key})

+ 3 - 1
src/uilts/help.js

@@ -257,4 +257,6 @@ export function $(key, cache = true) {
     }
   }
   return _dom
-}
+}
+
+export const iframeID = getQueryString('iframeID');

+ 44 - 2
src/uilts/messageCenter/index.js

@@ -3,14 +3,22 @@ class MessageCenter {
     constructor() { 
         //  缓存事件队列
         this.messageCallbackMap = new Map();
+        this.messageFailbackMap = new Map();
         this.init()
     }
 
-    send({ actionType, data }) {
+    send({ actionType, data, callback, overTime, failback }) {
         window.parent.postMessage({
             actionType,
             data
         }, '*');
+        if (data.messageID && callback) {
+            // 带回调callback 的message, 要求携带messageID,callback,failback等
+            this.listen(`${actionType}-${data.messageID}`, callback)
+            if (failback) { 
+                this.addFailback(`${actionType}-${data.messageID}`, overTime, failback)
+            }
+        }
     }
 
     listen(actionType, callback) {
@@ -22,9 +30,25 @@ class MessageCenter {
         }
     }
 
-    init() { 
+    addFailback(actionType, overTime=2000, failback) { 
+        let failbackQuene = this.messageFailbackMap.get(actionType);
+        if (failbackQuene && failbackQuene.failCallbackList) {
+            failbackQuene.failCallbackList.push(
+                failback
+                )
+            } else { 
+                this.messageFailbackMap.set(actionType, {
+                    time: new Date().getTime(),
+                    overTime,
+                    failCallbackList: [failback]
+                })
+            }
+    }
+
+    init() {
         window.addEventListener('message', (e) => { 
             const { actionType, data } = e.data;
+            this.messageFailbackMap.delete(actionType);
             const quene = this.messageCallbackMap.get(actionType) ||  [];
             let index = 0;
             while (index < quene.length) { 
@@ -33,6 +57,24 @@ class MessageCenter {
                 index++
             }
         })
+        setInterval(() => { 
+            // 轮询查看有无超期的message信息
+            const now = new Date().getTime();
+            for (let item of this.messageFailbackMap.values()) { 
+                if (now - item.time > item.overTime) { 
+                    let index = 0;
+                    while (index < item.failCallbackList.length) { 
+                        const callback = item.failCallbackList[index];
+                        callback({
+                            error: 0,
+                            msg: "message 超时错误"
+                        })
+                        index++
+                    }
+                }
+            }
+
+        }, 1000)
     }
 }
 

+ 3 - 1
src/uilts/messageCenter/messageEnum.js

@@ -3,7 +3,9 @@ const SEND_MESSAGE_ENUM =  {
     IFREME_TAB_GROUP_SET_IFRAME_HEIGHT: 'IFREME_TAB_GROUP_SET_IFRAME_HEIGHT',
     /** group tab 内的列表项点击 */
     IFRAME_PAGE_JUMP: 'IFRAME_PAGE_JUMP',
-    IFREME_TAB_GROUP_CONTENT_GET_NAV_TOP: 'IFREME_TAB_GROUP_CONTENT_GET_NAV_TOP'
+    IFREME_TAB_GROUP_CONTENT_GET_NAV_TOP: 'IFREME_TAB_GROUP_CONTENT_GET_NAV_TOP',
+    /** 获取content的localstorge数据 */
+    IFRAME_GET_EXTENSION_STORGE_DATA: 'IFRAME_GET_EXTENSION_STORGE_DATA'
 }
 
 /** 接收父窗口的事件定义 */