通用js方法封装处理
export function tansParams(params) {
let result = ''
for (const propName of Object.keys(params)) {
const value = params[propName];
var part = encodeURIComponent(propName) + "=";
if (value !== null && value !== "" && typeof (value) !== "undefined") {
if (typeof value === 'object') {
for (const key of Object.keys(value)) {
if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
let params = propName + '[' + key + ']';
var subPart = encodeURIComponent(params) + "=";
result += subPart + encodeURIComponent(value[key]) + "&";
}
}
} else {
result += part + encodeURIComponent(value) + "&";
}
}
}
return result
}
export function blobValidate(data) {
return data.type !== 'application/json'
}
export const generateCarouselItems = (arr, itemsPerGroup) => {
const totalGroups = Math.ceil(arr.length / itemsPerGroup);
const result = [];
for (let i = 0; i < totalGroups; i++) {
const startIndex = i * itemsPerGroup;
const endIndex = startIndex + itemsPerGroup;
result.push(arr.slice(startIndex, endIndex));
}
return result;
};
export function extractContent(htmlString) {
let withoutImg = htmlString.replace(/<img[^>]*>/g, '');
var removeImgTag = withoutImg.replace(/<br>/g, "");
removeImgTag = removeImgTag.replace(/\s+/g, ' ').trim();
const regex = /<p>(.*?)<\/p>/g;
const matches = [];
let match;
while ((match = regex.exec(removeImgTag)) !== null) {
matches.push(match[1]);
}
for (let i = 0; i < matches.length; i++) {
if (matches[i]) {
return matches[i];
}
}
return ''
};
export function formatDate(timestamp, format = 'y-m-d') {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const formats = {
'y': year,
'm': month,
'd': day,
'y-m': `${year}/${month}`,
'm-d': `${month}-${day}`,
'y.m': `${year}.${month}`,
'default': `${year}-${month}-${day}`
};
return formats[format] || formats['default'];
};
export function getDictLabelFromVal(dictionaryArray, dictValue) {
const dictItem = dictionaryArray.find(item => item.dictValue === dictValue);
return dictItem ? dictItem.dictLabel : null;
}
export function filterByFields(dataArray, filtersArray) {
return dataArray.filter(function (item) {
for (const filterObj of filtersArray) {
let match = true;
for (const key in filterObj) {
if (item.hasOwnProperty(key) && item[key] !== filterObj[key]) {
match = false;
break;
}
}
if (match) {
return true;
}
}
return false;
});
export function removeTagsAndSpaces(input) {
var output = input.replace(/<\/?[^>]+(>|$)/g, '');
output = output.replace(/[\t\n\r]/g, '');
output = output.replace(/ /g, '');
output = output.replace(/\s+/g, '');
return output;
},
}