mirror of
https://gh-proxy.org/https://github.com/klinsmaya/SQLBot.git
synced 2026-11-08 01:06:59 +08:00
106 lines
2.7 KiB
TypeScript
106 lines
2.7 KiB
TypeScript
import dayjs from 'dayjs'
|
|
import { useCache } from '@/utils/useCache'
|
|
const { wsCache } = useCache()
|
|
const getCheckDate = (timestamp: any) => {
|
|
if (!timestamp) return false
|
|
const dt = new Date(timestamp)
|
|
if (isNaN(dt.getTime())) return false
|
|
return dt
|
|
}
|
|
|
|
export const datetimeFormat = (timestamp: any) => {
|
|
const dt = getCheckDate(timestamp)
|
|
if (!dt) return timestamp
|
|
|
|
const y = dt.getFullYear()
|
|
const m = (dt.getMonth() + 1 + '').padStart(2, '0')
|
|
const d = (dt.getDate() + '').padStart(2, '0')
|
|
const hh = (dt.getHours() + '').padStart(2, '0')
|
|
const mm = (dt.getMinutes() + '').padStart(2, '0')
|
|
const ss = (dt.getSeconds() + '').padStart(2, '0')
|
|
|
|
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
|
|
}
|
|
|
|
/**
|
|
*
|
|
* string: only accept ISO 8601, example: '2018-04-04T16:00:00.000Z'
|
|
* number: timestamp
|
|
* @param time
|
|
*/
|
|
export function getDate(time?: Date | string | number) {
|
|
if (!time) return undefined
|
|
if (time instanceof Date) return time
|
|
if (typeof time === 'string') {
|
|
return dayjs(time).toDate()
|
|
}
|
|
return new Date(time)
|
|
}
|
|
|
|
export const getBrowserLocale = () => {
|
|
const language = navigator.language
|
|
if (!language) {
|
|
return 'zh-CN'
|
|
}
|
|
if (language.startsWith('en')) {
|
|
return 'en'
|
|
}
|
|
if (language.toLowerCase().startsWith('zh')) {
|
|
const temp = language.toLowerCase().replace('_', '-')
|
|
return temp === 'zh' ? 'zh-CN' : temp === 'zh-cn' ? 'zh-CN' : 'tw'
|
|
}
|
|
return language
|
|
}
|
|
export const getLocale = () => {
|
|
return wsCache.get('user.language') || getBrowserLocale() || 'zh-CN'
|
|
}
|
|
|
|
export const setSize = (size: any) => {
|
|
let data = ''
|
|
const _size = Number.parseFloat(size)
|
|
if (_size < 1 * 1024) {
|
|
//如果小于0.1KB转化成B
|
|
data = _size.toFixed(2) + 'B'
|
|
} else if (_size < 1 * 1024 * 1024) {
|
|
//如果小于0.1MB转化成KB
|
|
data = (_size / 1024).toFixed(2) + 'KB'
|
|
} else if (_size < 1 * 1024 * 1024 * 1024) {
|
|
//如果小于0.1GB转化成MB
|
|
data = (_size / (1024 * 1024)).toFixed(2) + 'MB'
|
|
} else {
|
|
//其他转化成GB
|
|
data = (_size / (1024 * 1024 * 1024)).toFixed(2) + 'GB'
|
|
}
|
|
const size_str = data + ''
|
|
const len = size_str.indexOf('.')
|
|
const dec = size_str.substr(len + 1, 2)
|
|
if (dec == '00') {
|
|
//当小数点后为00时 去掉小数部分
|
|
return size_str.substring(0, len) + size_str.substr(len + 3, 2)
|
|
}
|
|
return size_str
|
|
}
|
|
|
|
export const isInIframe = () => {
|
|
try {
|
|
return window.top !== window.self
|
|
} catch (error) {
|
|
console.error(error)
|
|
return true
|
|
}
|
|
}
|
|
|
|
export const isBtnShow = (val: string) => {
|
|
if (!val || val === '0') {
|
|
return true
|
|
} else if (val === '1') {
|
|
return false
|
|
} else {
|
|
return !isInIframe()
|
|
}
|
|
}
|
|
|
|
export const setTitle = (title?: string) => {
|
|
document.title = title || 'SQLBot'
|
|
}
|