// 获取客户端操作系统信息 export const getUserOsInfo = function () { const userAgent = navigator.userAgent; if (userAgent.indexOf("Windows NT 10.0") !== -1) return "Windows 10"; if (userAgent.indexOf("Windows NT 6.2") !== -1) return "Windows 8"; if (userAgent.indexOf("Windows NT 6.1") !== -1) return "Windows 7"; if (userAgent.indexOf("Windows NT 6.0") !== -1) return "Windows Vista"; if (userAgent.indexOf("Windows NT 5.1") !== -1) return "Windows XP"; if (userAgent.indexOf("Windows NT 5.0") !== -1) return "Windows 2000"; if (userAgent.indexOf("Mac") !== -1) return "Mac/iOS"; if (userAgent.indexOf("X11") !== -1) return "UNIX"; if (userAgent.indexOf("Linux") !== -1) return "Linux"; return "Other"; } // 获取浏览器类型 export const _mime = function (option, value) { var mimeTypes = navigator.mimeTypes; for (var mt in mimeTypes) { if (mimeTypes[mt][option] == value) { return true; } } return false; } export const getBrowserType = function () { let ua = navigator.userAgent.toLocaleLowerCase() let browserType = null if (ua.match(/msie/) != null || ua.match(/trident/) != null) { browserType = 'IE' } else if (ua.match(/firefox/) != null) { browserType = 'firefox' } else if (ua.match(/ucbrowser/) != null) { browserType = 'UC' } else if (ua.match(/opera/) != null || ua.match(/opr/) != null) { browserType = 'opera' } else if (ua.match(/bidubrowser/) != null) { browserType = 'baidu' } else if (ua.match(/metasr/) != null) { browserType = 'sougou' } else if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) { browserType = 'QQ' } else if (ua.match(/maxthon/) != null) { browserType = 'maxthon' } else if (ua.match(/chrome/) != null) { var is360 = _mime('type', 'application/vnd.chromium.remoting-viewer') if (is360) { browserType = '360' } else { browserType = 'chrome' } } else if (ua.match(/safari/) != null) { browserType = 'Safari' } else { browserType = 'others' } return browserType } export function timeFix () { const time = new Date() const hour = time.getHours() return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好' } export function welcome () { const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了'] const index = Math.floor(Math.random() * arr.length) return arr[index] } /** * 触发 window.resize */ export function triggerWindowResizeEvent () { const event = document.createEvent('HTMLEvents') event.initEvent('resize', true, true) event.eventType = 'message' window.dispatchEvent(event) } export function handleScrollHeader (callback) { let timer = 0 let beforeScrollTop = window.pageYOffset callback = callback || function () {} window.addEventListener( 'scroll', event => { clearTimeout(timer) timer = setTimeout(() => { let direction = 'up' const afterScrollTop = window.pageYOffset const delta = afterScrollTop - beforeScrollTop if (delta === 0) { return false } direction = delta > 0 ? 'down' : 'up' callback(direction) beforeScrollTop = afterScrollTop }, 50) }, false ) } /** * 数组转树形结构 * @param {array} list 被转换的数组 * @param {number|string} root 根节点(最外层节点)的 id * @return array */ export function arrayToTree (list, root) { const result = [] // 用于存放结果 const map = {} // 用于存放 list 下的节点 // 1. 遍历 list,将 list 下的所有节点以 id 作为索引存入 map for (const item of list) { map[item.id] = { ...item } // 浅拷贝 } // 2. 再次遍历,将根节点放入最外层,子节点放入父节点 for (const item of list) { // 3. 获取节点的 id 和 父 id const { id, parentId } = item // ES6 解构赋值 // 4. 如果是根节点,存入 result if (item.parentId == root) { result.push(map[id]) } else { // 5. 反之,存入到父节点 map[parentId].children ? map[parentId].children.push(map[id]) : (map[parentId].children = [map[id]]) } } // 将结果返回 return result } /** * Remove loading animate * @param id parent element id or class * @param timeout */ export function removeLoadingAnimate (id = '', timeout = 1500) { if (id === '') { return } setTimeout(() => { document.body.removeChild(document.getElementById(id)) }, timeout) } /** * @param {String|Number} value 要验证的字符串或数值 * @param {*} validList 用来验证的列表 */ export function oneOf (value, validList) { for (let i = 0; i < validList.length; i++) { if (value === validList[i]) { return true } } return false } // 选择文本 export function onTextSelected ($copyText, $message) { const textDiv = document.getElementsByClassName('copy-rect') const selection = window.getSelection() if (!selection) { return } if (selection.rangeCount <= 0) { return } const range = selection.getRangeAt(0) if (!range) { return } const rect = range.getBoundingClientRect() if (!rect) { return } if (rect.width <= 0) { return } const wholeText = range.startContainer.wholeText if (!wholeText) { return } if (wholeText && wholeText.length <= 0) { return } const str = wholeText.slice(range.startOffset, range.endOffset) if (!str) { return } let copyNode = textDiv[0] if (!copyNode) { if (str.length > 0) { copyNode = document.createElement('div') copyNode.innerHTML = '复制' copyNode.className = 'copy-rect' copyNode.style.position = 'fixed' copyNode.style.top = rect.top - rect.height - 10 + 'px' copyNode.style.left = rect.left + rect.width - 20 + 'px' copyNode.style.height = 'auto' copyNode.style.width = 'auto' document.body.appendChild(copyNode) copyNode.addEventListener('mousedown', (event) => { console.log(event) event.stopPropagation() console.log(str) $copyText(str).then(message => { console.log('copy', message) $message.success('复制完毕') document.body.removeChild(copyNode) }).catch(err => { console.log('copy.err', err) $message.error('复制失败') document.body.removeChild(copyNode) }) }) } } }