{"id":1597,"date":"2023-03-25T20:03:40","date_gmt":"2023-03-25T12:03:40","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1597"},"modified":"2023-04-27T21:25:46","modified_gmt":"2023-04-27T13:25:46","slug":"jsbridge-encapsulation-and-interaction-in-vue","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/jsbridge-encapsulation-and-interaction-in-vue\/","title":{"rendered":"JSbridge\u5728Vue\u7684\u5c01\u88c5\u4e0e\u4ea4\u4e92"},"content":{"rendered":"<p>JSbridge: <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/lzyzsd\/JsBridge\">https:\/\/github.com\/lzyzsd\/JsBridge<\/a><\/p>\n<h2>JSbridge.js<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-javascript\">let isAndroid = navigator.userAgent.indexOf(&#039;Android&#039;) &gt; -1 || navigator.userAgent.indexOf(&#039;Adr&#039;) &gt; -1;\nlet isiOS = !!navigator.userAgent.match(\/\\(i[^;]+;( U;)? CPU.+Mac OS X\/);\n\n\/\/\u8fd9\u662f\u5fc5\u987b\u8981\u5199\u7684\uff0c\u7528\u6765\u521b\u5efa\u4e00\u4e9b\u8bbe\u7f6e\nfunction setupWebViewJavascriptBridge(callback) {\n    \/\/Android\u4f7f\u7528\n    if (isAndroid) {\n        if (window.WebViewJavascriptBridge) {\n            callback(WebViewJavascriptBridge)\n        } else {\n            document.addEventListener(\n                &#039;WebViewJavascriptBridgeReady&#039;,\n                function () {\n                    callback(WebViewJavascriptBridge)\n                },\n                false\n            );\n        }\n        console.log(&#039;tag&#039;, &#039;\u5b89\u5353&#039;)\n        sessionStorage.phoneType = &#039;android&#039;\n    }\n\n    \/\/iOS\u4f7f\u7528\n    if (isiOS) {\n        if (window.WebViewJavascriptBridge) {\n            return callback(WebViewJavascriptBridge);\n        }\n        if (window.WVJBCallbacks) {\n            return window.WVJBCallbacks.push(callback);\n        }\n        window.WVJBCallbacks = [callback];\n        var WVJBIframe = document.createElement(&#039;iframe&#039;);\n        WVJBIframe.style.display = &#039;none&#039;;\n        WVJBIframe.src = &#039;wvjbscheme:\/\/__BRIDGE_LOADED__&#039;;\n        document.documentElement.appendChild(WVJBIframe);\n        setTimeout(function () {\n            document.documentElement.removeChild(WVJBIframe)\n        }, 0);\n        console.log(&#039;tag&#039;, &#039;ios&#039;);\n        sessionStorage.phoneType = &#039;ios&#039;\n    }\n}\n\n\/\/\u6ce8\u518c\u56de\u8c03\u51fd\u6570\uff0c\u7b2c\u4e00\u6b21\u8fde\u63a5\u65f6\u8c03\u7528\u521d\u59cb\u5316\u51fd\u6570\uff08Android\u9700\u8981\u521d\u59cb\u5316\uff0cios\u4e0d\u7528\uff09\nsetupWebViewJavascriptBridge(function (bridge) {\n    if (isAndroid) {\n        \/\/\u521d\u59cb\u5316\n        bridge.init(function (message, responseCallback) {\n            var data = {\n                &#039;Javascript Responds&#039;: &#039;Wee!&#039;\n            };\n            responseCallback(data);\n        })\n    }\n})\n\nexport default {\n    \/\/ js\u8c03APP\u65b9\u6cd5 \uff08\u53c2\u6570\u5206\u522b\u4e3a:app\u63d0\u4f9b\u7684\u65b9\u6cd5\u540d  \u4f20\u7ed9app\u7684\u6570\u636e  \u56de\u8c03\uff09\n    callHandler(name, data, callback) {\n        setupWebViewJavascriptBridge(function (bridge) {\n            bridge.callHandler(name, data, callback)\n        })\n    },\n    \/\/ APP\u8c03js\u65b9\u6cd5 \uff08\u53c2\u6570\u5206\u522b\u4e3a:js\u63d0\u4f9b\u7684\u65b9\u6cd5\u540d  \u56de\u8c03\uff09\n    registerHandler(name, callback) {\n        setupWebViewJavascriptBridge(function (bridge) {\n            bridge.registerHandler(name, function (data, responseCallback) {\n                callback(data, responseCallback)\n            })\n        })\n    }\n}<\/code><\/pre>\n<h2>main.js\u4e2d\u5f15\u5165<\/h2>\n<pre><code class=\"language-javascript\">import Bridge from &#039;.\/config\/JSbridge.js&#039;\nVue.prototype.$bridge = Bridge<\/code><\/pre>\n<h2>\u7ec4\u4ef6\u4e2d\u4f7f\u7528<\/h2>\n<p>\u65b9\u6cd5\u540d\u548capp\u5f00\u53d1\u4eba\u5458\u6c9f\u901a\u597d\uff0c\u8fd9\u91ccjs\u8c03app\uff0capp\u63d0\u4f9b\u7684\u65b9\u6cd5\u662f<code>callNative<\/code>\uff1bapp\u8c03js\uff0cjs\u63d0\u4f9b\u7684\u65b9\u6cd5\u662f<code>callJs<\/code><\/p>\n<pre><code class=\"language-javascript\">\/\/\u6ce8\u518c\u65b9\u6cd5\n  mounted() {\n    this.registerJs()\n  },\n\n\/\/js\u8c03app\n  sendMsg() {\n    let msg = this.msg\n    this.$bridge.callHandler(&#039;callNative&#039;, msg, (res)=&gt;{\n      alert(&#039;\u83b7\u53d6app\u54cd\u5e94\u6570\u636e:&#039; + res)\n      this.test = res\n    })\n  },\n\n\/\/app\u8c03js\n  registerJs() {\n    this.$bridge.registerHandler(&#039;callJs&#039;, (data, responseCallback) =&gt; {\n      alert(&#039;app\u4e3b\u52a8\u8c03\u7528js\u65b9\u6cd5\uff0c\u4f20\u5165\u6570\u636e:&#039; + data)\n      responseCallback(data)\n    })\n  }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JSbridge: https:\/\/github.com\/lzyzsd\/JsBridge JSbridge.j [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[320],"class_list":["post-1597","post","type-post","status-publish","format-standard","hentry","category-vue","tag-jsbridge"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1597"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1597\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}