/*
* *****xia
*/
let pace = module.exports = exports = {};
let DEVICE_TYPE = getDeviceType();
let PaceBridge = null;
let URL_BASE = 'http://pacehirunact.sparta.html5.qq.com'
pace.checkUrl = function (url, on_succ, on_fail) {
url = getOriginUrl(url);
if (!getBridge()) {
url += url.indexOf('?') >= 0 ? '&' : '?';
url += 'uin=MTEwOTI1&sign=b6b7aef83e9d7f3ae4eaade429383f23&pass_ticket=0dc18f27c9a2472112d0eb38e683b374';
on_succ && on_succ(url);
return;
}
if (DEVICE_TYPE === 'ios') {
try {
PaceBridge.postMessage({
method: 'checkUrl',
arguments: arguments,
url: url,
on_succ: on_succ,
on_fail: on_fail || ((err) => console.error('checkUrl fail. err:', err)),
});
} catch (err) {
console.log(err);
}
} else {
PaceBridge.checkUrl(url, on_succ, on_fail);
}
};
pace.openMapApp = function (param) {
if (!getBridge()) {
console.log('cannot open map app');
return;
}
if (DEVICE_TYPE === 'ios') {
PaceBridge.postMessage({
method: 'openMapApp',
arguments: arguments,
param: param,
});
} else {
PaceBridge.openMapApp(JSON.stringify(param));
}
};
pace.back = function () {
if (!getBridge()) {
window.history && window.history.back && window.history.back();
return;
}
if (DEVICE_TYPE === 'ios') {
PaceBridge.postMessage({
method: 'back',
arguments: arguments,
});
} else {
PaceBridge.back();
}
};
pace.openSetPhone = function () {
if (!getBridge()) {
console.error('open set phone page fail');
return;
}
if (DEVICE_TYPE === 'ios') {
PaceBridge.postMessage({
method: 'openSetPhone',
arguments: arguments,
});
} else {
PaceBridge.openSetPhone();
}
};
pace.shareWx = function (param) {
if (!getBridge()) {
console.log('cannot share to wechat');
return;
}
if (DEVICE_TYPE === 'ios') {
PaceBridge.postMessage({
method: 'shareWx',
arguments: arguments,
param: param,
});
} else {
PaceBridge.shareWx(JSON.stringify(param));
}
};
function getOriginUrl(url) {
if (url[0] !== '/')
return url;
getOriginUrl.cache = getOriginUrl.cache || {};
return getOriginUrl.cache[url] = getOriginUrl.cache[url] || URL_BASE + url;
}
function getBridge() {
if (!PaceBridge) {
if (DEVICE_TYPE === 'ios')
PaceBridge = window.webkit.messageHandlers.PaceBridge;
else
PaceBridge = window.PaceBridge;
}
return PaceBridge;
}
function getBrowser() {
return {
versions: function () {
var u = navigator.userAgent,
app = navigator.appVersion;
return { // 移动终端浏览器版本信息
trident: u.indexOf('Trident') > -1, // IE内核
presto: u.indexOf('Presto') > -1, // opera内核
webKit: u.indexOf('AppleWebKit') > -1, // 苹果、谷歌内核
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, // 火狐内核
mobile: !!u.match(/AppleWebKit.*Mobile.*/), // 是否为移动终端
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, // android终端或uc浏览器
iPhone: u.indexOf('iPhone') > -1, // 是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') > -1, // 是否iPad
webApp: u.indexOf('Safari') == -1, // 是否web应该程序,没有头部与底部
weixin: u.match(/MicroMessenger/i) == "micromessenger" || window.WeixinJSBridge != undefined
};
}(),
language: (navigator.browserLanguage || navigator.language).toLowerCase()
};
}
function getDeviceType() {
var result = '';
var browser = getBrowser();
if (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) {
result = 'ios';
console.log(result);
return result;
} else if (browser.versions.android) {
result = 'android';
console.log(result);
return result;
} else {
console.log(result);
return result;
}
}