(function(win) { var ua = navigator.userAgent; function getQueryString(name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'); var r = window.location.search.substr(1).match(reg); if (r !== null) return unescape(r[2]); return null; } function isAndroid() { return ua.indexOf('Android') > 0; } function isIOS() { return /(iPhone|iPad|iPod)/i.test(ua); } var mobile = { /** *通过bridge调用app端的方法 * @param method * @param params * @param callback */ callAppRouter: function(method, params, callback) { var req = { 'Method': method, 'Data': params }; console.info(JSON.stringify(req)); if (isIOS()) { // win.app.callRouter(req, function(err, result) { // var resultObj = null; // var errorMsg = null; // if (typeof(result) !== 'undefined' && result !== 'null' && result !== null) { // resultObj = JSON.parse(result); // if (resultObj) { // resultObj = resultObj['result']; // } // } // if (err !== 'null' && typeof(err) !== 'undefined' && err !== null) { // errorMsg = err; // } // callback(err, resultObj); // }); window.webkit.messageHandlers[method].postMessage([params]); } else if (isAndroid()) { win.app[method](params._id, JSON.stringify(params.params)); } }, login: function() { this.callAppRouter('Login', null, function(errMsg, res) { if (errMsg !== null && errMsg !== 'undefined' && errMsg !== 'null') { } else { var name = res['phone']; if (name !== 'undefined' && name !== 'null') { var button = document.getElementById('loginButton'); button.innerHTML = name; } } }); } }; //将mobile对象挂载到window全局 win.webBridge = mobile; })(window);