util.js
3.12 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
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 || 'D2Admin'
window.document.title = `${processTitle}${titleText ? ` | ${titleText}` : ''}`
}
/**
* @description 打开新页面
* @param {String} url 地址
*/
util.open = function (url) {
var a = document.createElement('a')
a.setAttribute('href', url)
a.setAttribute('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) => {
// Handle the 3 simple types, and null or undefined or function
if (obj == null || typeof obj !== 'object') return obj
// Handle Date
if (obj instanceof Date) {
const copy = new Date()
copy.setTime(obj.getTime())
return copy
}
// Handle Array or Object
if (obj instanceof Array | obj instanceof Object) {
const copy = (obj instanceof Array) ? [] : {}
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) { copy[attr] = util.deepClone(obj[attr]) }
}
return copy
}
throw new Error("Unable to clone obj! Its type isn't supported.")
}
/**
* 深度递归搜索
* @param {Array} arr 你要搜索的数组
* @param {Function} condition 回调函数,必须返回谓词,判断是否找到了。会传入(item, index, level)三个参数
* @param {String} children 子数组的key
*/
util.deepFind = function (menus, id) {
const cloneMenus = util.deepClone(menus)
for (let menu of cloneMenus) {
if (menu.id === id) {
delete menu.children
return menu
}
if (menu.children) {
return util.deepFind(menu.children, id)
}
}
return null
}
util.packRoutes = function (resources, idsInPermision) {
if (!resources || !idsInPermision || !idsInPermision.length) {
throw new Error('参数错误')
}
let __aRouteInPermision = []
idsInPermision.forEach(obj => {
__aRouteInPermision.push(resources[obj.id])
})
return __aRouteInPermision
}
util.packAsideMenus = function (resources, idsInPermision) {
if (!resources || !idsInPermision || !idsInPermision.length) {
throw new Error('参数错误')
}
let __aMenusInPermision = []
let __aSub = []
idsInPermision.forEach(obj => {
const { icon, path, meta } = resources[obj.id]
const { title, id, pid } = meta
if (obj.pid === '0') {
__aMenusInPermision.push({ title, icon, path, id, pid })
} else {
__aSub.push({ title, icon, path, id, pid })
}
})
let __parent = null
__aSub.forEach(obj => {
__parent = __aMenusInPermision.find(menu => menu.id === obj.pid)
if (__parent.children) {
__parent.children.push(obj)
} else {
__parent.children = [obj]
}
})
return __aMenusInPermision
}
export default util