1 701项目用过
公司常用,全部字典根据类型,筛选某个字典
interface RootObject {
id: number;
description: string;
code: string;
enumItems: any;
}
interface RootObjectItem {
value: string;
code: string;
}
export const getFindList = (arr: any[] = [], name: string) => {
const dict =
(arr &&
arr.filter((item: RootObject) => {
return item.code === name;
})[0]?.itemList) ||
[];
const list = dict.map((item: RootObjectItem) => {
const obj = {
label: item.value,
value: item.code,
};
return obj;
});
return list;
};
export const getDataSourceList = (arr: any[] = [], object: any) => {
if (Object.keys(object).length == 0) {
const list =
arr &&
arr.map((item: RootObject3) => {
const obj = {
id: item.value,
name: item.label,
parentId: null,
};
return obj;
});
return list || [];
}
const list =
arr &&
arr.map((item: RootObject3) => {
const obj = {
id: item.value,
name: `${item.label}(${object[item.value] || 0})`,
parentId: null,
};
return obj;
});
return list || [];
};
export const getFindListTree = (arr: any[] = [], name: string) => {
const dict =
(arr &&
arr.filter((item: RootObject2) => {
return item.code === name;
})[0]?.items) ||
[];
return dict;
};
export const getDataSourceListTree = (arr: any[] = [], object: any) => {
if (object) {
const list = arr.map((item: any) => {
const arr2: any = {};
for (const key in object) {
if (item.code == key) {
item.childrenNode = [...object[key]];
arr2.id = `${item.id}`;
arr2.caName = item.value;
arr2.childrenNode = item.childrenNode;
}
arr2.id = `${item.id}`;
arr2.caName = item.value;
}
return arr2;
});
return list;
}
const list = arr.map((item: any) => {
const obj = {
id: item.value,
caName: item.value,
childrenNode: [],
};
return obj;
});
return list;
};
根据字典的值,筛选出对应的值(name)进行展示,高频率使用
export function getValue(data: string, arr: any[] = []) {
if (arr.length) {
let name = '';
arr.map((item: any) => {
if (item.value == data) {
name = item.label;
}
});
return name;
}
return '';
}
两个数组取并集第一种:批量添加新数据时,会对比新的数据根据id进行过滤,过滤后的数组需要定义字段名,高频率使用
export const getAnalysisList = (arr: any[] = [], newArr: any[] = []) => {
const tempKeys = [];
for (const item of arr) {
if (tempKeys.indexOf(item['id']) == -1) {
tempKeys.push(item['id']);
}
}
for (const item of newArr) {
let sign = true;
for (const key of tempKeys) {
if (key == item['id']) {
sign = false;
}
}
if (sign) arr.push(item);
}
const list = arr.map((item: any, index: number) => {
const obj = {
id: item.id,
key: index + 1,
name: item.name,
goodsType: item.goodsType || item.weaponType,
analysisCount: item.analysisCount || 0,
demandCount: item.demandCount || 0,
feedBackCount: item.feedBackCount || 0,
minimum: item.minimum || item.minimum || 0,
canUse: item.canUse || 0,
goodsTotal: item.goodsTotal || item.weaponTotal,
};
return obj;
});
return list;
};
两个数组取并集第二种:批量添加新数据时,会对比新的数据根据type变量进行过滤,高频率使用
export const getReductList = (arr: any[] = [], newArr: any[] = [], type: string) => {
const tempKeys = [];
for (const item of arr) {
if (tempKeys.indexOf(item[type]) == -1) {
tempKeys.push(item[type]);
}
}
for (const item of newArr) {
let sign = true;
for (const key of tempKeys) {
if (key == item[type]) {
sign = false;
}
}
if (sign) arr.push(item);
}
return arr;
};
对象的索引值作为变量的写法[name]
export const getAnalysisCount = (arr: any[] = [], name: string, fuel: number, water: number) => {
const list = arr.map((item: any) => {
if (item.goodsType === '1') {
const obj = {
...item,
[name]: fuel?.toFixed(2),
};
return obj;
} else {
const obj = {
...item,
[name]: water?.toFixed(2),
};
return obj;
}
});
return list;
};
data2数组里满足条件的数据,插入到data1数组里
export function findList(data1: any[], data2: any[]) {
const list = data1.map((item: any) => {
const installer = data2.realList.find(
(ele: any) => ele.deviceId == item.id
);
if (installer) {
item.onlineStatus = 1;
item.startCreateTime = installer.alarmBegin;
}
return item;
});
return list;
}
2 wrj项目用过
export function classify(arr: any) {
let map: any = {};
let myArr = [];
for (let i = 0; i < arr.length; i++) {
if (!map[arr[i].groupNo]) {
myArr.push({
groupNo: arr[i].groupNo,
data: [arr[i]],
});
map[arr[i].groupNo] = arr[i];
} else {
for (let j = 0; j < myArr.length; j++) {
if (arr[i].groupNo === myArr[j].groupNo) {
myArr[j].data.push(arr[i]);
break;
}
}
}
}
return myArr;
}
export const generateArr = (arr: any) => {
const map = new Map();
if (arr?.length) {
arr.forEach((item: any) => {
const sign = item.groupNo;
if (map.has(sign)) {
const temp = map.get(sign);
temp.push(item);
map.set(sign, temp);
} else {
map.set(sign, [item]);
}
});
}
return Object.values(Object.fromEntries(map));
};
上移,下移某个数组里的值
const upItem = (item: any) => {
const index = state.formData.dataItem.indexOf(item);
swapArray(state.formData.dataItem, index - 1, index, 0);
};
const downItem = (item: any) => {
const index = state.formData.dataItem.indexOf(item);
swapArray(state.formData.dataItem, index, index + 1, 0);
};
export const swapArray = (arr:any, index1:any, index2:any,max:any) => {
if (index1<max) {
return ElMessage.error('已在最上面');
}
if (index2>=arr.length) {
return ElMessage.error('已在最下面');
}
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
};
export const swapArrayPlus = (arr:any, index1:any, index2:any) => {
if (index1<3) {
return ElMessage.error('已在最上面');
}
if (index2>=arr.length) {
return ElMessage.error('已在最下面');
}
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
};
保留*位小数,默认两位
export function getComFixed(data: number, len = 2) {
if (data && data >= 0) {
return data.toFixed(len);
}
return '-';
}
数组转化字符串
export function getJoin(data: any, sty: any = ',') {
if (data && data.length) {
return data.join(sty);
}
}
二维数组转化-一维数组
export function getEWList(data: any) {
if (data && data.length) {
const list = data.map((item: any) => {
return { fLat: item[0], fLon: item[1] };
});
return list;
}
}
export const erWeiArr = (arr: any) => {
const res: any = [];
arr.map((item: any, index: any) => {
const temp = arr.slice(index * 1, index * 1 + 1);
res.push(temp);
});
return res;
};
export const numberArr = (arr: any) => {
const res: any = [];
arr.map((item: any, index: any) => {
const temp = item[0].split(',');
res.push(temp);
});
return res;
};
时间
import moment from 'moment';
export function getMomentData(data: number) {
if (data) {
return moment(data).format('YYYY-MM-DD HH:mm:ss');
}
return '-';
}
export function getMomentData2(data: number) {
if (data) {
return moment(data).toDate().getTime();
}
return null;
}
export const formatSecond = (data: any) => {
const tData = parseInt(data) || 0;
const minutes = Math.floor(tData / 60);
const second = tData % 60;
return `${minutes}分${second}秒`;
};
export const secondToHSM = (sData: any) => {
const h = Math.floor(sData / 3600);
const minute = Math.floor((sData / 60) % 60);
const second = Math.ceil(sData % 60);
const hours = h < 10 ? '0' + h : h;
const formatSecond = second > 59 ? 59 : second;
return `${hours > 0 ? `${hours}:` : ''}${minute < 10 ? '0' + minute : minute}:${
formatSecond < 10 ? '0' + formatSecond : formatSecond
}`;
};
export const formatting = (data: any) => {
const newData = data.map((item: any) => {
return {
...item,
children: item?.children?.map((pl: any) => ({
...pl,
childrenFlag: true,
})),
};
});
return newData;
};
export const getLableValue2 = (data: any) => {
const arr: any = [];
data.map((item: any) => {
arr.push({
label: item.name,
value: item.vmf,
children: getLableValue2(item.children),
});
});
return arr;
};
export const arrTree = (list: any) => {
const arr: any = [];
list.forEach((item: any) => {
arr.push(item);
item.children && arr.push(...arrTree(item.children));
});
return arr;
};
操作文件图片的一些方法
export const getName = (arr: any) => {
const list =
arr &&
arr.map((item: any, index: any) => {
const obj = { ...item, name: item.file_name + "." + item.file_type };
return obj;
});
return list;
};
export const getBase64 = (file: any) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = (error) => reject(error);
});
};
export const getFileData = (arr: any[]) => {
return new Promise((resolve, reject) => {
let list: any = [];
arr.map(async (file: any) => {
let res = (await getBase64(file.raw)) as any;
list.push(res);
if (list.length === arr.length) {
resolve(list);
}
});
});
};
export function base64ToFile(base64: string, fileName: string, fileType: any) {
const fileStream = base64;
const blob = new Blob([fileStream], {
type: "application/octet-stream",
});
const file: any = new File([blob], fileName + "." + fileType, {
type: fileType,
});
return file;
}
export function base64ToFile2(base64: any, fileName: any, fileType: any) {
let bstr = atob(base64);
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], fileName, { type: fileType });
}
export const getExportFile = (url: string) => {
const link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", "文件名");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};