这是一个平台信息检测工具
Demo
@niu-tools/browser/check
getExplorerInfo
检测结果:-1getExplorerInfo
检测结果:这不是IEisHtml5Plus
检测结果:这不是html5Plus环境isWxMp
检测结果:这不是微信小程序isWeChat
检测结果:这不是微信浏览器isMobile
检测结果:这不是手机isWeCom
检测结果:这不是企业微信isAlipay
检测结果:这不是支付宝isDingTalk
检测结果:这不是钉钉<template>
<div>
<cop info="getExplorerInfo" :text="ExplorerInfo"></cop>
<cop info="getExplorerInfo" :text="IeInfo === -1 ? '这不是IE' : '这是IE' + IeInfo"></cop>
<cop info="isHtml5Plus" :text="html5PlusInfo ? '这是html5Plus环境' : '这不是html5Plus环境'"></cop>
<cop info="isWxMp" :text="wxMpInfo ? '这是微信小程序' : '这不是微信小程序'"></cop>
<cop info="isWeChat" :text="weChatInfo ? '这是微信浏览器' : '这不是微信浏览器'"></cop>
<cop info="isMobile" :text="mobileInfo ? '这是手机' : '这不是手机'"></cop>
<cop info="isWeCom" :text="isWeCom ? '这是企业微信' : '这不是企业微信'"></cop>
<cop info="isAlipay" :text="isAlipay ? '这是支付宝' : '这不是支付宝'"></cop>
<cop info="isDingTalk" :text="isDingTalk ? '这是钉钉' : '这不是钉钉'"></cop>
</div>
</template>
<script lang="ts" setup>
import { getExplorerInfo, IEVersion } from '@niu-tools/browser/check/ie'
import { isWeCom, isAlipay, isDingTalk, isHtml5Plus, isMobile, isWeChat, isWxMp } from '@niu-tools/browser/check'
import { defineComponent, h, ref } from 'vue'
const cop = defineComponent({
props: ['info', 'text'],
setup(props: any, ctx) {
return () => h('div', [
h('code', [props.info]),
h('span', ["检测结果:" + props.text]),
])
},
})
const ExplorerInfo = getExplorerInfo()
const IeInfo = IEVersion()
const html5PlusInfo = isHtml5Plus()
const mobileInfo = isMobile()
const weChatInfo = isWeChat()
const wxMpInfo = ref<boolean>();
;(async () => {
try {
await isWxMp()
wxMpInfo.value = true
} catch (error) {
wxMpInfo.value = false
}
})();
</script>
<style lang="less" scoped></style>
<template>
<div>
<cop info="getExplorerInfo" :text="ExplorerInfo"></cop>
<cop info="getExplorerInfo" :text="IeInfo === -1 ? '这不是IE' : '这是IE' + IeInfo"></cop>
<cop info="isHtml5Plus" :text="html5PlusInfo ? '这是html5Plus环境' : '这不是html5Plus环境'"></cop>
<cop info="isWxMp" :text="wxMpInfo ? '这是微信小程序' : '这不是微信小程序'"></cop>
<cop info="isWeChat" :text="weChatInfo ? '这是微信浏览器' : '这不是微信浏览器'"></cop>
<cop info="isMobile" :text="mobileInfo ? '这是手机' : '这不是手机'"></cop>
<cop info="isWeCom" :text="isWeCom ? '这是企业微信' : '这不是企业微信'"></cop>
<cop info="isAlipay" :text="isAlipay ? '这是支付宝' : '这不是支付宝'"></cop>
<cop info="isDingTalk" :text="isDingTalk ? '这是钉钉' : '这不是钉钉'"></cop>
</div>
</template>
<script lang="ts" setup>
import { getExplorerInfo, IEVersion } from '@niu-tools/browser/check/ie'
import { isWeCom, isAlipay, isDingTalk, isHtml5Plus, isMobile, isWeChat, isWxMp } from '@niu-tools/browser/check'
import { defineComponent, h, ref } from 'vue'
const cop = defineComponent({
props: ['info', 'text'],
setup(props: any, ctx) {
return () => h('div', [
h('code', [props.info]),
h('span', ["检测结果:" + props.text]),
])
},
})
const ExplorerInfo = getExplorerInfo()
const IeInfo = IEVersion()
const html5PlusInfo = isHtml5Plus()
const mobileInfo = isMobile()
const weChatInfo = isWeChat()
const wxMpInfo = ref<boolean>();
;(async () => {
try {
await isWxMp()
wxMpInfo.value = true
} catch (error) {
wxMpInfo.value = false
}
})();
</script>
<style lang="less" scoped></style>
源码
查看源码
check/client.ts源码
ts
// @ts-nocheck
var client = function () {
// 用户代理引擎
var engine = {
ie: 0,
gecko: 0,
webkit: 0,
khtml: 0,
opera: 0,
ver: null
};
//用户浏览器类型
var browser = {
ie: 0,
firefox: 0,
safari: 0,
konq: 0,
opera: 0,
chrome: 0,
ver: null
}
//用户平台、设备、操作系统
var system = {
win: false,
mac: false,
xll: false,
iphone: false,
ipod: false,
ipad: false,
ios: false,
android: false,
nokiaN: false,
winMobile: false,
wii: false,
ps: false,
};
/*
按顺序检测:opera,webkit,khtml,gecko,ie
*/
var ua = navigator.userAgent;
if (window.opera) {
engine.ver = browser.ver = window.opera.version();
engine.opera = browser.opera = parseFloat(engine.ver);
} else if (/AppleWebKit\/(\S+)/.test(ua)) { //S+表示非空格的特殊字符
engine.ver = RegExp["$1"]; //捕获正则表达式匹配的第一个字符串
engine.webkit = parseFloat(engine.ver);
//chrome or safari
if (/Chrome\/(\S+)/.test(ua)) {
browser.ver = RegExp["$1"];
browser.chrome = parseFloat(browser.ver);
} else if (/Version\/(\S+)/.test(ua)) {
browser.ver = RegExp["$1"];
browser.safari = parseFloat(browser.ver);
} else {
//近似确定版本
var safariVersion = 1;
if (engine.webkit < 100) {
safariVersion = 1;
} else if (engine.webkit < 312) {
safariVersion = 1.2;
} else if (engine.webkit < 412) {
safariVersion = 1.3;
} else {
safariVersion = 2;
}
}
} else if (/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)) {
engine.ver = browser.ver = RegExp["$1"];
engine.khtml = browser.konq = parseFloat(engine.ver);
} else if (/rv:([^\)]+))\) Gecko\/\d{8}/.test(ua)) {
engine.ver = RegExp["$1"];
engine.gecko = parseFloat(engine.ver);
//firefox?
if (/FireFox\/(\S+)/.test(ua)) {
browser.ver = RegExp["$1"];
browser.firefox = parseFloat(browser.ver);
}
} else if (/MSIE ([^;]+)/.test(ua)) {
engine.ver = browser.ver = RegExp["$1"];
engine.ie = browser.ie = parseFloat(engine.ver);
};
//检测平台
var plaform = navigator.plaform;
system.win = plaform.indexOf("Win") == 0;
system.mac = plaform.indexOf("Mac") == 0;
system.xll = (plaform.indexOf("Xll") == 0) || (plaform.indexOf("Linux") == 0);
//检测Windows操作系统
if (system.win) {
if (/WIn(?:dows)?([^do]{2})\s?(\d+\.\d+)?/.test(ua)) {
if (RegExp["$1"] == "NT") {
switch (RegExp["$2"]) {
case "5.0": {
system.win = "2000";
break;
}
case "5.1": {
system.win = "XP";
break;
}
case "6.0": {
system.win = "Vista";
break;
}
case "6.1": {
system.win = "7";
break;
}
default: {
system.win = "NT";
break;
}
}
} else if (RegExp["$1"] == "9x") {
system.win = "ME";
} else {
system.win = RegExp["$1"];
}
}
}
//移动设备
system.iphone = ua.indexOf("iPhone") > -1;
system.ipod = ua.indexOf("iPod") > -1;
system.ipad = ua.indexOf("iPad") > -1;
system.NokiaN = ua.indexOf("NokiaN") > -1;
//windowsMobile
if (system.win == "CE") {
system.winMobile = system.win;
} else if (system.win == "Ph") {
if (/Windows Phone OS (\d+.\d+)/.test(ua)) {
system.win = "Phone";
system.winMobile = parseFloat(RegExp["$1"]);
}
}
//检测ios版本
if (system.mac && ua.indexOf("Mobile") > -1) {
if (/CPU (?:iPhone )?OS (\d+_\d+)/.test(ua)) {
system.ios = parseFloat(RegExp.$1.replace("_", "."));
} else {
system.ios = 2;
}
}
//检测安卓版本
if (/Android (\d+\.+\d)/.test(ua)) {
system.android = parseFloat(RegExp.$1);
}
//游戏系统
system.wii = ua.indexOf("Wii") > -1;
system.ps = /playstation/i.test(ua);
return {
engine: engine,
browser: browser,
system: system
};
}();
export default client
// @ts-nocheck
var client = function () {
// 用户代理引擎
var engine = {
ie: 0,
gecko: 0,
webkit: 0,
khtml: 0,
opera: 0,
ver: null
};
//用户浏览器类型
var browser = {
ie: 0,
firefox: 0,
safari: 0,
konq: 0,
opera: 0,
chrome: 0,
ver: null
}
//用户平台、设备、操作系统
var system = {
win: false,
mac: false,
xll: false,
iphone: false,
ipod: false,
ipad: false,
ios: false,
android: false,
nokiaN: false,
winMobile: false,
wii: false,
ps: false,
};
/*
按顺序检测:opera,webkit,khtml,gecko,ie
*/
var ua = navigator.userAgent;
if (window.opera) {
engine.ver = browser.ver = window.opera.version();
engine.opera = browser.opera = parseFloat(engine.ver);
} else if (/AppleWebKit\/(\S+)/.test(ua)) { //S+表示非空格的特殊字符
engine.ver = RegExp["$1"]; //捕获正则表达式匹配的第一个字符串
engine.webkit = parseFloat(engine.ver);
//chrome or safari
if (/Chrome\/(\S+)/.test(ua)) {
browser.ver = RegExp["$1"];
browser.chrome = parseFloat(browser.ver);
} else if (/Version\/(\S+)/.test(ua)) {
browser.ver = RegExp["$1"];
browser.safari = parseFloat(browser.ver);
} else {
//近似确定版本
var safariVersion = 1;
if (engine.webkit < 100) {
safariVersion = 1;
} else if (engine.webkit < 312) {
safariVersion = 1.2;
} else if (engine.webkit < 412) {
safariVersion = 1.3;
} else {
safariVersion = 2;
}
}
} else if (/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)) {
engine.ver = browser.ver = RegExp["$1"];
engine.khtml = browser.konq = parseFloat(engine.ver);
} else if (/rv:([^\)]+))\) Gecko\/\d{8}/.test(ua)) {
engine.ver = RegExp["$1"];
engine.gecko = parseFloat(engine.ver);
//firefox?
if (/FireFox\/(\S+)/.test(ua)) {
browser.ver = RegExp["$1"];
browser.firefox = parseFloat(browser.ver);
}
} else if (/MSIE ([^;]+)/.test(ua)) {
engine.ver = browser.ver = RegExp["$1"];
engine.ie = browser.ie = parseFloat(engine.ver);
};
//检测平台
var plaform = navigator.plaform;
system.win = plaform.indexOf("Win") == 0;
system.mac = plaform.indexOf("Mac") == 0;
system.xll = (plaform.indexOf("Xll") == 0) || (plaform.indexOf("Linux") == 0);
//检测Windows操作系统
if (system.win) {
if (/WIn(?:dows)?([^do]{2})\s?(\d+\.\d+)?/.test(ua)) {
if (RegExp["$1"] == "NT") {
switch (RegExp["$2"]) {
case "5.0": {
system.win = "2000";
break;
}
case "5.1": {
system.win = "XP";
break;
}
case "6.0": {
system.win = "Vista";
break;
}
case "6.1": {
system.win = "7";
break;
}
default: {
system.win = "NT";
break;
}
}
} else if (RegExp["$1"] == "9x") {
system.win = "ME";
} else {
system.win = RegExp["$1"];
}
}
}
//移动设备
system.iphone = ua.indexOf("iPhone") > -1;
system.ipod = ua.indexOf("iPod") > -1;
system.ipad = ua.indexOf("iPad") > -1;
system.NokiaN = ua.indexOf("NokiaN") > -1;
//windowsMobile
if (system.win == "CE") {
system.winMobile = system.win;
} else if (system.win == "Ph") {
if (/Windows Phone OS (\d+.\d+)/.test(ua)) {
system.win = "Phone";
system.winMobile = parseFloat(RegExp["$1"]);
}
}
//检测ios版本
if (system.mac && ua.indexOf("Mobile") > -1) {
if (/CPU (?:iPhone )?OS (\d+_\d+)/.test(ua)) {
system.ios = parseFloat(RegExp.$1.replace("_", "."));
} else {
system.ios = 2;
}
}
//检测安卓版本
if (/Android (\d+\.+\d)/.test(ua)) {
system.android = parseFloat(RegExp.$1);
}
//游戏系统
system.wii = ua.indexOf("Wii") > -1;
system.ps = /playstation/i.test(ua);
return {
engine: engine,
browser: browser,
system: system
};
}();
export default client
check/ie.ts源码
ts
import { isClient } from "@niu-tools/shared/browser";
export function IEVersion() {
if(!isClient){
return -1
}
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
if (isIE) {
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if (fIEVersion == 7) {
return 7;
} else if (fIEVersion == 8) {
return 8;
} else if (fIEVersion == 9) {
return 9;
} else if (fIEVersion == 10) {
return 10;
} else {
return 6;//IE版本<=7
}
} else if (isEdge) {
return 'edge';//edge
} else if (isIE11) {
return 11; //IE11
} else {
return -1;//不是ie浏览器
}
}
export function getExplorerInfo() {
if(!isClient){
return -1
}
//判断浏览器版本
var userAgent = navigator.userAgent;
var info;
var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
var isEdge = userAgent.toLowerCase().indexOf("edge") > -1 && !isIE; //判断是否IE的Edge浏览器
var isIE11 = (userAgent.toLowerCase().indexOf("trident") > -1 && userAgent.indexOf("rv") > -1);
var tempArray: RegExpExecArray
if (/[Ff]irefox(\/\d+\.\d+)/.test(userAgent)) {
tempArray = /([Ff]irefox)\/(\d+\.\d+)/.exec(userAgent) as RegExpExecArray;
info = tempArray[1] + tempArray[2];
} else if (isIE) {
var version = "";
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if (fIEVersion == 7) { version = "IE7"; }
else if (fIEVersion == 8) { version = "IE8"; }
else if (fIEVersion == 9) { version = "IE9"; }
else if (fIEVersion == 10) { version = "IE10"; }
else { version = "0" }
info = version;
} else if (isEdge) {
info = "Edge";
} else if (isIE11) {
info = "IE11";
} else if (/[Cc]hrome\/\d+/.test(userAgent)) {
tempArray = /([Cc]hrome)\/(\d+)/.exec(userAgent) as RegExpExecArray;
info = tempArray[1] + tempArray[2];
} else if (/[Vv]ersion\/\d+\.\d+\.\d+(\.\d)* *[Ss]afari/.test(userAgent)) {
tempArray = /[Vv]ersion\/(\d+\.\d+\.\d+)(\.\d)* *([Ss]afari)/.exec(userAgent) as RegExpExecArray;
info = tempArray[3] + tempArray[1];
} else if (/[Oo]pera.+[Vv]ersion\/\d+\.\d+/.test(userAgent)) {
tempArray = /([Oo]pera).+[Vv]ersion\/(\d+)\.\d+/.exec(userAgent) as RegExpExecArray;
info = tempArray[1] + tempArray[2];
} else {
info = "unknown";
}
return info;
}
import { isClient } from "@niu-tools/shared/browser";
export function IEVersion() {
if(!isClient){
return -1
}
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
if (isIE) {
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if (fIEVersion == 7) {
return 7;
} else if (fIEVersion == 8) {
return 8;
} else if (fIEVersion == 9) {
return 9;
} else if (fIEVersion == 10) {
return 10;
} else {
return 6;//IE版本<=7
}
} else if (isEdge) {
return 'edge';//edge
} else if (isIE11) {
return 11; //IE11
} else {
return -1;//不是ie浏览器
}
}
export function getExplorerInfo() {
if(!isClient){
return -1
}
//判断浏览器版本
var userAgent = navigator.userAgent;
var info;
var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
var isEdge = userAgent.toLowerCase().indexOf("edge") > -1 && !isIE; //判断是否IE的Edge浏览器
var isIE11 = (userAgent.toLowerCase().indexOf("trident") > -1 && userAgent.indexOf("rv") > -1);
var tempArray: RegExpExecArray
if (/[Ff]irefox(\/\d+\.\d+)/.test(userAgent)) {
tempArray = /([Ff]irefox)\/(\d+\.\d+)/.exec(userAgent) as RegExpExecArray;
info = tempArray[1] + tempArray[2];
} else if (isIE) {
var version = "";
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if (fIEVersion == 7) { version = "IE7"; }
else if (fIEVersion == 8) { version = "IE8"; }
else if (fIEVersion == 9) { version = "IE9"; }
else if (fIEVersion == 10) { version = "IE10"; }
else { version = "0" }
info = version;
} else if (isEdge) {
info = "Edge";
} else if (isIE11) {
info = "IE11";
} else if (/[Cc]hrome\/\d+/.test(userAgent)) {
tempArray = /([Cc]hrome)\/(\d+)/.exec(userAgent) as RegExpExecArray;
info = tempArray[1] + tempArray[2];
} else if (/[Vv]ersion\/\d+\.\d+\.\d+(\.\d)* *[Ss]afari/.test(userAgent)) {
tempArray = /[Vv]ersion\/(\d+\.\d+\.\d+)(\.\d)* *([Ss]afari)/.exec(userAgent) as RegExpExecArray;
info = tempArray[3] + tempArray[1];
} else if (/[Oo]pera.+[Vv]ersion\/\d+\.\d+/.test(userAgent)) {
tempArray = /([Oo]pera).+[Vv]ersion\/(\d+)\.\d+/.exec(userAgent) as RegExpExecArray;
info = tempArray[1] + tempArray[2];
} else {
info = "unknown";
}
return info;
}
check/index.ts源码
ts
import { isClient } from "@niu-tools/shared/browser";
export const UA = isClient && navigator.userAgent.toLowerCase();
/** 是不是企业微信 */
export const isWeCom: boolean = isClient && Boolean(UA) && (UA as string).indexOf('wxwork') > 0
/** 是不是支付宝 */
export const isAlipay: boolean = isClient && Boolean(UA) && (UA as string).indexOf('alipay') > 0
/** 是不是钉钉 */
export const isDingTalk: boolean = isClient && Boolean(UA) && (UA as string).indexOf('dingtalk') > 0
/**
* 是否是微信浏览器
*/
export function isWeChat() {
var ua = isClient && navigator.userAgent.toLowerCase()
if (ua && ua.match(/MicroMessenger/i)?.toString() == 'micromessenger') {
return true
} else {
return false
}
}
export function isMobile() {
if(!isClient){
return false
}
let flag = navigator.userAgent.match(
/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
);
return flag;
}
export function isWxMp() {
return new Promise<number>((resolve, reject) => {
if (isWeChat()) {
wx.miniProgram.getEnv((res: any) => {
if (res.miniprogram) {
resolve(0)
} else {
reject(1)
}
})
} else {
reject(2)
}
})
}
export function isHtml5Plus() {
if (typeof plus === 'object') {
return true
} else {
return false
}
}
import { isClient } from "@niu-tools/shared/browser";
export const UA = isClient && navigator.userAgent.toLowerCase();
/** 是不是企业微信 */
export const isWeCom: boolean = isClient && Boolean(UA) && (UA as string).indexOf('wxwork') > 0
/** 是不是支付宝 */
export const isAlipay: boolean = isClient && Boolean(UA) && (UA as string).indexOf('alipay') > 0
/** 是不是钉钉 */
export const isDingTalk: boolean = isClient && Boolean(UA) && (UA as string).indexOf('dingtalk') > 0
/**
* 是否是微信浏览器
*/
export function isWeChat() {
var ua = isClient && navigator.userAgent.toLowerCase()
if (ua && ua.match(/MicroMessenger/i)?.toString() == 'micromessenger') {
return true
} else {
return false
}
}
export function isMobile() {
if(!isClient){
return false
}
let flag = navigator.userAgent.match(
/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
);
return flag;
}
export function isWxMp() {
return new Promise<number>((resolve, reject) => {
if (isWeChat()) {
wx.miniProgram.getEnv((res: any) => {
if (res.miniprogram) {
resolve(0)
} else {
reject(1)
}
})
} else {
reject(2)
}
})
}
export function isHtml5Plus() {
if (typeof plus === 'object') {
return true
} else {
return false
}
}