浏览代码

当当前环境未nvue时,才初始化 uniapp 相关逻辑

x86 1 年之前
父节点
当前提交
3a9fde103d
共有 2 个文件被更改,包括 15 次插入6 次删除
  1. 1 0
      open-work/public/index.html
  2. 14 6
      open-work/src/jssdk/bridgeClientImpl.uni.js

+ 1 - 0
open-work/public/index.html

@@ -7,6 +7,7 @@
     <link rel="icon" href="<%= BASE_URL %>favicon.ico">
     <title><%= htmlWebpackPlugin.options.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 <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>

+ 14 - 6
open-work/src/jssdk/bridgeClientImpl.uni.js

@@ -66,9 +66,17 @@ function register(handlerName, callback) {
     eventListeners[handlerName] = callback;
 }
 
-if (uni.postMessage) {
-    console.log('init uni client')
-    init();
-} else {
-   console.log('not init uni client')
-}
+console.log('add UniAppJSBridgeReady listener')
+document.addEventListener('UniAppJSBridgeReady', () => {
+    console.log('receive UniAppJSBridgeReady event')
+    uni.getEnv((res) => {
+        console.log('当前环境:' + JSON.stringify(res));
+        // web 端 h5: true
+        if (res.nvue){
+            console.log('init uni client')
+            init();
+        }else {
+            console.log('not init uni client')
+        }
+    });
+});