181 lines
3.3 KiB
JavaScript
181 lines
3.3 KiB
JavaScript
/**
|
|
* 工具类
|
|
*/
|
|
const tool = {
|
|
/**
|
|
* 本地存储操作
|
|
*/
|
|
data: {
|
|
/**
|
|
* 设置本地存储
|
|
* @param {string} key - 键名
|
|
* @param {*} value - 值
|
|
*/
|
|
set(key, value) {
|
|
if (typeof value === 'object') {
|
|
localStorage.setItem(key, JSON.stringify(value))
|
|
} else {
|
|
localStorage.setItem(key, value)
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 获取本地存储
|
|
* @param {string} key - 键名
|
|
* @param {*} defaultValue - 默认值
|
|
* @returns {*}
|
|
*/
|
|
get(key, defaultValue = null) {
|
|
const value = localStorage.getItem(key)
|
|
if (!value) {
|
|
return defaultValue
|
|
}
|
|
try {
|
|
return JSON.parse(value)
|
|
} catch (e) {
|
|
return value
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 删除本地存储
|
|
* @param {string} key - 键名
|
|
*/
|
|
remove(key) {
|
|
localStorage.removeItem(key)
|
|
},
|
|
|
|
/**
|
|
* 清空本地存储
|
|
*/
|
|
clear() {
|
|
localStorage.clear()
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 树形结构转列表
|
|
* @param {Array} tree - 树形结构数据
|
|
* @param {string} childrenKey - 子节点键名
|
|
* @returns {Array} 扁平化后的数组
|
|
*/
|
|
tree_to_list(tree, childrenKey = 'children') {
|
|
if (!tree || !Array.isArray(tree)) {
|
|
return []
|
|
}
|
|
|
|
const result = []
|
|
|
|
const traverse = (nodes) => {
|
|
if (!nodes || !Array.isArray(nodes)) {
|
|
return
|
|
}
|
|
nodes.forEach((node) => {
|
|
result.push(node)
|
|
if (node[childrenKey] && node[childrenKey].length > 0) {
|
|
traverse(node[childrenKey])
|
|
}
|
|
})
|
|
}
|
|
|
|
traverse(tree)
|
|
return result
|
|
},
|
|
|
|
/**
|
|
* 列表转树形结构
|
|
* @param {Array} list - 列表数据
|
|
* @param {string} idKey - ID键名
|
|
* @param {string} parentIdKey - 父ID键名
|
|
* @param {string} childrenKey - 子节点键名
|
|
* @returns {Array} 树形结构数据
|
|
*/
|
|
list_to_tree(list, idKey = 'id', parentIdKey = 'parentId', childrenKey = 'children') {
|
|
if (!list || !Array.isArray(list)) {
|
|
return []
|
|
}
|
|
|
|
const map = {}
|
|
const roots = []
|
|
|
|
// 创建映射表
|
|
list.forEach((item) => {
|
|
map[item[idKey]] = { ...item, [childrenKey]: [] }
|
|
})
|
|
|
|
// 构建树形结构
|
|
list.forEach((item) => {
|
|
const node = map[item[idKey]]
|
|
const parentId = item[parentIdKey]
|
|
|
|
if (parentId && map[parentId]) {
|
|
map[parentId][childrenKey].push(node)
|
|
} else {
|
|
roots.push(node)
|
|
}
|
|
})
|
|
|
|
return roots
|
|
},
|
|
|
|
/**
|
|
* 深拷贝
|
|
* @param {*} obj - 要拷贝的对象
|
|
* @returns {*} 拷贝后的对象
|
|
*/
|
|
deepClone(obj) {
|
|
if (obj === null || typeof obj !== 'object') {
|
|
return obj
|
|
}
|
|
|
|
if (Array.isArray(obj)) {
|
|
return obj.map((item) => this.deepClone(item))
|
|
}
|
|
|
|
const cloned = {}
|
|
for (const key in obj) {
|
|
if (obj.hasOwnProperty(key)) {
|
|
cloned[key] = this.deepClone(obj[key])
|
|
}
|
|
}
|
|
|
|
return cloned
|
|
},
|
|
|
|
/**
|
|
* 防抖函数
|
|
* @param {Function} func - 要防抖的函数
|
|
* @param {number} wait - 等待时间
|
|
* @returns {Function}
|
|
*/
|
|
debounce(func, wait = 300) {
|
|
let timeout
|
|
return function (...args) {
|
|
clearTimeout(timeout)
|
|
timeout = setTimeout(() => {
|
|
func.apply(this, args)
|
|
}, wait)
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 节流函数
|
|
* @param {Function} func - 要节流的函数
|
|
* @param {number} wait - 等待时间
|
|
* @returns {Function}
|
|
*/
|
|
throttle(func, wait = 300) {
|
|
let timeout
|
|
return function (...args) {
|
|
if (!timeout) {
|
|
timeout = setTimeout(() => {
|
|
func.apply(this, args)
|
|
timeout = null
|
|
}, wait)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export default tool
|