Commit 2f40df9c by fukai

提交object加工处理

parent c2174da3
export default class Utils{
static toDepthObject(obj)
{
var target = {};
for(var key in obj)
{
var keyArr = key.split("_"); //以下划线或分割
var tempobj = target;
for(var i=0;i<keyArr.length - 1;i++)
{
if(tempobj[keyArr[i]])
tempobj = tempobj[keyArr[i]];
else
{
tempobj[keyArr[i]] = {};
tempobj = tempobj[keyArr[i]];
}
}
//给叶子元素赋值
tempobj[keyArr[keyArr.length - 1]]=obj[key];
}
return target;
}
static flatObject(obj,target,pkey)
{
target = target||{};
for(var key in obj)
{
var nkey = pkey?pkey+"_"+key:key;
if(!obj[key])
target[nkey] = obj[key];
else if(obj[key] instanceof Array)
target[nkey] = obj[key];
else if(typeof obj[key] == "object")
Utils.flatObject(obj[key],target,nkey); //递归赋值
else
target[nkey] = obj[key];
}
return target;
}
static copyValueFromVO(model,vo)
{
var target = model;
for(var key in vo)
{
var keyArr = key.split("_"); //以下划线或分割
var tempobj = target;
for(var i=0;i<keyArr.length - 1;i++)
{
if(tempobj[keyArr[i]])
tempobj = tempobj[keyArr[i]];
else
{
tempobj = null;
break;
}
}
//给叶子元素赋值
if(tempobj )
{
let leafProp = keyArr[keyArr.length - 1];
if(tempobj.hasOwnProperty(leafProp))
tempobj[leafProp]=obj[key];
}
}
return target;
}
}
\ No newline at end of file
...@@ -9,8 +9,34 @@ export function findCodeLabel(codes,val) ...@@ -9,8 +9,34 @@ export function findCodeLabel(codes,val)
return item.label return item.label
} }
export function dateFormat (date, format) {
let _format = format || 'yyyy-MM-dd';
const d = date;
const o = {
'M+' : d.getMonth() + 1, // month
'd+' : d.getDate(), // day
'h+' : d.getHours(), // hour
'm+' : d.getMinutes(), // minute
's+' : d.getSeconds(), // second
'q+' : Math.floor((d.getMonth() + 3) / 3), // quarter
'S' : d.getMilliseconds() // millisecond
};
if (/(y+)/.test(_format)) {
_format = _format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (const k in o) {
if (o.hasOwnProperty(k) && new RegExp('(' + k + ')').test(_format)) {
_format = _format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
}
}
return _format;
}
export default { export default {
install(Vue){ install(Vue){
Vue.prototype.findCodeLabel = findCodeLabel Vue.prototype.findCodeLabel = findCodeLabel
Vue.prototype.dateFormat = dateFormat
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment