Files
admin-govern/src/utils/common.ts

131 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-12-21 16:42:39 +08:00
import type { App } from 'vue'
import { adminBaseRoutePath } from '@/router/static'
import router from '@/router/index'
import { trimStart } from 'lodash-es'
import * as elIcons from '@element-plus/icons-vue'
import Icon from '@/components/icon/index.vue'
export function registerIcons(app: App) {
/*
* Icon
* 使: <Icon name="name" size="size" color="color" />
* <待完善>
*/
app.component('Icon', Icon)
/*
* element Plus的icon
*/
const icons = elIcons as any
for (const i in icons) {
app.component(`el-icon-${icons[i].name}`, icons[i])
}
}
/**
*
* @param path path
*/
export const isAdminApp = (path = '') => {
const regex = new RegExp(`^${adminBaseRoutePath}`)
if (path) {
return regex.test(path)
}
if (regex.test(getCurrentRoutePath())) {
return true
}
return false
}
/**
* path
*/
export const getCurrentRoutePath = () => {
let path = router.currentRoute.value.path
if (path == '/') path = trimStart(window.location.hash, '#')
if (path.indexOf('?') !== -1) path = path.replace(/\?.*/, '')
return path
}
/**
*
* @param relativeUrl
* @param domain
*/
export const fullUrl = (relativeUrl: string, domain = '') => {
return domain + relativeUrl
}
/**
*
* @param path
*/
export function isExternal(path: string): boolean {
return /^(https?|ftp|mailto|tel):/.test(path)
}
2023-12-22 16:19:33 +08:00
/**
*
* _.debounce
* @param fn
* @param ms
*/
export const debounce = (fn: Function, ms: number) => {
return (...args: any[]) => {
if (window.lazy) {
clearTimeout(window.lazy)
}
window.lazy = window.setTimeout(() => {
fn(...args)
}, ms)
}
}
/**
*
*/
const padStart = (str: string, maxLength: number, fillString = ' ') => {
if (str.length >= maxLength) return str
const fillLength = maxLength - str.length
let times = Math.ceil(fillLength / fillString.length)
while ((times >>= 1)) {
fillString += fillString
if (times === 1) {
fillString += fillString
}
}
return fillString.slice(0, fillLength) + str
}
/**
*
* @param dateTime
* @param fmt yyyy-mm-dd hh:MM:ss
*/
export const timeFormat = (dateTime: string | number | null = null, fmt = 'yyyy-mm-dd hh:MM:ss') => {
if (dateTime == 'none') return '-'
if (!dateTime) dateTime = Number(new Date())
if (dateTime.toString().length === 10) {
dateTime = +dateTime * 1000
}
const date = new Date(dateTime)
let ret
const opt: anyObj = {
'y+': date.getFullYear().toString(), // 年
'm+': (date.getMonth() + 1).toString(), // 月
'd+': date.getDate().toString(), // 日
'h+': date.getHours().toString(), // 时
'M+': date.getMinutes().toString(), // 分
's+': date.getSeconds().toString() // 秒
}
for (const k in opt) {
ret = new RegExp('(' + k + ')').exec(fmt)
if (ret) {
fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : padStart(opt[k], ret[1].length, '0'))
}
}
return fmt
}