低代码平台使用的一些好用的js方法
# 例子
import dayjs from "dayjs"
import { removeConfigLayout } from "@/utils/cache/local-storage"
/** 格式化时间 */
export const formatDateHfm = (time: string | number | Date) => {
return time ? dayjs(new Date(time)).format("HH:mm:ss") : ""
}
/** 格式化时间 */
export const formatDateTime = (time: string | number | Date, isShow = false) => {
return time ? dayjs(new Date(time)).format("YYYY-MM-DD HH:mm:ss") : isShow ? "N/A" : ""
}
/** 格式化时间 */
export const formatDateYMD = (time: string | number | Date, isShow = false) => {
return time ? dayjs(new Date(time)).format("YYYY-MM-DD") : isShow ? "N/A" : ""
}
export const formatDateYMDHm = (time: string | number | Date) => {
return time ? dayjs(new Date(time)).format("YYYY-MM-DD HH:mm") : ""
}
// 过滤空值
export function dealObjectValue(obj) {
const param = {}
if (obj === null || obj === undefined || obj === "") return param
for (const key in obj) {
if (obj[key] !== null && obj[key] !== undefined && obj[key] !== "") {
param[key] = obj[key]
}
}
return param
}
/** 用 JS 获取全局 css 变量 */
export const getCssVariableValue = (cssVariableName: string) => {
let cssVariableValue = ""
try {
// 没有拿到值时,会返回空串
cssVariableValue = getComputedStyle(document.documentElement).getPropertyValue(cssVariableName)
} catch (error) {
console.error(error)
}
return cssVariableValue
}
/** 用 JS 设置全局 CSS 变量 */
export const setCssVariableValue = (cssVariableName: string, cssVariableValue: string) => {
try {
document.documentElement.style.setProperty(cssVariableName, cssVariableValue)
} catch (error) {
console.error(error)
}
}
/** 重置项目配置 */
export const resetConfigLayout = () => {
removeConfigLayout()
location.reload()
}
// 输入框 输数字 取小数点4位 不能是负数
export const inputPrecision = (item, value, num = 4) => {
setTimeout(() => {
if (item[value] < 0) return (item[value] = -item[value])
const multiplier = Math.pow(10, num)
item[value] = Math.round(item[value] * multiplier) / multiplier
}, 100)
}
// 数组扁平话
export const flattenArray = (arr) => {
if (!Array.isArray(arr)) {
return [];
}
return arr.reduce((acc, val) => {
if (val.children&&val.children.length>0) {
// 如果当前元素是数组,则递归处理
return acc.concat(flattenArray(val.children));
} else {
return acc.concat(val);
}
}, []);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
上次更新: 2024/11/22, 15:42:10