baimacms/utils/env.js

272 lines
6.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* ============================================================
* 环境检测器 - uni-app 适配版
* 支持H5、微信小程序、支付宝小程序、百度小程序、抖音小程序、App
* ============================================================
*
* 原始版本基于 navigator.userAgentH5 专用)
* 本版本通过 uni-app 条件编译适配多端
*
* 【用法】
* import { getEnvironment, detect, isWechat, isBrowser, ... } from '@/utils/env.js';
*
* const env = getEnvironment();
* if (env.isBrowser) { ... }
* if (env.isWechatWebview) { ... }
* ============================================================
*/
/* ========== H5 平台:原始 UA 检测 ========== */
// #ifdef H5
const UA = (navigator.userAgent || '').toLowerCase();
function _isWechatMiniProgram() {
if (window.__wxjs_environment === 'miniprogram') return true;
if (/miniprogram/.test(UA)) return true;
return false;
}
function _isWechatH5() {
if (!/micromessenger/.test(UA)) return false;
if (_isWechatMiniProgram()) return false;
return true;
}
function _isWechat() {
return /micromessenger/.test(UA);
}
function _isAppWebview() {
const patterns = [
/aliapp\(tb/i,
/aliapp\(tm/i,
/jdapp/i,
/aweme/i,
/ksnc/i,
/weibo/i,
/dingtalk/i,
/alipayclient/i,
/baiduboxapp/i,
/qq\//i,
];
return patterns.some(function (re) { return re.test(UA); });
}
function _isAppNative() {
if (typeof window.TBJS !== 'undefined') return true;
if (typeof window.AlipayJSBridge !== 'undefined') return true;
if (typeof window.dd !== 'undefined') return true;
if (typeof window.toutiao !== 'undefined') return true;
if (typeof window.JDBridge !== 'undefined') return true;
return false;
}
function _isBrowser() {
if (_isWechat()) return false;
if (_isAppWebview()) return false;
if (_isAppNative()) return false;
return /safari|chrome|crios|firefox|fxios|edge|edg|opr/.test(UA);
}
function _isMobile() {
return /iphone|ipad|ipod|android/.test(UA);
}
function _getAppName() {
var map = [
{ name: 'taobao', regex: /aliapp\(tb/i },
{ name: 'tmall', regex: /aliapp\(tm/i },
{ name: 'jd', regex: /jdapp/i },
{ name: 'douyin', regex: /aweme/i },
{ name: 'kuaishou', regex: /ksnc/i },
{ name: 'weibo', regex: /weibo/i },
{ name: 'dingtalk', regex: /dingtalk/i },
{ name: 'alipay', regex: /alipayclient/i },
{ name: 'baidu', regex: /baiduboxapp/i },
{ name: 'qq', regex: /qq\//i },
];
for (var i = 0; i < map.length; i++) {
if (map[i].regex.test(UA)) return map[i].name;
}
return null;
}
function _detect() {
if (_isWechatMiniProgram()) return 'wechat-miniprogram';
if (_isWechatH5()) return 'wechat-h5';
if (_isAppNative()) return 'app-native';
if (_isAppWebview()) return 'app-webview';
if (_isBrowser()) return _isMobile() ? 'mobile-browser' : 'pc-browser';
return 'unknown';
}
function _getEnvironment() {
var wechat = _isWechat();
var wechatMini = _isWechatMiniProgram();
var wechatH5Env = _isWechatH5();
var appWebview = _isAppWebview();
var appNative = _isAppNative();
var browser = _isBrowser();
return {
isApp: appNative,
isWechat: wechat,
isWechatH5: wechatH5Env,
isBrowser: browser,
isWechatWebview: wechatH5Env || wechatMini,
isAppWebview: appWebview,
isWechatMiniProgram: wechatMini,
isAppNative: appNative,
appName: _getAppName(),
isMobile: _isMobile(),
env: _detect()
};
}
// #endif
/* ========== 微信小程序 ========== */
// #ifdef MP-WEIXIN
function _getEnvironment() {
return {
isApp: false,
isWechat: true,
isWechatH5: false,
isBrowser: false,
isWechatWebview: true,
isAppWebview: false,
isWechatMiniProgram: true,
isAppNative: false,
appName: null,
isMobile: true,
env: 'wechat-miniprogram'
};
}
function _detect() { return 'wechat-miniprogram'; }
function _isWechatMiniProgram() { return true; }
function _isWechatH5() { return false; }
function _isWechat() { return true; }
function _isWechatWebview() { return true; }
function _isAppWebview() { return false; }
function _isAppNative() { return false; }
function _isBrowser() { return false; }
function _isMobile() { return true; }
function _getAppName() { return null; }
// #endif
/* ========== App 平台 ========== */
// #ifdef APP-PLUS
function _getEnvironment() {
return {
isApp: false,
isWechat: false,
isWechatH5: false,
isBrowser: false,
isWechatWebview: false,
isAppWebview: true,
isWechatMiniProgram: false,
isAppNative: false,
appName: null,
isMobile: true,
env: 'app-webview'
};
}
function _detect() { return 'app-webview'; }
function _isWechatMiniProgram() { return false; }
function _isWechatH5() { return false; }
function _isWechat() { return false; }
function _isWechatWebview() { return false; }
function _isAppWebview() { return true; }
function _isAppNative() { return false; }
function _isBrowser() { return false; }
function _isMobile() { return true; }
function _getAppName() { return null; }
// #endif
/* ========== 其他小程序平台(支付宝、百度、抖音等) ========== */
// #ifndef H5 || MP-WEIXIN || APP-PLUS
function _getEnvironment() {
return {
isApp: false,
isWechat: false,
isWechatH5: false,
isBrowser: false,
isWechatWebview: false,
isAppWebview: false,
isWechatMiniProgram: false,
isAppNative: false,
appName: null,
isMobile: true,
env: 'unknown'
};
}
function _detect() { return 'unknown'; }
function _isWechatMiniProgram() { return false; }
function _isWechatH5() { return false; }
function _isWechat() { return false; }
function _isWechatWebview() { return false; }
function _isAppWebview() { return false; }
function _isAppNative() { return false; }
function _isBrowser() { return false; }
function _isMobile() { return true; }
function _getAppName() { return null; }
// #endif
/* ========== 统一暴露接口 ========== */
export function getEnvironment() {
return _getEnvironment();
}
export function detect() {
return _detect();
}
export function isWechatMiniProgram() {
return _isWechatMiniProgram();
}
export function isWechatH5() {
return _isWechatH5();
}
export function isWechat() {
return _isWechat();
}
export function isWechatWebview() {
return _isWechatWebview();
}
export function isAppWebview() {
return _isAppWebview();
}
export function isAppNative() {
return _isAppNative();
}
export function isBrowser() {
return _isBrowser();
}
export function isMobile() {
return _isMobile();
}
export function getAppName() {
return _getAppName();
}
export default {
getEnvironment,
detect,
isWechatMiniProgram,
isWechatH5,
isWechat,
isWechatWebview,
isAppWebview,
isAppNative,
isBrowser,
isMobile,
getAppName
};