util.js
4.32 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import cookies from "./util.cookies"
import db from "./util.db"
import log from "./util.log"
import permision from "./util.permision"
const util = {
cookies,
db,
log,
permision
}
/**
* @description 更新标题
* @param {String} title 标题
*/
util.title = function (titleText) {
const processTitle = process.env.VUE_APP_TITLE
window.document.title = `${processTitle}${titleText ? ` | ${titleText}` : ""}`
}
/**
* @description 打开新页面
* @param {String} url 地址
*/
util.open = function (url, target) {
var a = document.createElement("a")
a.setAttribute("href", url)
a.setAttribute("target", target || "_blank")
a.setAttribute("id", "d2admin-link-temp")
document.body.appendChild(a)
a.click()
document.body.removeChild(document.getElementById("d2admin-link-temp"))
}
/**
* 深克隆 所有原型链上的对象和属性
* 例: const bar=deepClone(foo)
* console.log(bar===foo) false
* @param {*} obj
*/
util.deepClone = (obj) => {
if (typeof obj !== "object") {
return obj
}
if (Array.isArray(obj)) {
return util.deepCloneArray(obj)
} else {
return util.deepCloneObj(obj)
}
}
util.deepCloneArray = (arr) => {
// function deepCloneArray(arr) {
const target = []
arr.forEach((it) => {
if (typeof it === "object") {
target.push(util.deepClone(it))
} else {
target.push(it)
}
})
return target
}
util.deepCloneObj = (target) => {
const map = new WeakMap()
function isObject(target) {
return (
(typeof target === "object" && target) || typeof target === "function"
)
}
function clone(data) {
if (!isObject(data)) {
return data
}
if ([Date, RegExp].includes(data.constructor)) {
return new data.constructor(data)
}
// 会在生产环境的clone props对象报错 导致报错 弃用
if (typeof data === "function") {
return new Function("return " + data.toString())()
}
const exist = map.get(data)
if (exist) {
return exist
}
const keys = Reflect.ownKeys(data)
const allDesc = Object.getOwnPropertyDescriptors(data)
const result = Object.create(Object.getPrototypeOf(data), allDesc)
map.set(data, result)
keys.forEach((key) => {
const val = data[key]
if (isObject(val)) {
result[key] = clone(val)
} else {
result[key] = val
}
})
return result
}
return clone(target)
}
/**
* 深度递归搜索
* @param {Array} arr 你要搜索的数组
* @param {Function} condition 回调函数,必须返回谓词,判断是否找到了。会传入(item, index, level)三个参数
* @param {String} children 子数组的key
*/
util.deepFind = function (menus, id) {
const cloneMenus = util.deepClone(menus)
for (const menu of cloneMenus) {
if (menu.id === id) {
delete menu.children
return menu
}
if (menu.children) {
return util.deepFind(menu.children, id)
}
}
return null
}
/**
* 16进制颜色值 转换为 rgb
* @param {*} sColor 16进制颜色值
* @param {*} alpha 透明度
*/
util.colorToRgb = (sColor, alpha) => {
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
sColor = sColor.toLowerCase()
if (sColor && reg.test(sColor)) {
if (sColor.length === 4) {
var sColorNew = "#"
for (var i = 1; i < 4; i += 1) {
sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
}
sColor = sColorNew
}
// 处理六位的颜色值
var sColorChange = []
for (let i = 1; i < 7; i += 2) {
sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)))
}
if (alpha || alpha === 0 || alpha === "0") {
return `rgba(${sColorChange.join(",")} ,${alpha})`
}
return `rgb(${sColorChange.join(",")})`
} else {
return sColor
}
}
/**
* 重置form
* 用法: util.resetForm.call(this,'formName')
*/
util.resetForm = function (formName) {
this.$nextTick(() => {
for (var key in this[formName]) {
if (this[formName].hasOwnProperty(key)) {
this[formName][key] = ""
}
}
this.$refs[formName] && this.$refs[formName].resetFields()
})
}
/**
* 格式化时间
* @param {Array} time
*/
util.formatTime = function (time) {
return {
startTime: time ? time[0] + " " + "00:00:00" : "",
endTime: time ? time[1] + " " + "23:59:59" : ""
}
}
export default util
export const deepClone = util.deepClone