You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/oaweb/composables/util.ts

112 lines
2.9 KiB
TypeScript

1 year ago
import bus from './bus'
function padLeftZero(str: string): string {
1 year ago
return ('00' + str).substr(str.length)
}
1 year ago
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) + '天'
}
},
1 year ago
datetostr(d: string) {
if (!d.endsWith('Z')) {
d = d + 'Z'
}
let r = new Date(d)
1 year ago
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()
1 year ago
},
1 year ago
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('auth_token') || ''
},
setToken(t: string) {
localStorage.setItem('auth_token', t)
1 year ago
bus.emit('token', t)
1 year ago
},
addTokenOf(url: string) {
return url + '?auth_token=' + encodeURIComponent(this.getToken())
},
checkLogin() {
// return parseInt(this.getCookie('stat')) === 1
return Boolean(localStorage.auth_token)
},
1 year ago
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