import bus from './bus' function padLeftZero(str: string): string { return ('00' + str).substr(str.length) } const util = { timetostr(d: number) { if (d < 60) { return d + '秒' } else if (d < 3600) { return (d / 60).toFixed(1) + '分钟' } else if (d < 86400) { return (d / 3600).toFixed(1) + '小时' } else if (d < 2592000) { return (d / 86400).toFixed(1) + '天' } }, datetostr(d: string) { if (!d.endsWith('Z')) { d = d + 'Z' } let r = new Date(d) let delta = (new Date().getTime() - r.getTime()) / 1000 if (delta < 0) { } else if (delta < 60) { return Math.floor(delta) + '秒前' } else if (delta < 3600) { return Math.floor(delta / 60) + '分钟前' } else if (delta < 86400) { return Math.floor(delta / 3600) + '小时前' } return r.toLocaleString() }, randomNum(minNum: number, maxNum: number) { return Math.floor(Math.random() * maxNum) + minNum }, goto(url: string) { window.open(url, '_blank') }, title: function(title: string) { window.document.title = title ? title + ' - oa' : 'veypi project' }, getCookie(name: string) { const reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)') const arr = document.cookie.match(reg) if (arr) { return unescape(arr[2]) } else return null }, delCookie(name: string) { const exp = new Date() exp.setTime(exp.getTime() - 1) const cval = this.getCookie(name) if (cval !== null) { document.cookie = name + '=' + cval + ';expires=' + exp.toLocaleString() } }, setCookie(name: string, value: string, time: number) { const exp = new Date() exp.setTime(exp.getTime() + time) document.cookie = name + '=' + escape(value) + ';expires=' + exp.toLocaleString() }, getToken() { return localStorage.getItem('token') || '' }, setToken(t: string) { localStorage.setItem('token', t) bus.emit('token', t) }, addTokenOf(url: string) { return url + '?token=' + encodeURIComponent(this.getToken()) }, checkLogin() { return Boolean(localStorage.token) }, formatDate(date: Date, fmt: string) { if (/(y+)/.test(fmt)) { fmt = fmt.replace( RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length), ) } const o = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds(), } for (const k in o) { if (new RegExp(`(${k})`).test(fmt)) { // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore const str = o[k] + '' fmt = fmt.replace( RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str), ) } } return fmt }, } export default util