UNPKG

@ace-util/core

Version:
167 lines 6.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.browserType = exports.BrowserType = exports.deviceType = exports.isHarmonyOS = exports.isAndroid = exports.isIOS = exports.isMoible = exports.isDouyin = exports.isFeiShu = exports.isDingTalk = exports.isAlipay = exports.isQQ = exports.isWeibo = exports.isWechatWork = exports.isWechat = void 0; function isWechat(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /MicroMessenger/i.test(ua) && !/wxwork/i.test(ua); } exports.isWechat = isWechat; function isWechatWork(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /wxwork/i.test(ua) && !/MicroMessenger/i.test(ua); } exports.isWechatWork = isWechatWork; function isWeibo(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /Weibo/i.test(ua); } exports.isWeibo = isWeibo; function isQQ(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /QQ/i.test(ua); } exports.isQQ = isQQ; function isAlipay(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /AlipayClient/i.test(ua); } exports.isAlipay = isAlipay; function isDingTalk(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /DingTalk/i.test(ua); } exports.isDingTalk = isDingTalk; function isFeiShu(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /Lark/i.test(ua); } exports.isFeiShu = isFeiShu; function isDouyin(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /aweme/i.test(ua); } exports.isDouyin = isDouyin; function isMoible(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /(HarmonyOS|Android|webOS|iPhone|iPod|tablet|BlackBerry|Mobile)/i.test(ua); } exports.isMoible = isMoible; function isIOS(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /iPhone|iPad|iPod|iOS/i.test(ua); } exports.isIOS = isIOS; function isAndroid(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /Android/i.test(ua); } exports.isAndroid = isAndroid; function isHarmonyOS(ua) { if (ua === void 0) { ua = navigator.userAgent; } return /HarmonyOS/i.test(ua); } exports.isHarmonyOS = isHarmonyOS; /** * 获取设备类型 * @param {*} ua ,就是userAgent * @returns  type: 设备类型 *          env: 访问环境(微信/微博/qq) *          masklayer: 就是给外部拿到判断是否显示遮罩层的,一些特殊环境要引导用户到外部去打开访问 */ function deviceType(ua) { if (ua === void 0) { ua = navigator.userAgent; } var env = isWechat(ua) ? 'wechat' : isWechatWork(ua) ? 'wxwork' : isWeibo(ua) ? 'weibo' : isQQ(ua) ? 'qq' : isAlipay(ua) ? 'alipay' : isDingTalk(ua) ? 'dingtalk' : isFeiShu(ua) ? 'feishu' : isDouyin(ua) ? 'douyin' : undefined; if (isMoible(ua)) { return { type: isIOS(ua) ? 'ios' : isAndroid(ua) ? 'android' : isHarmonyOS(ua) ? 'harmonyos' : 'mobile', env: env, masklayer: !!env, }; } else { return { type: 'pc', env: env, // 部分应用没有桌面版 }; } } exports.deviceType = deviceType; var BrowserType; (function (BrowserType) { BrowserType["IE"] = "ie"; BrowserType["Edge"] = "edge"; BrowserType["Firefox"] = "firefox"; BrowserType["Chrome"] = "chrome"; BrowserType["Opera"] = "opera"; BrowserType["Safari"] = "safari"; })(BrowserType = exports.BrowserType || (exports.BrowserType = {})); /** * 获取浏览器类型 * @param {*} ua ,就是userAgent * @returns  type: 浏览器类型 *          version: 浏览器版本 */ function browserType(ua) { if (ua === void 0) { ua = navigator.userAgent; } var ieTridents, trident, matchStr, ieAerRv, browserChiType; //判断IE 浏览器, //blog: http://blog.csdn.Net/aerchi/article/details/51697592 if ('ActiveXObject' in self) { // ie_aer_rv: 指示IE 的版本. // It can be affected by the current document mode of IE. ieAerRv = (matchStr = ua.match(/msie ([\d.]+)/i)) ? matchStr[1] : (matchStr = ua.match(/rv:([\d.]+)/i)) ? matchStr[1] : 0; // ie: Indicate the really version of current IE browser. ieTridents = { 'trident/7.0': 11, 'trident/6.0': 10, 'trident/5.0': 9, 'trident/4.0': 8 }; //匹配 ie8, ie11, edge trident = (matchStr = ua.match(/(trident\/[\d.]+|edge\/[\d.]+)/i)) ? matchStr[1] : undefined; browserChiType = trident && (ieTridents[trident] || ieAerRv) > 0 ? BrowserType.IE : undefined; } else { //判断 windows edge 浏览器 // match_str[1]: 返回浏览器及版本号,如: "edge/13.10586" // match_str[1]: 返回版本号,如: "edge" //若要返回 "edge" 请把下行的 "ie" 换成 "edge"。 注意引号及冒号是英文状态下输入的 browserChiType = (matchStr = ua.match(/edge\/([\d.]+)/i)) ? BrowserType.Edge : //判断firefox 浏览器 (matchStr = ua.match(/firefox\/([\d.]+)/i)) ? BrowserType.Firefox : //判断chrome 浏览器 (matchStr = ua.match(/chrome\/([\d.]+)/i)) ? BrowserType.Chrome : //判断opera 浏览器 (matchStr = ua.match(/opera.([\d.]+)/i)) ? BrowserType.Opera : //判断safari 浏览器 (matchStr = ua.match(/version\/([\d.]+).*safari/i)) ? BrowserType.Safari : undefined; } var version = trident && (ieTridents === null || ieTridents === void 0 ? void 0 : ieTridents[trident]) ? String(ieTridents[trident]) : matchStr === null || matchStr === void 0 ? void 0 : matchStr[1]; return { type: browserChiType, version: version, }; } exports.browserType = browserType; //# sourceMappingURL=device-type.js.map