From 1157e5ee2c61319b7d6ba6d22936576f638743dc Mon Sep 17 00:00:00 2001 From: Kntt Date: Tue, 18 Jun 2019 11:06:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=EF=BC=8C=E5=A4=84=E7=90=86=E5=AE=89?= =?UTF-8?q?=E5=8D=93=E7=9A=84=E9=97=AE=E9=A2=98=20#12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/plugin.js | 42 +++++++++++++++++++++------------- lib/utils.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 15 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index 67125bf..faa6991 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -3,7 +3,8 @@ * @author Kntt 20190216 */ import MockBridge from './mock' - +import { getDeviceInfo } from './utils' +const deviceInfo = getDeviceInfo() export default class VueJsBridgePlugin { constructor (options = {}) { this.options = options @@ -21,21 +22,32 @@ export default class VueJsBridgePlugin { const bridge = new MockBridge(mockHandler) return callback(bridge) } - // 以下为[WebViewJavascriptBridge](https://github.com/marcuswestin/WebViewJavascriptBridge)源码 - if (window.WebViewJavascriptBridge) { - return callback(window.WebViewJavascriptBridge) - } - if (window.WVJBCallbacks) { - return window.WVJBCallbacks.push(callback) + if (deviceInfo.android) { + // 以下为[JsBridge](https://github.com/lzyzsd/JsBridge)源码 -- android + if (window.WebViewJavascriptBridge) { + callback(window.WebViewJavascriptBridge) + } else { + document.addEventListener('WebViewJavascriptBridgeReady', function () { + callback(window.WebViewJavascriptBridge) + }, false) + } + } else { + // 以下为[WebViewJavascriptBridge](https://github.com/marcuswestin/WebViewJavascriptBridge)源码 -- ios + if (window.WebViewJavascriptBridge) { + return callback(window.WebViewJavascriptBridge) + } + if (window.WVJBCallbacks) { + return window.WVJBCallbacks.push(callback) + } + window.WVJBCallbacks = [callback] + var WVJBIframe = document.createElement('iframe') + WVJBIframe.style.display = 'none' + WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__' + document.documentElement.appendChild(WVJBIframe) + setTimeout(function () { + document.documentElement.removeChild(WVJBIframe) + }, 0) } - window.WVJBCallbacks = [callback] - var WVJBIframe = document.createElement('iframe') - WVJBIframe.style.display = 'none' - WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__' - document.documentElement.appendChild(WVJBIframe) - setTimeout(function () { - document.documentElement.removeChild(WVJBIframe) - }, 0) } /** * 注册提供native调用的方法 diff --git a/lib/utils.js b/lib/utils.js index 49e68db..ce3c137 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -25,3 +25,65 @@ export const type = function (o) { export const toCamelCaseVar = (variable) => { return variable.replace(/\-(\w)/g, (_, letter) => letter.toUpperCase()) } + +/** + * 获取设备信息 + */ +export const getDeviceInfo = () => { + var device = {} + var ua = navigator.userAgent + + var windows = ua.match(/(Windows Phone);?[\s\/]+([\d.]+)?/) + var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/) + var ipad = ua.match(/(iPad).*OS\s([\d_]+)/) + var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/) + var iphone = !ipad && ua.match(/(iPhone\sOS|iOS)\s([\d_]+)/) + + device.ios = device.android = device.windows = device.iphone = device.ipod = device.ipad = device.androidChrome = false + + // Windows + if (windows) { + device.os = 'windows' + device.osVersion = windows[2] + device.windows = true + } + // Android + if (android && !windows) { + device.os = 'android' + device.osVersion = android[2] + device.android = true + device.androidChrome = ua.toLowerCase().indexOf('chrome') >= 0 + } + if (ipad || iphone || ipod) { + device.os = 'ios' + device.ios = true + } + // iOS + if (iphone && !ipod) { + device.osVersion = iphone[2].replace(/_/g, '.') + device.iphone = true + } + if (ipad) { + device.osVersion = ipad[2].replace(/_/g, '.') + device.ipad = true + } + if (ipod) { + device.osVersion = ipod[3] ? ipod[3].replace(/_/g, '.') : null + device.iphone = true + } + // iOS 8+ changed UA + if (device.ios && device.osVersion && ua.indexOf('Version/') >= 0) { + if (device.osVersion.split('.')[0] === '10') { + device.osVersion = ua + .toLowerCase() + .split('version/')[1] + .split(' ')[0] + } + } + device.iphonex = device.ios && screen.height === 812 && screen.width === 375 + // Webview + device.webView = + (iphone || ipad || ipod) && ua.match(/.*AppleWebKit(?!.*Safari)/i) + + return device +}