/**
* 日期时间格式化
* @param {需要格式化的时间} time
* @param {格式化字符串} pattern
* @returns
*/exportfunctionparseTime(time, pattern){if(arguments.length ===0||!time){returnnull}const format = pattern ||'{y}-{m}-{d} {h}:{i}:{s}'let date
if(typeof time ==='object'){
date = time
}else{if((typeof time ==='string')&&(/^[0-9]+$/.test(time))){
time =parseInt(time)}elseif(typeof time ==='string'){
time = time.replace(newRegExp(/-/gm),'/').replace('T',' ').replace(newRegExp(/\.[\d]{3}/gm),'');}if((typeof time ==='number')&&(time.toString().length ===10)){
time = time *1000}
date =newDate(time)}const formatObj ={
y: date.getFullYear(),
m: date.getMonth()+1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()}const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g,(result, key)=>{let value = formatObj[key]if(key ==='a'){return['日','一','二','三','四','五','六'][value]}if(result.length >0&& value <10){
value ='0'+ value
}return value ||0})return time_str
}
/**
* 将对象数组构造成树型结构数据
* @param {数据源} data
* @param {id字段} id
* @param {父节点字段} parentId
* @param {孩子节点字段} children
*/exportfunctionhandleTree(data, id, parentId, children){let config ={
id: id ||"id",
parentId: parentId ||"parentId",
childrenList: children ||"children",};var childrenListMap ={};var nodeIds ={};var tree =[];for(let d of data){let parentId = d[config.parentId];if(childrenListMap[parentId]==null){
childrenListMap[parentId]=[];}
nodeIds[d[config.id]]= d;
childrenListMap[parentId].push(d);}for(let d of data){let parentId = d[config.parentId];if(nodeIds[parentId]==null){
tree.push(d);}}for(let t of tree){adaptToChildrenList(t);}functionadaptToChildrenList(o){if(childrenListMap[o[config.id]]!==null){
o[config.childrenList]= childrenListMap[o[config.id]];}if(o[config.childrenList]){for(let c of o[config.childrenList]){adaptToChildrenList(c);}}}return tree;}
13、从一棵树中查找value对应的label
/**
* 从一棵树中查找value对应的label
* @param {需要查找的目标树} tree
* @param {需要查找的value值} value
* @param {value字段名} valueName
* @param {label字段名} labelName
* @param {children字段名} childrenName
* @returns
*/exportfunctionqueryTree(tree, value,valueName='value',labelName='label',childrenName='children'){let stark =[];
stark = stark.concat(tree);while(stark.length){const temp = stark.shift();if(temp[childrenName]){
stark = stark.concat(temp[childrenName]);}if(temp[valueName]===value){return temp[labelName];}}return'';}