function isAppInstall(callback) {
console.log('in isAppInstall')
let startTime = +new Date()
let client = getClient()
let isAndroid = getPlatform() === 'android'
//直接打开
openSchema('weixin://')
//通过是否成功打开判断是否安装
if (isAndroid) {
//android 打开app后,setTimeout中的执行栈被停掉了
setTimeout(function() {
let delta = +new Date() - startTime
if (delta < 850) {
//没有安装
callback('no')
} else {
//安装
callback('yes')
}
}, 800)
} else {
//如果是safari,
//ios打开app后,setTimeout会继续执行,用页面可见性判断
let hidden, state, visibilityChange
if (typeof document.hidden !== 'undefined') {
hidden = 'hidden'
visibilityChange = 'visibilitychange'
state = 'visibilityState'
} else if (typeof document.mozHidden !== 'undefined') {
hidden = 'mozHidden'
visibilityChange = 'mozvisibilitychange'
state = 'mozVisibilityState'
} else if (typeof document.msHidden !== 'undefined') {
hidden = 'msHidden'
visibilityChange = 'msvisibilitychange'
state = 'msVisibilityState'
} else if (typeof document.webkitHidden !== 'undefined') {
hidden = 'webkitHidden'
visibilityChange = 'webkitvisibilitychange'
state = 'webkitVisibilityState'
}
if (visibilityChange) {
//设置一个超时,如果在2s内页面可见性没变化,可以判断是没有安装
//safafi中,会有一个确认弹框,没法判断
//但是如果用户在2秒之内打开客户端然后又返回h5页面,是会呼起appstore的,暂时解决不了这个问题
let timeoutId = setTimeout(function() {
callback('no')
document.removeEventListener(visibilityChange, fn)
}, 2000)
let fn = function(data) {
//如果能打开其余的app,会触发hidden
if (document[state] === hidden || data.hidden) {
callback('yes')
clearTimeout(timeoutId)
}
document.removeEventListener(visibilityChange, fn)
}
if (client === 'qzone') {
window.mqq &&
mqq.addEventListener('qbrowserVisibilityChange', function(da) {
fn(da)
})
} else {
document.addEventListener(visibilityChange, fn, false)
}
} else {
callback('unknow')
}
}
}
function getClient() {
let ua = navigator.userAgent
let isWX = ua.match(/MicroMessenger\/([\d\.]+)/), //微信
isQQ = ua.match(/QQ\/([\d\.]+)/) || ua.match('_SQ_'), //手Q
isQzone = ua.match('Qzone/') || ua.match('_QZ_'), //手空
isQQMusic = /QQMUSIC\/(\d[\.\d]*)/i.test(ua), //qq音乐
isQQBrowser = ua.match('MQQBrowser') && ua.indexOf('TBS') === -1, //qq浏览器
isFM = ua.match('_FM_'), //企鹅FM
isTencentVideo = ua.match('QQLiveBrowser'), //腾讯视频
isQQNews = ua.match('qqnews'), //腾讯新闻
isQQSecure = ua.match('Secure'), // 安全管家
isWiFiManager = /wifimanager/i.test(ua), // wifi管家
isWS = ua.match('_WEISHI_'), //微视
isYYB = /\/qqdownloader\/(\d+)(?:\/(appdetail|external|sdk))?/.test(ua), //应用宝
isWeiyun = ua.match(/Weiyun\/(\d\.)+/) // 微云
let rs = 'normal'
if (isQQ) {
rs = 'qq'
} else if (isQzone) {
rs = 'qzone'
} else if (isWX) {
rs = 'wx'
} else if (isQQMusic) {
rs = 'qqmusic'
} else if (isFM) {
rs = 'fm'
} else if (isWS) {
rs = 'ws'
} else if (isYYB) {
rs = 'yyb'
} else if (isQQBrowser) {
rs = 'QQBrowser'
} else if (isQQSecure) {
rs = 'QQSecure'
} else if (isWiFiManager) {
rs = 'QQSecure' // 手机管家和wifi管家用的是同一套jsBridge
} else if (isQQNews) {
rs = 'QQNews'
} else if (isWeiyun) {
rs = 'Weiyun'
} else if (isTencentVideo) {
rs = 'TencentVideo'
}
return rs
}
function getPlatform() {
let ua = navigator.userAgent
let ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
isIphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
isAndroid = ua.match(/Android/)
if (isIphone || ipad) {
return 'ios'
} else if (isAndroid) {
return 'android'
} else {
return 'pc'
}
}
网友评论