Vue的写法
main.js中定义:
/* ================================= APP下载 start ========================================*/
// 判断是安卓还是IOS
Vue.prototype.detect = function (){
var equipmentType = "";
var agent = navigator.userAgent.toLowerCase();
var android = agent.indexOf("android");
var iphone = agent.indexOf("iphone");
var ipad = agent.indexOf("ipad");
if(android != -1){
console.log('android')
equipmentType = "android";
}
if(iphone != -1 || ipad != -1){
console.log('ios')
equipmentType = "ios";
}
return equipmentType;
}
// 判断是否是微信或者微博环境
Vue.prototype.softEnviroment = function () {
let ua = window.navigator.userAgent.toLowerCase();
let isweixin = ua.match(/micromessenger/i) != null;
let isweibo = ua.match(/weibo/i) == "weibo";
if (isweixin) {
return 'weixin'
}else if (isweibo) {
return 'weibo'
}
return ''
}
// IOS 的唤起
Vue.prototype.downloadIOSApp = function (urlScheme, storeURL, fallback, universalLink) {
var tid = this.deferFallback(3000, storeURL, fallback);
let str = navigator.userAgent.toLowerCase();
let list = str.match(/cpu iphone os (.*?) like mac os/);
console.log(str, list)
if (parseInt(list[1], 10) < 8) {
this.bindPagehideEvent(tid);
} else {
this.bindVisibilityChangeEvent(tid);
}
if (parseInt(list[1], 10) > 8) {
// 通过universalLink
if (universalLink === undefined) {
universalLink = urlScheme;
} else {
clearTimeout(tid);
}
this.vLocation(universalLink);
} else {
this.vIframe(urlScheme);
}
}
// IOS 中的 可见性事件
Vue.prototype.bindPagehideEvent = function (tid) {
window.addEventListener('pagehide', function clear() {
if (this.isPageVisible()) {
clearTimeout(tid);
window.removeEventListener('pagehide', clear);
}
});
}
Vue.prototype.bindVisibilityChangeEvent = function (tid) {
document.addEventListener('visibilitychange', function clear() {
if (this.isPageVisible()) {
clearTimeout(tid);
document.removeEventListener('visibilitychange', clear);
}
});
}
Vue.prototype.isPageVisible = function () {
var attrNames = ['hidden', 'webkitHidden'];
for (var i = 0, len = attrNames.length; i < len; i++) {
if (typeof document[attrNames[i]] !== 'undefined') {
return !document[attrNames[i]];
}
}
return true;
}
// 安卓 的唤起
Vue.prototype.deferFallback = function (timeout, storeURL, fallback) {
var clickedAt = new Date().getTime();
return setTimeout(function () {
var now = new Date().getTime();
if (Vue.prototype.isPageVisible() && now - clickedAt < timeout + 1000) {
fallback(storeURL);
}
}, timeout);
}
// 打开页面的方式可能
// 1. 通过改变location
Vue.prototype.vLocation = function (urlScheme) {
window.location.href = urlScheme;
}
// 2. 通过ifreame
Vue.prototype.vIframe = function (urlScheme) {
setTimeout(() => {
var iframe = this.createHiddenIframe('appLauncher');
iframe.src = urlScheme;
}, 100);
}
// 3. 通过intent
function vIntent(launchURI) {
if (browser.name == 'Chrome') {
move();
} else {
setTimeout(move, 100);
}
function move() {
// window.top.location.href = launchURI;
window.location.href = launchURI
}
}
Vue.prototype.createHiddenIframe = function (id) {
const ele = document.createElement('iframe');
ele.id = id;
ele.style.display = 'none';
document.documentElement.appendChild(ele);
return ele;
}
/* ================================= APP下载 end ========================================*/
vue中调用
// 进入应用宝下载APP(区分ios和安卓)
if (enviro === 'weibo' || enviro === 'weixin') {
// 如果是微博或者微信的话,则android提示“选择在浏览器中打开”,IOS提示“选择“在safari中打开””
this.$refs.tipBg.style.display = 'block';
} else {
// 区分android和IOS
if (this.palateInfo === 'android') {
this.deferFallback(0, '/apis/web/app/download/KTB.apk', this.vLocation);
} else if (this.palateInfo === 'ios') {
this.downloadIOSApp('baswiftdiscount://', 'https://apps.apple.com/cn/app/%E5%BF%AB%E8%B4%B4%E5%AE%9D/id1527518469', this.vLocation, 'https://ktb.rzline.com.cn');
}
}