Created
May 26, 2017 02:04
-
-
Save forthxu/7832933b9e5fd7122a2a3843ade29d82 to your computer and use it in GitHub Desktop.
app下载处理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Emt = (function() { | |
var env = {}, | |
navigator = window.navigator, | |
userAgent = navigator.userAgent, | |
ios = userAgent.match(/(iPad|iPhone|iPod)[^;]*;.+OS\s([\d_\.]+)/), | |
android = userAgent.match(/(Android)\s([\d\.]+)/); | |
env.isAndroid = (/android/gi).test(navigator.appVersion); | |
env.isIDevice = (/iphone|ipad|ipod/gi).test(navigator.appVersion); | |
env.isWebkit = /WebKit\/[\d.]+/i.test(userAgent); | |
env.isSafari = ios ? (navigator.standalone ? env.isWebkit : (/Safari/i.test(userAgent) && !/CriOS/i.test(userAgent) && !/MQQBrowser/i.test(userAgent))) : false; | |
env.isUC = /uc/i.test(); | |
if (ios) { | |
env.device = ios[1]; | |
env.version = ios[2].replace(/_/g, '.'); | |
} else if (android) { | |
env.version = android[2]; | |
} | |
env.standalone = navigator.standalone; | |
env.wechat = navigator.userAgent.indexOf("MicroMessenger") >= 0; | |
return env; | |
})(); | |
if (Emt.standalone && Emt.version < "7.0") { | |
document.write('<meta name="apple-mobile-web-app-capable" content="yes">'); | |
} | |
var MPage = MPage = {}; | |
$.extend(MPage, { | |
cache: {}, | |
init: function() { | |
var that = this; | |
//底部提示 | |
var $app_guide = $("#j-app_guide"); | |
$app_guide.find(".btn-close").bind("click", function(e) { | |
e.preventDefault(); | |
$app_guide.remove(); | |
$(window).unbind("scroll.app"); | |
}); | |
//滚动显示 | |
$(window).bind("scroll.app", function() { | |
if (window.scrollY > 79) { | |
$app_guide.show(); | |
$("body").css({marginBottom: "50px"}); | |
} else { | |
$app_guide.hide(); | |
$("body").css({marginButton: "0px"}); | |
} | |
}); | |
//下载按钮 | |
var $app_download = $("#j-app_guide .btn,#j-app_downbtn,.j-app_downbtn"), | |
app_down_url; | |
//下载链接 | |
if (Emt.isIDevice) { | |
app_down_url = $app_download.attr('data-i'); | |
} else if (Emt.wechat) { | |
app_down_url = $app_download.attr('href'); | |
} else if (Emt.isAndroid) { | |
app_down_url = $app_download.attr('data-a'); | |
} else { | |
app_down_url = $app_download.attr('href'); | |
} | |
$app_download.attr("href", app_down_url); | |
//点击下载或启动 | |
$app_download.bind("click", function() { | |
if (Emt.isAndroid) { | |
AndroidConnect.launch('zizhuAPP://launch?type=main', function() { | |
document.location.href = app_down_url;//失败时调用 | |
}); | |
return false; | |
}else if (app_down_url.indexOf('http') > -1){ | |
//document.location.href = app_down_url; | |
//return false; | |
} | |
}); | |
} | |
}); | |
var AndroidConnect = { | |
initLaunch: function() { | |
var _this = this; | |
if (!this.frame) { | |
this.frame = document.createElement("iframe"); | |
this.frame.height = "0"; | |
this.frame.width = "0"; | |
this.frame.frameBorder = 0; | |
document.body.appendChild(this.frame); | |
this.timeout = 1000; | |
window.onblur = function() { | |
clearTimeout(_this.timer); | |
_this.timer = null; | |
}; | |
this.frame.onload = function() { | |
console.log("404 onload"); | |
if (_this.timer == null) { | |
return; | |
} | |
clearTimeout(_this.timer); | |
_this.timer = null; | |
_this.launchFallback(new Date() - 0); | |
} | |
this.frame.onerror = function() { | |
console.log("onerror"); | |
if (_this.timer == null) { | |
return; | |
} | |
clearTimeout(_this.timer); | |
_this.timer = null; | |
_this.launchFallback(new Date() - 0); | |
} | |
} | |
}, | |
launchFallback: function(start_time) { | |
var now_time = Date.now(); | |
console.log(now_time - start_time, this.timeout + 200); | |
if (!start_time || now_time - start_time < this.timeout + 200) { | |
this.fallback(); | |
} | |
}, | |
launch: function(launch_url, fallback) { | |
var _this = this, | |
start_time = Date.now(); | |
this.launch_url = launch_url || 'zizhuAPP://launch'; | |
this.fallback = typeof fallback === "function" ? fallback : function() { | |
}; | |
this.initLaunch(); | |
//超时错误 | |
if (this.fallback) { | |
this.timer = setTimeout(function() { | |
_this.launchFallback(start_time); | |
}, _this.timeout); | |
} | |
this.redirect(); | |
}, | |
redirect: function() { | |
this.frame.setAttribute("src", this.launch_url); | |
} | |
} | |
MPage.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment