bp-base-test-txf
Version:
C端 bp-base 基础升级
139 lines (138 loc) • 3.96 kB
JavaScript
import Mtop from '@ali/lib-mtop';
export default class Basic {
constructor (environment) {
this.environment = environment
}
/**
* 测试当前的mtop
*/
getMtop () {
console.log(Mtop, 'Mtop')
return Mtop
}
/**
* 判断是否是IOS
*/
isIOS () {
if (this.environment === 'miniapp') {
return my.getSystemInfoSync().platform == 'iOS'
}
return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) + '1';
}
/**
* 是否是在手淘内
*/
isTaobao() {
if (this.environment === 'miniapp') {
return my.getSystemInfoSync().app === 'TB'
}
return /AliApp\(TB/.test(navigator.userAgent);
}
/**
* 是否在天猫内
*/
isTmall() {
if (this.environment === 'miniapp') {
return my.getSystemInfoSync().app === 'TM'
}
return /AliApp\(TM/.test(navigator.userAgent);
}
/**
* 获取 url 参数 暂时不能在小程序内部使用
*/
getParam(name) {
let url = '';
if (this.environment === 'miniapp') {
url = location.href;
} else {
url = window.location.href;
}
let urlRemoveHash = url.split('#')[0];
let reg = new RegExp('(^|)' + name + '=([^&]*)(|$)');
let matchResult = urlRemoveHash.match(reg);
return matchResult ? matchResult[2] : '';
}
/**
* 更新 url 参数
* @param {string} key
*
*/
updateQueryStringParameter(uri, key, value) {
let re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i');
let hashArray = uri.split('#');
let hash = '';
let noHashUri = hashArray[0];
if (hashArray.length > 1) {
hash = uri.split('#')[1];
}
let separator = uri.indexOf('?') !== -1 ? '&' : '?';
if (uri.match(re)) {
return uri.replace(re, '$1' + key + '=' + value + '$2');
} else {
return noHashUri + separator + key + '=' + value + '#' + hash;
}
}
/**
* 批量更新 url 参数
* @param { obj } params
*/
updateQueryStringParameters(uri, params) {
for (let key in params) {
uri = this.updateQueryStringParameter(uri, key, params[key]);
}
return uri;
}
/**
* pushWindow
* @param {String} url
*/
pushWindow(url) {
if (this.environment === 'miniapp') {
my.navigateTo({
url
})
return false
}
let tempV = navigator.userAgent.match(/AliApp\(TB\/(\d+(\.\d+)*)/i);
let tbAppVersion;
if (tempV) {
tbAppVersion = tempV[1];
}
if (tbAppVersion && Ali._versionCompare(tbAppVersion, '5.11.0') > 0) {
// 手淘下打开一个新的 Native 窗口,并在其中显示指定 URL 的页面。与
// Base.openWindow 功能相同,但在 Android 平台可以打开新窗口。
// 页头合并后必须新开webview,否则页头一会儿有一会儿无
window.WindVane.call(
'WVNative',
'openWindow',
{ url },
function(e) {},
function(e) {
location.href = url;
}
);
} else {
Ali.pushWindow(url, function(data) {
if (data.errorCode) {
location.href = url;
}
});
}
}
/**
* 节流函数
* @param {Fucntion} fn 需要节流的函数
* @param {*} delay 节流时间
*/
throttle(fn, delay) {
let timer = null;
return function() {
let context = this,
args = arguments;
clearTimeout(timer);
timer = setTimeout(function() {
fn.apply(context, args);
}, delay);
};
}
}