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 ) } export function isIE () { const bw = window.navigator.userAgent const compare = (s) => bw.indexOf(s) >= 0 const ie11 = (() => 'ActiveXObject' in window)() return compare('MSIE') || ie11 } /** * 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 min 最小值 * @param max 最大值 * @return int 生成后的数字 */ export function randomNumber (min, max) { return Math.floor(Math.random() * (max - min + 1) + min) } /** * 随机生成字符串 * @param length 字符串的长度 * @param chats 可选字符串区间(只会生成传入的字符串中的字符) * @return string 生成的字符串 */ export function randomString (length, chats) { if (!length) length = 1 if (!chats) chats = '0123456789qwertyuioplkjhgfdsazxcvbnm' let str = '' for (let i = 0; i < length; i++) { const num = randomNumber(0, chats.length - 1) str += chats[num] } return str } /** * 随机生成uuid * @return string 生成的uuid */ export function randomUUID () { const chats = '0123456789abcdef' return randomString(32, chats) } /** * json对象单层,转换url参数 * @param {*} paramObj */ export function formateObjToParamStr (paramObj) { const sdata = [] for (const attr in paramObj) { sdata.push(`${attr} = ${filter(paramObj[attr])}`) } return sdata.join('&') } // 过滤特殊符号 function filter (str) { // 特殊字符转义 str += '' // 隐式转换 str = str.replace(/%/g, '%25') str = str.replace(/\+/g, '%2B') str = str.replace(/ /g, '%20') str = str.replace(/\//g, '%2F') str = str.replace(/\?/g, '%3F') str = str.replace(/&/g, '%26') str = str.replace(/=/g, '%3D') str = str.replace(/#/g, '%23') return str }