/**
* Created For: 根据访问浏览器 用户代理获取 访问设备、操作系统、浏览器等信息
* Author: chenLiYan.
* Date: 2019/5/9 0009 18:13.
*/
function getDeviceInfo () {
// 第一步判断访问设备是PC还是手机端(ios/Android)
let userAgent = navigator.userAgent.toLowerCase()
let browserInfo = getBrowserInfo()
let result = {
channel: undefined,
system: undefined,
browser: browserInfo.browserType + ' ' + browserInfo.browserVer
}
if (/(iphone|ios)/i.test(userAgent)) {
result.channel = '手机'
result.system = 'ios'
} else if (/(ipad)/i.test(userAgent)) {
result.channel = 'iPad'
result.system = 'iPad'
} else if (/(ipod)/i.test(userAgent)) {
result.channel = 'iPod'
result.system = 'iPod'
} else if (/(Android)/i.test(navigator.userAgent)) {
result.channel = '手机'
result.system = 'Android'
} else {
result.channel = 'PC'
result.system = detectOS()
}
return result
}
// 判断操作系统
function detectOS () {
let sUserAgent = navigator.userAgent
let isWin = (navigator.platform === 'Win32') || (navigator.platform === 'Windows')
let isMac = (navigator.platform === 'Mac68K') || (navigator.platform === 'MacPPC') || (navigator.platform === 'Macintosh') || (navigator.platform === 'MacIntel')
if (isMac) return 'Mac'
let isUnix = (navigator.platform === 'X11') && !isWin && !isMac
if (isUnix) return 'Unix'
let isLinux = (String(navigator.platform).indexOf('Linux') > -1)
if (isLinux) return 'Linux'
if (isWin) {
let isWin2K = sUserAgent.indexOf('Windows NT 5.0') > -1 || sUserAgent.indexOf('Windows 2000') > -1
if (isWin2K) return 'Windows 2000'
let isWinXP = sUserAgent.indexOf('Windows NT 5.1') > -1 || sUserAgent.indexOf('Windows XP') > -1
if (isWinXP) return 'Windows XP'
let isWin2003 = sUserAgent.indexOf('Windows NT 5.2') > -1 || sUserAgent.indexOf('Windows 2003') > -1
if (isWin2003) return 'Windows 2003'
let isWinVista = sUserAgent.indexOf('Windows NT 6.0') > -1 || sUserAgent.indexOf('Windows Vista') > -1
if (isWinVista) return 'Windows Vista'
let isWin7 = sUserAgent.indexOf('Windows NT 6.1') > -1 || sUserAgent.indexOf('Windows 7') > -1
if (isWin7) return 'Windows 7'
let isWin10 = sUserAgent.indexOf('Windows NT 10.0') > -1 || sUserAgent.indexOf('Windows 10') > -1
if (isWin10) return 'Windows 10'
}
return 'other'
}
// 获取当前浏览器类型及版本
function getBrowserInfo () {
let ua = navigator.userAgent.toLocaleLowerCase()
let browserType = null
let browserVer = ''
let re = /(msie|firefox|chrome|opera|netscape|version).*?([\d.]+)/i
let m = ua.match(re)
if (m && m[2]) {
browserVer = m[2]
}
if (/micromessenger/i.test(ua)) {
browserType = '微信浏览器'
browserVer = ua.match(/micromessenger\/([\d.]+)/i)[1]
} else if (/dingtalk/i.test(ua)) {
browserType = '钉钉'
browserVer = ua.match(/dingtalk\/([\d.]+)/i)[1]
} else if (ua.match(/firefox/) !== null) {
browserType = '火狐'
} else if (ua.match(/ubrowser/) !== null) {
browserType = 'UC'
browserVer = ua.match(/ubrowser\/([\d.]+)/i)[1]
} else if (ua.match(/opera/) !== null) {
browserType = '欧朋'
} else if (ua.match(/bidubrowser/) !== null) {
browserType = '百度'
browserVer = ua.match(/bidubrowser\/([\d.]+)/i)[1]
} else if (ua.match(/metasr/) !== null || ua.match(/se 2.x/) !== null) {
browserType = '搜狗'
browserVer = ''
} else if (ua.match(/tencenttraveler/) !== null || ua.match(/qqbrowser/) !== null) {
browserType = 'QQ'
browserVer = ua.match(/tencenttraveler\/([\d.]+)/i) !== null ? ua.match(/tencenttraveler\/([\d.]+)/i)[1] : (ua.match(/qqbrowser\/([\d.]+)/i) !== null ? ua.match(/qqbrowser\/([\d.]+)/i)[1] : '')
} else if (ua.match(/maxthon/) !== null) {
browserType = '遨游'
browserVer = ua.match(/maxthon\/([\d.]+)/i)[1]
} else if (ua.match(/lbbrowser/) !== null) {
browserType = '猎豹'
browserVer = ua.match(/lbbrowser\/([\d.]+)/i)[1]
} else if (ua.match(/oppobrowser/i) !== null) {
browserType = 'oppo 浏览器'
browserVer = ua.match(/oppobrowser\/([\d.]+)/i)[1]
} else if (ua.match(/XiaoMi\/MiuiBrowser/i) !== null) {
browserType = '小米 浏览器'
browserVer = ua.match(/MiuiBrowser\/([\d.]+)/i)[1]
} else if (ua.match(/FxiOS/i) !== null) {
browserType = '火狐'
browserVer = ua.match(/FxiOS\/([\d.]+)/i)[1]
} else if (ua.match(/OPR/i) !== null) {
browserType = 'Opera'
browserVer = ua.match(/OPR\/([\d.]+)/i)[1]
} else if (ua.match(/chrome/) !== null) {
let is360 = _mime('type', 'application/vnd.chromium.remoting-viewer')
if (is360) {
browserType = '360'
browserVer = ''
} else {
browserType = 'chrome'
}
} else if (!!window.ActiveXObject || 'ActiveXObject' in window) {
browserType = 'IE'
if (!ua.match(/msie/) && ua.match(/trident/) !== null) {
browserVer = '11+'
}
} else if (ua.match(/safari/) !== null) {
browserType = 'Safari'
browserVer = ua.match(/safari\/([\d.]+)/i)[1]
} else {
browserType = '其他'
browserVer = ''
}
return {
browserType: browserType,
browserVer: browserVer
}
}
// 判断是否是360浏览器
function _mime (option, value) {
let mimeTypes = navigator.mimeTypes
for (let mt in mimeTypes) {
if (mimeTypes[mt][option] === value) {
return true
}
}
return false
}
export default getDeviceInfo
// 查询字符串转对象
export function param2Obj (url) {
const search = url.split('?')[1]
if (!search) {
return {}
}
return JSON.parse(
'{"' +
decodeURIComponent(search)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"') +
'"}'
)
}