Browse Source

支持 uniapp

x86 1 year ago
parent
commit
829271a1d4

+ 2 - 1
open-work/package.json

@@ -43,7 +43,8 @@
       "parser": "babel-eslint"
     },
     "rules": {
-      "no-unused-vars": "off"
+      "no-unused-vars": "off",
+      " no-undef": "off"
     }
   },
   "browserslist": [

+ 3 - 2
open-work/src/App.vue

@@ -1,7 +1,7 @@
 <template>
     <div id="app">
         <div class="top">
-            <p v-if="account">{{'欢迎 ' + account.displayName}}</p>
+            <p v-if="account">{{ '欢迎 ' + account.displayName }}</p>
             <p v-else>欢迎使用野火IM工作台</p>
         </div>
         <div v-if="!showManageFavApp" class="apps-container">
@@ -51,6 +51,7 @@
 
 import wf from "@/jssdk/wf";
 import api from "@/api/api";
+import './jssdk/bridgeClientImpl.uni'
 
 export default {
     name: 'App',
@@ -236,7 +237,7 @@ body {
     align-items: center;
 }
 
-.title-action-container .action-container{
+.title-action-container .action-container {
     display: flex;
     flex-direction: row;
 }

+ 69 - 0
open-work/src/jssdk/bridgeClientImpl.uni.js

@@ -0,0 +1,69 @@
+/**
+ * 大概流程
+ *  采用类 client-server 模式实现开放平台 js sdk,有两部分相关代码:
+ * 1. client 运行在工作台所加载的 webview 里面
+ * 2. server 直接运行在工作台页面里面,也就是主窗口的渲染进程
+ * 3. client 和 server 之间的交互,通过 websocket 进行中转
+ *
+ */
+let callbackMap = new Map();
+let eventListeners = {};
+let requestId = 0;
+let client;
+
+function init() {
+    window.__messageFromUni = (data) => {
+        console.log('__messageFromUni', data)
+        let obj;
+        try {
+            obj = JSON.parse(data);
+        } catch (e) {
+            console.error('parse ws data error', e);
+            return;
+        }
+        if (obj.type === 'wf-op-event') {
+            handleOpEvent(obj.handlerName, obj.args);
+        } else if (obj.type === 'wf-op-response') {
+            handleOpResponse(obj.requestId, obj.args)
+        }
+    }
+
+    window.__wf_bridge_ = {
+        call: call,
+        register: register,
+    }
+    console.log('bridgeClientImpl init')
+}
+
+function call(handlerName, args, callback) {
+    let reqId = 0;
+    if (callback && typeof callback === 'function') {
+        reqId = requestId++;
+        callbackMap.set(reqId, callback)
+    }
+    let appUrl = location.href;
+    let obj = {type: 'wf-op-request', requestId: reqId, appUrl, handlerName, args};
+    console.log('wf-op-request', obj)
+    uni.postMessage({data: {obj: JSON.stringify(obj)}})
+    // uni.send(JSON.stringify(obj));
+}
+
+function handleOpResponse(requestId, args) {
+    console.log('handle op response', requestId, args)
+    let cb = callbackMap.get(requestId);
+    if (cb) {
+        cb(args);
+        callbackMap.delete(requestId);
+    }
+}
+
+function handleOpEvent(handlerName, args) {
+    eventListeners[handlerName] && eventListeners[handlerName](args);
+
+}
+
+function register(handlerName, callback) {
+    eventListeners[handlerName] = callback;
+}
+
+init();

+ 1 - 0
open-work/src/output-template.html

@@ -7,6 +7,7 @@
     <meta name=viewport content="width=device-width,initial-scale=1">
     <title>example title</title>
 </head>
+<script type="text/javascript" src="https://static.wildfirechat.cn/uni.webview.1.5.4.js"></script>
 <body><noscript><strong>We're sorry but my example doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript>
 <div id=app>