index.js
//检测浏览器类型及版本
export function getBrowserVersion() {
let agent = navigator.userAgent.toLowerCase()
let obj = {}
let Browser = ''
let Bversion = ''
let verinNum = ''
//IE
if (agent.indexOf('msie') > 0) {
var regStr_ie = /msie [\d.]+;/gi
Browser = 'IE'
Bversion = '' + agent.match(regStr_ie)
}
//firefox
else if (agent.indexOf('firefox') > 0) {
var regStr_ff = /firefox\/[\d.]+/gi
Browser = 'firefox'
Bversion = '' + agent.match(regStr_ff)
}
//Chrome
else if (agent.indexOf('chrome') > 0) {
var regStr_chrome = /chrome\/[\d.]+/gi
Browser = 'chrome'
Bversion = '' + agent.match(regStr_chrome)
}
//Safari
else if (agent.indexOf('safari') > 0 && agent.indexOf('chrome') < 0) {
var regStr_saf = /version\/[\d.]+/gi
Browser = 'safari'
Bversion = '' + agent.match(regStr_saf)
}
//Opera
else if (agent.indexOf('opera') >= 0) {
var regStr_opera = /version\/[\d.]+/gi
Browser = 'opera'
Bversion = '' + agent.match(regStr_opera)
} else {
var browser = navigator.appName
if (browser == 'Netscape') {
var version = agent.split(';')
var trim_Version = version[7].replace(/[ ]/g, '')
var rvStr = trim_Version.match(/[\d\.]/g).toString()
var rv = rvStr.replace(/[,]/g, '')
Bversion = rv
Browser = 'IE'
}
}
verinNum = (Bversion + '').replace(/[^0-9.]/gi, '')
obj.Browser = Browser
obj.verinNum = verinNum
return obj
}
//判断浏览器类型
export function bw() {
var UserAgent = navigator.userAgent.toLowerCase()
return {
isIE6: /msie 6.0/.test(UserAgent), // IE6
isIE7: /msie 7.0/.test(UserAgent), // IE7
isIE8: /msie 8.0/.test(UserAgent), // IE8
isIE9: /msie 9.0/.test(UserAgent), // IE9
isIE10: /msie 10.0/.test(UserAgent), // IE10
isIE11: /msie 11.0/.test(UserAgent), // IE11
isLB: /lbbrowser/.test(UserAgent), // 猎豹浏览器
isUc: /ucweb/.test(UserAgent), // UC浏览器
is360: /360se/.test(UserAgent), // 360浏览器
isBaidu: /bidubrowser/.test(UserAgent), // 百度浏览器
isSougou: /metasr/.test(UserAgent), // 搜狗浏览器
isChrome: /chrome/.test(UserAgent.substr(-33, 6)), // Chrome浏览器
isFirefox: /firefox/.test(UserAgent), // 火狐浏览器
isOpera: /opera/.test(UserAgent), // Opera浏览器
isSafire: /safari/.test(UserAgent) && !/chrome/.test(UserAgent) // safire浏览器
}
}
export default utils
home.vue页面
import { getFlatArr, fontSize, message, throttle, getBrowserVersion } from '@/utils'
//根据选中的模块,调用服务获取该模块下的菜单数据
const selectSystemData = (data) => {
return new Promise(async (resolve) => {
const res = await http.getLoginResourceTree(data)
if (res && res.success) {
filtrate(res.data).then((retData) => {
resolve(retData)
})
} else {
message('error', res.info, 3000)
resolve(false)
}
})
}
//判断浏览器类型及版本
const { Browser, verinNum } = getBrowserVersion()
const num = verinNum.substring(0, verinNum.indexOf('.'))
//处理服务返回的数据
const filtrate = (menus) => {
console.log('浏览器:', Browser, '版本:' + num)
return new Promise((resolve, reject) => {
makeMenus(menus)
.then((data) => {
if (Browser == 'chrome' && num <= 72) {
setTimeout(() => {
if (data.length) {
const menu = getFlatArr(data)
store.dispatch('user/setMenusRouter', menu).then(() => {
initDynamicRouter()
resolve(true)
})
} else {
resolve(false)
}
}, 150)
} else {
if (data.length) {
const menu = getFlatArr(data)
store.dispatch('user/setMenusRouter', menu).then(() => {
initDynamicRouter()
resolve(true)
})
} else {
resolve(false)
}
}
})
.catch(function (reason) {
console.log('makeMenus失败回调catch:', reason)
})
}).catch((e) => {
console.log('filtrate Catch报错:', e)
})
}
//从返回数据中提取关键值,组成路由表需要的格式
const makeMenus = (menusData) => {
let newMenus = []
return new Promise((resolve, reject) => {
menusData.map(async (routeItem) => {
let item = {
path: routeItem.path,
name: routeItem.title,
meta: {
icon: routeItem.icon,
title: routeItem.name,
isLink: true,
isHide: false,
isKeepAlive: true
}
}
if (routeItem.children) {
item.redirect = routeItem.children[0].path
item.component = '/components/Template'
item.children = await makeMenus(routeItem.children)
} else {
item.component = routeItem.path
}
// if (routeItem.children) item.children = await makeMenus(routeItem.children)
newMenus.push(item)
})
resolve(newMenus)
}).catch((e) => {
console.log('makeMenus报错:', e)
})
}