JSBridge.js 3.53 KB
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
function AppClient(appObj, isAndroid) {
    layer.open({
        content: appObj,
        time: 2
    })
    this.appObj = appObj;
    this.isAndroid = isAndroid;
};
function isJson(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}
function toJson(str) {
    var i = str.index('"{')
    try {
        var arr = str.split(2,str.length - 4)
        console.log(arr)
    } catch (e) {
        return {}
    }
}
// window.invokeJsPassCompanyCodeAndName = function(_params) {
//     companyCode = _params.companyCode;
//     companyName = _params.companyName;
//     var jsonStr = "";
//     getSign(jsonStr);
// }
//
AppClient.prototype.getERCode = function (jsonStr, getCallback, postCallback) {
    var dataParams = "";
    if(isJson(jsonStr)){
        dataParams = JSON.parse(jsonStr);
    }
    var param = {
        from: 'H5',
        params: dataParams
    };
    // if(this.isAndroid) {
    //     this.appObj.callHandler('getERCode', jsonStr, function (res) {
    //         layer.open({
    //             content: res,
    //             time: 2
    //         })
    //     });
    // } else {
    this.appObj.registerHandler("postERCOde", postCallback)
    this.appObj.callHandler("getERCode", param, getCallback);
    // }
};
AppClient.prototype.getGPS = function (jsonStr, getCallback, postCallback) {
    var dataParams = "";
    if(isJson(jsonStr)){
        dataParams = JSON.parse(jsonStr);
    }
    var iOSData = {
        from: 'H5',
        params: dataParams
    };
    // if (this.isAndroid) {
    //     this.appObj.callHandler('getGPS', jsonStr, function (res) {
    //         layer.open({
    //             content: res,
    //             time: 2
    //         })
    //     })
    // } else {
    this.appObj.registerHandler("postGPS", postCallback)
    this.appObj.callHandler("getGPS", param, getCallback);

    // }
};

if(isAndroid || isiOS) {

    function setupWebViewJavascriptBridge(callback) {

        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
        if(isiOS){
            if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
            window.WVJBCallbacks = [callback];
            var WVJBIframe = document.createElement('iframe');
            WVJBIframe.style.display = 'none';
            WVJBIframe.src = 'https://__bridge_loaded__';
            document.documentElement.appendChild(WVJBIframe);
            setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
        } else if (isAndroid) {
            document.addEventListener('WebViewJavascriptBridgeReady', function (event) {
                callback(WebViewJavascriptBridge)
            }, false)
        }
    }
    setupWebViewJavascriptBridge(function(bridge) {

        if(isAndroid) {

            bridge.init(function (message, responseCallback) {
                layer.open({
                    content: '22',
                    time: 2
                })
                responseCallback({'Javascript Responds': 'Wee!'})
                appClient =  new AppClient(bridge, isAndroid);
            })
        }
        if (isiOS) {
            appClient = new AppClient(bridge, false);
        }
        // 此处可以调用一些init的方法
    });
} else {
    appClient = new AppClient({}, false);
    layer.open({
      content: '环境判断失败',
      time: 2
    })
}