JSBridge.js 3.38 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) {
    this.appObj = appObj;
    this.isAndroid = isAndroid;
};

function isJson(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}
function toJson(str) {
    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 (callbackName, callback, jsonStr) {
    var dataParams = "";
    if(isJson(jsonStr)){
        dataParams = JSON.parse(jsonStr);
    }
    var iOSData = {
        from: 'H5',
        params: dataParams
    };
    if (this.isAndroid) {
        window.WebViewJavascriptBridge.callHandler('getERCode', jsonStr, function (res) {
            layer.open({
                content: res,
                time: 2
            })
        });
    } else {
        this.appObj.callHandler("getERCode", iOSData, callback);

    }
};
AppClient.prototype.getGPS = function (callbackName, callback, jsonStr) {
    var dataParams = "";
    if(isJson(jsonStr)){
        dataParams = JSON.parse(jsonStr);
    }
    var iOSData = {
        from: 'H5',
        params: dataParams
    };
    if (this.isAndroid) {
        this.appObj.getGPS('H5', jsonStr);
    } else {
        this.appObj.callHandler("getGPS", iOSData, callback);

    }
};
//
// window. = function (_params) {
//
// }
// window.postGPS = function (params) {
//     layer.open({
//         content: JSON.stringify(_params),
//         time: 10
//     });
// }
// var appClient = new AppClient(window.android, true);
// layer.open({
//     content:  window.android,
//     time: 2
// })
layer.open({
    content:  'hah' ,
    time: 2
})
// bridge.registerHandler('postGPS', function(data, responseCallback) {
//     log('ObjC called testJavascriptHandler with', data)
//     var responseData = { 'Javascript Says':'Right back atcha!' }
//     log('JS responding with', responseData)
//     responseCallback(responseData)
// })


if(isAndroid || isiOS) {
    function setupWebViewJavascriptBridge(callback) {
        layer.open({
            content:  window.WebViewJavascriptBridge ,
            time: 2
        })
        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
        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)
    }
    setupWebViewJavascriptBridge(function(bridge) {
        if(isAndroid) {
            // appClient =  new AppClient(bridge, isAndroid);
        }
        if (isiOS) {
            appClient = new AppClient(bridge, isAndroid);
        }
        // 此处可以调用一些init的方法
    });
} else {
    layer.open({
      content: '环境判断失败',
      time: 2
    })
}