class Util {
public delArrObjProperties(srcObjArr: any, property: any) {
return srcObjArr.map((el: any) => {
if (
el[property].length === 0 ||
el[property] === '' ||
Object.keys(el[property]).length === 0 ||
el[property]?.length === 0
) {
delete el[property]
}
return el
})
}
public convertToBoolean = (envName: any) => {
return envName === 'true' ? true : envName === 'false' ? false : envName
}
public findParentNode(data: any, id: any): any {
let result
if (!data) {
return
}
for (let i = 0; i < data.children.length; i++) {
const item = data.children[i]
if (item.id == id) {
result = data
return result
} else if (item.children && item.children.length > 0) {
result = this.findParentNode(item, id)
if (result) {
return result
}
}
}
return result
}
public findChildrenNode(data: any, id: any): any {
for (let i = 0; i < data.length; i++) {
const el = data[i]
if (el.id === id) {
return el?.children
} else {
if ('children' in data) {
this.findChildrenNode(el?.children, id)
}
}
}
}
public tableDataTransfer(data: any, toTable?: boolean) {
let tableData: any = {}
let flag = false
if (arguments.length < 2) {
if (data.data) {
flag = true
} else {
flag = false
}
} else {
if (toTable) {
flag = true
} else {
flag = false
}
}
if (flag) {
tableData = {
data: data.data?.content || [],
success: data.status === 'success',
total: data.data?.totalElements,
}
} else {
const pagination = {
size: data.pageSize || 10,
page: (data.current || 1) - 1,
}
tableData = { ...data, ...pagination }
delete tableData.pageSize
delete tableData.current
}
return tableData
}
public setObjToUrlParams(baseUrl: string, obj: any): string {
let parameters = ''
for (const key in obj) {
parameters += key + '=' + encodeURIComponent(obj[key]) + '&'
}
parameters = parameters.replace(/&$/, '')
return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters
}
public deepMerge<T = any>(src: any = {}, target: any = {}): T {
let key: string
for (key in target) {
src[key] = this.isObject(src[key])
? this.deepMerge(src[key], target[key])
: (src[key] = target[key])
}
return src
}
public withInstall = <T>(component: T, alias?: string) => {
const comp = component as any
comp.install = (app: any) => {
app.component(comp.name || comp.displayName, component)
if (alias) {
app.config.globalProperties[alias] = component
}
}
return component as T
}
public isObject(val: any): val is Record<any, any> {
return val !== null && this.is(val, 'Object')
}
public is(val: unknown, type: string) {
return toString.call(val) === `[object ${type}]`
}
public formatDate(dateTime?: any, format?: string = "YY-MM-DD hh:mm:ss" ) {
let time = dateTime ? dateTime : new Date();
let date;
if (typeof time === "object") {
date = time;
} else {
if (typeof time === "string" && /^[0-9]+$/.test(time)) {
time = parseInt(time);
} else if (typeof time === "string") {
time = time
.replace(new RegExp(/-/gm), "/")
.replace("T", " ")
.replace(new RegExp(/\.[\d]{3}/gm), "");
}
if (typeof time === "number" && time.toString().length === 10) {
time = time * 1000;
}
date = new Date(time);
}
const formatObj = {
YYYY: date.getFullYear(),
MM: date.getMonth() + 1,
DD: date.getDate(),
HH: date.getHours(),
mm: date.getMinutes(),
ss: date.getSeconds(),
W: date.getDay(),
};
const time_str = format.replace(/YYYY|MM|DD|hh|mm|ss|W/g, (key, result) => {
let value = formatObj[key];
if (key === "W") {
return "星期" + ["日", "一", "二", "三", "四", "五", "六"][value];
}
if (result.length > 0 && value < 10) {
value = "0" + value;
}
return value || 0;
});
return time_str;
}
public findIntersection(arr1: any, arr2: any) {
const intersect: any = new Set([...arr1].filter((x) => arr2.includes(x)))
if (intersect.size > 0) return true
return false
}
public setDefaultValue(sourceData: any, defaultVal?: any, tofix?: number) {
let data = defaultVal
if (tofix && sourceData) {
data =
sourceData || sourceData === 0
? sourceData.toFixed(tofix)
: arguments.length > 1
? defaultVal
: '--'
} else {
data = sourceData || sourceData === 0 ? sourceData : arguments.length > 1 ? defaultVal : '--'
}
return data
}
public transformEmptyData(sourceData: any, defaultVal?: any) {
if (sourceData) {
Object.keys(sourceData).map((key) => {
if (
sourceData[key] === null ||
sourceData[key] === '' ||
sourceData[key] === undefined ||
sourceData[key] === NaN
) {
sourceData[key] = defaultVal || '--'
}
})
}
return sourceData
}
public finalDebounce(fn: any, delay: number, immediate: boolean) {
let timer: any = null
const debounced: any = () => {
if (timer) clearTimeout(timer)
if (immediate) {
const callNow = !timer
timer = setTimeout(() => {
timer = null
}, delay || 500)
if (callNow) fn.call()
} else {
timer = setTimeout(() => {
fn.call()
}, delay || 500)
}
}
debounced.cancel = () => {
clearTimeout(timer)
timer = null
}
return debounced
}
public fileDownload(url: any, download = true) {
function getFileName(_url: string) {
const name = _url.split('/')
return name.pop()
}
const filename: any = getFileName(url)
fetch(url, {
method: 'GET',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/octet-stream',
},
mode: 'no-cors',
})
.then((response) => {
return response.blob()
})
.then((blob) => {
const blobUrl = window.URL.createObjectURL(blob)
const tempLink = document.createElement('a')
tempLink.style.display = 'none'
tempLink.href = blobUrl
if (download) {
tempLink.setAttribute('download', filename)
} else {
tempLink.setAttribute('target', '_blank')
}
document.body.appendChild(tempLink)
tempLink.click()
setTimeout(() => {
URL.revokeObjectURL(blobUrl)
document.body.removeChild(tempLink)
})
})
.catch()
}
}
export default new Util()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2021-09-09 hbuilder检测不到夜神模拟器
2020-09-09 JavaScript——for循环
2020-09-09 迭代器
2020-09-09 DOM外联样式相关操作