You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.6 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import typeUtil from './type'
let pro = {
// copy:function(data) {
// const t = typeUtil.getType(data);
// let o;
// if (t === 'array') {
// o = [];
// } else if (t === 'object') {
// o = {};
// } else {
// return data;
// }
// if (t === 'array') {
// for (let i = 0; i < data.length; i++) {
// o.push(copy(data[i]));
// }
// } else if (t === 'object') {
// for (let i in data) {
// o[i] = copy(data[i]);
// }
// }
// return o;
// },
copy: (function () {
'use strict'
return function innerCopy(data) {
const t = typeUtil.getType(data);
let o;
if (t === 'array') {
o = [];
for (let i = 0; i < data.length; i++) {
o.push(innerCopy(data[i]));
}
} else if (t === 'object') {
o = {};
for (let i in data) {
if (Object.prototype.hasOwnProperty.call(data, i)) {
o[i] = innerCopy(data[i]);
}
}
} else {
return data;
}
return o;
}
})(),
flatMap: (function () {
'use strict'
return function innerMap(arr) {
let newArr = [];
arr.forEach(item => {
newArr.push(item);
if (item.children && item.children.length > 0) {
newArr.push(...innerMap(item.children))
}
})
return newArr;
}
})(),
dateFormat: function (inDate) {
const daterc = inDate
if (daterc != null) {
var date = new Date(daterc);
var year = date.getFullYear();
/* 在日期格式中月份是从0开始11结束因此要加0
* 使用三元表达式在小于10的前面加0以达到格式统一 如 09:11:05
* */
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
// 拼接
return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}
},formatDate:function (dateString,format) {
const date = new Date(dateString);
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
let hour=date.getHours();
let minute=date.getMinutes();
let second=date.getSeconds();
if(hour<10){
hour ="0"+hour;
}
if(minute<10){
minute ="0"+minute;
}
if(second<10){
second ="0"+second;
}
const milSecond=date.getMilliseconds();
if(format=="YYYY-MM-DD"){
return `${year}-${month}-${day}`;
}else if(format=="YYYY-MM-DD HH:MM:SS"){
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}else if(format=="YYYY-MM-DD HH:MM:SS:SSS"){
return `${year}-${month}-${day} ${hour}:${minute}:${second}:${milSecond}`;
}else if(format=="HH:MM:SS"){
return `${hour}:${minute}:${second}:${milSecond}`;
}
return "";
},NumRound(num) {//保留两位小数
num=Math.round(num*100)/100
return num;
},dateFormatDay: function (inDate) {
const daterc = inDate
if (daterc != null) {
var date = new Date(daterc);
var year = date.getFullYear();
/* 在日期格式中月份是从0开始11结束因此要加0
* 使用三元表达式在小于10的前面加0以达到格式统一 如 09:11:05
* */
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
// 拼接
return year + "-" + month + "-" + day;
}
},
checkMonth(i) {
if (i < 10) {
i = "0" + i;
}
return i;
},
MonthAdd(date,num) {
let currentDate = date;
currentDate = new Date(currentDate);
let nextDate = currentDate.setMonth(currentDate.getMonth() + num); // 输出日期格式为毫秒形式1556668800000
nextDate = new Date(nextDate);
// let nextYear = nextDate.getFullYear();
// let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11所以要显示正确的月份需要 + 1
// nextDate = nextYear + "-" + nextMonth+'-'+nextDate.getDate()+" "+nextDate.getHours()+":"+nextDate.getMonth()+":"+nextDate.getSeconds();
date = nextDate;
return date;
},DayDec(date,num) {//前 num天数 的日期
var dd = new Date(date);
dd.setDate(dd.getDate() + num);
var y = dd.getFullYear();
var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
return y + "-" + m + "-" + d;
},
YearAdd(date,num) {
let currentDate = date;
currentDate = new Date(currentDate);
let nextDate = currentDate.setYear(currentDate.getFullYear() + num); // 输出日期格式为毫秒形式1556668800000
nextDate = new Date(nextDate);
// nextDate = nextYear + "-" + nextMonth+'-'+nextDate.getDate()+" "+nextDate.getHours()+":"+nextDate.getMonth()+":"+nextDate.getSeconds();
date = nextDate;
return date;
},
toCamelCase: function (s) {
return s.replace(/([A-Z])/g, '_$1')
},
ensureNotNull: function (value) {
if (typeUtil.isNull(value) || typeUtil.isUndefined(value)) {
return 0
}
return value
},
deepLocate: (function () {
'use strict'
return function innerLocate(target, arr) {
let newArr = [];
arr.forEach(item => {
newArr.push(item);
if (item.children && item.children.length > 0) {
newArr.push(...innerLocate(item.children))
}
})
return newArr;
}
})(),
getParentNodes: (function () {
'use strict'
return function getInnerParentNodes(currentValue, allDatas, props = {
label: 'name',
key: 'id',
children: 'children'
}) {
let parent = [];
(allDatas || []).forEach(item => {
if (parent.length == 0) {
if (currentValue == (item[props.key] || 0)) {
parent.push(item);
} else if (item[props.children] && item[props.children].length > 0) {
const childArr = getInnerParentNodes(item[props.children])
if (childArr.length > 0) {
parent.push(item);
parent = parent.concat(childArr)
}
}
}
})
return parent
}
})(),
}
export default pro;