export function deleteChildren(arr: any) {
let child = arr;
for (let i = child.length; i--; i > 0) {
if (child[i].children) {
if (child[i].children.length) {
deleteChildren(child[i].children);
} else {
delete child[i].children;
}
}
}
return arr;
}
export function getLocationName(tree: any, id: any) {
var isGet = false;
var retNode = null;
function deepSearch(tree: any, id: any) {
for (var i = 0; i < tree.length; i++) {
if (tree[i].children && tree[i].children.length > 0) {
deepSearch(tree[i].children, id);
}
if (id === tree[i].locationId || isGet) {
isGet || (retNode = tree[i]);
isGet = true;
break;
}
}
}
deepSearch(tree, id);
return retNode;
}
export const findParent = (data:any, target:any, result:any) => {
for (let i in data) {
let item = data[i]
if (item.locationId === target) {
result.unshift(item.locationId)
return true
}
if (item.children && item.children.length > 0) {
let ok = findParent(item.children, target, result)
if (ok) {
result.unshift(item.locationId)
return result
}
}
}
return false
}
export function toCamel(data: any) {
if (typeof data != 'object' || !data) return data;
if (Array.isArray(data)) {
return data.map((item: any) => toCamel(item));
}
const newData: any = {};
for (let key in data) {
let newKey = key.replace(/_([a-z])/g, (p, m) => m.toUpperCase());
newData[newKey] = toCamel(data[key]);
}
return newData;
}
export function valueFind(valueList: any, key: any) {
return valueList.value.find((item: any) => item.label === key).value;
}
export function labelFind(valueList: any, val: any) {
return valueList.find((item: any) => item.value == val).label;
}
export function getAge(val: any) {
let currentYear = new Date().getFullYear();
let calculationYear = new Date(val).getFullYear();
const wholeTime = currentYear + val.substring(4);
const calculationAge = currentYear - calculationYear;
if (new Date().getTime() > new Date(wholeTime).getTime()) {
return calculationAge;
} else {
return calculationAge - 1;
}
}
- 转换日期格式为'yyyy-MM-dd HH:mm:ss'
export function formatDate (oldDate) {
function add0 (num) { return num < 10 ? '0' + num : num };
const date = new Date(oldDate);
const Y = date.getFullYear();
const M = date.getMonth() + 1;
const D = date.getDate();
const h = date.getHours();
const m = date.getMinutes();
const s = date.getSeconds();
const dateString = Y + '-' + add0(M) + '-' + add0(D) + ' ' + add0(h) + ':' + add0(m) + ':' + add0(s);
return dateString;
}
export function parseMoney(num) {
if (!num) {
return "0.00";
}
num = Math.round(num * 100) / 100;
let result = num.toLocaleString();
var arrayNum = result.split(".");
if (arrayNum.length === 1) {
result += ".00";
}
if (arrayNum.length === 2) {
if (arrayNum[1].length < 2) {
result += "0";
}
}
return result;
}
export function getDateTime (type : string) {
let date = new Date();
let hengGang = "-";
let maoHao = ":";
let year : any = date.getFullYear();
let month : any = date.getMonth() + 1;
let curDate : any = date.getDate();
let curHours : any = date.getHours();
let curMinutes : any = date.getMinutes();
let curSeconds : any = date.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (curDate >= 0 && curDate <= 9) {
curDate = "0" + curDate;
}
if (curHours >= 0 && curHours <= 9) {
curHours = "0" + curHours;
}
if (curMinutes >= 0 && curMinutes <= 9) {
curMinutes = "0" + curMinutes;
}
if (curSeconds >= 0 && curSeconds <= 9) {
curSeconds = "0" + curSeconds;
}
let currentDate = "";
if (type == "year") {
currentDate = year;
return currentDate;
} else if (type == "month") {
currentDate = year + hengGang + month;
return currentDate;
}
else if(type == 'currentMonth'){
return month;
}
else {
currentDate = year + hengGang + month + hengGang + curDate + " " + curHours + maoHao + curMinutes + maoHao + curSeconds;
return currentDate;
}
}
const getCurrentDate = () => {
const date = new Date();
const year: any = date.getFullYear();
let month: any = date.getMonth() + 1;
if (month >= 1 && month <= 9) {
month = '0' + month;
}
const currentDate = year + '-' + month;
return currentDate;
};
export const getUpMonth = (currentDate: any) => {
const year = currentDate.split('-')[0];
const month = currentDate.split('-')[1];
let year2 = year;
let month2: any = parseInt(month) - 1;
if (month2 == 0) {
year2 = parseInt(year2) - 1;
month2 = 12;
}
if (month2 < 10) {
month2 = '0' + month2;
}
const m = year2.toString();
const n = month2.toString();
const t2 = m + '-' + n;
return t2;
};
export const bookmarksBarVisible = () =>{
let isBookmarksBarVisible = false;
if (window.innerHeight > 0) {
let prevHeight = window.innerHeight;
window.addEventListener('resize', function() {
if (window.innerHeight < prevHeight) {
isBookmarksBarVisible = true;
console.log('书签栏是否可见:', isBookmarksBarVisible);
} else {
isBookmarksBarVisible = false;
console.log('书签栏是否可见:', isBookmarksBarVisible);
}
prevHeight = window.innerHeight;
});
}
return isBookmarksBarVisible;
}
export const getBookmarksBarHeight = () => {
let bookmarksBarHeight = 0;
if (window.innerHeight > 0) {
let div = document.createElement('div');
div.style.position = 'fixed';
div.style.top = '-1000px';
div.style.left = '-1000px';
div.style.width = '100px';
div.style.height = '100px';
div.style.overflow = 'scroll';
document.body.appendChild(div);
setTimeout(function() {
bookmarksBarHeight = div.offsetHeight - div.clientHeight;
console.log('书签栏高度为:', bookmarksBarHeight);
document.body.removeChild(div);
}, 10);
}
}
export function debounce(func, wait) {
let timeout;
return function() {
const context = this;
const args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
func.apply(context, args);
}, wait);
};
}
使用:
data() {
return {
debouncedHandleInput: null,
}
},
this.debouncedHandleInput = debounce(this.handleInput.bind(this), 300);
<u--input @change="debouncedHandleInput($event,month)" border="none" :value="formData[month.monthId]" placeholder="请输入" ></u--input>