/**
- 将Json内容生成File
- @param name 文件名
- @param data 数据
- @returns {null|File}
*/
// const buildFileForJson = (name, data) => {
// try {
// const content = JSON.stringify(data);
// return new File([content], name, {
// type: ‘text/plain’,
// });
// } catch (e) {
// return null;
// }
// };
/**
-
深拷贝
-
@param data
-
@returns {null|any}
/
const deepCopy = (data) => {
try {
return JSON.parse(JSON.stringify(data));
} catch (e) {
return null;
}
};
/* -
预处理VinList,判断长度,去重等
-
@param list
-
@returns {any[]}
*/
const preVinList = (list) => {
// 去除空格
list.forEach((val, idx) => {
list[idx] = val.trim();
});
// 判断长度
list = list.filter((val) => val.length === 17);
// 去重
list = […new Set(list)];
return list;
};
// 乘法函数
const accMul = (arg1, arg2) => {
var m = 0,
s1 = arg1.toString(),
s2 = arg2.toString();
try {
m += s1.split(‘.’)[1].length;
} catch (e) { }
try {
m += s2.split(‘.’)[1].length;
} catch (e) { }
return (Number(s1.replace(‘.’, ‘’)) * Number(s2.replace(‘.’, ‘’))) / Math.pow(10, m);
};
module.exports = {
// 通过创建a标签下载 有两种形式 第一种是根据地址下载 第二种是根据数据下载
// type 是url还是data; data 是数据
// bssID是前缀
// suffix是后缀
downloadA(type, data, bssID, suffix) {
if (type === ‘url’) {
const link = document.createElement(‘a’);
link.style.display = ‘none’;
link.href = data;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} else {
// const str = JSON.stringify(data)
const link = document.createElement(‘a’);
// const blob = new Blob([str]);
link.style.display = ‘none’;
link.href = data;
link.download = bssID ? bssID + suffix : suffix;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
},
// 下载二进制流 excel 文件 data是接口返回的二进制流文件 name是传递过来的表格下载名字
downloadExcel(data, name) {
const url = window.URL.createObjectURL(new Blob([data], { type: ‘application/octet-stream’ }));
const link = document.createElement(‘a’);
link.style.display = ‘none’;
link.href = url;
link.setAttribute(‘download’,${name}.xlsx
);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
},
// 下载二进制流 json 文件 data是接口返回的二进制流文件 name是传递过来的表格下载名字
downloadTxt(data, name) {
const url = window.URL.createObjectURL(new Blob([data], { type: ‘application/octet-stream’ }));
const link = document.createElement(‘a’);
link.style.display = ‘none’;
link.href = url;
link.setAttribute(‘download’,${name}.json
);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
},
// 返回不带-的时间
formatDateNOlink(data, type = true) {
const time = new Date(data);
const YY =${time.getFullYear()}
;
let MM = ‘’;
let DD = ‘’;
let HH = ‘’;
let mm = ‘’;
let ss = ‘’;
if (time.getMonth() + 1 < 10) {
MM =0${time.getMonth() + 1}
;
} else {
MM =${time.getMonth() + 1}
;
}
if (time.getDate() < 10) {
DD =0${time.getDate()}
;
} else {
DD =${time.getDate()}
;
}
if (time.getHours() < 10) {
HH =0${time.getHours()}
;
} else {
HH =${time.getHours()}
;
}
if (time.getMinutes() < 10) {
mm =0${time.getMinutes()}
;
} else {
mm =${time.getMinutes()}
;
}
if (time.getSeconds() < 10) {
ss =0${time.getSeconds()}
;
} else {
ss =${time.getSeconds()}
;
}
if (!type) {
return${YY}-${MM}-${DD} ${HH}:${mm}:${ss}
;
}
return${YY + MM + DD + HH + mm + ss}
;
},
// 返回带-的时间
formatDatelink(data) {
const time = new Date(data);
const YY =${time.getFullYear()}
;
let MM = ‘’;
let DD = ‘’;
if (time.getMonth() + 1 < 10) {
MM =0${time.getMonth() + 1}
;
} else {
MM =${time.getMonth() + 1}
;
}
if (time.getDate() < 10) {
DD =0${time.getDate()}
;
} else {
DD =${time.getDate()}
;
}
return${YY + '-' + MM + '-' + DD}
;
},
// 返回时分秒
formatDateSecond() {
const time = new Date(new Date().getTime());
let HH = ‘’;
let mm = ‘’;
let ss = ‘’;
if (time.getHours() < 10) {
HH =0${time.getHours()}
;
} else {
HH =${time.getHours()}
;
}
if (time.getMinutes() < 10) {
mm =0${time.getMinutes()}
;
} else {
mm =${time.getMinutes()}
;
}
if (time.getSeconds() < 10) {
ss =0${time.getSeconds()}
;
} else {
ss =${time.getSeconds()}
;
}
return${HH}:${mm}:${ss}
;
},
// 数据对象去重处理 params是传递过来的需要去重的那一项
repeatData(data, params) {
const obj = {};
return data.reduce((item, next) => {
if (!obj[next[params]]) {
obj[next[params]] = true;
item.push(next);
}
return item;
}, []);
},
// 获取当前记录的索引
getCurrentIndex({ index, query: { currentPage = 1, pageSize = 10 } = {} }) {
if (index === undefined) return ‘’;
const serial = (currentPage - 1) * pageSize + (+index + 1);
if (serial < 10) {
return00${serial}
;
}
if (serial < 100) {
return0${serial}
;
}
return serial;
},
// 前端分页
getCurrentItems(items, { currentPage = 1, pageSize = 10 }) {
const total = items.length;
const count = currentPage * pageSize;
const end = count > total ? total : count;
return items.slice((currentPage - 1) * pageSize, end);
},
// hex to string
hextoString(hexCharCodeStr) {
const trimedStr = hexCharCodeStr.trim();
const rawStr = trimedStr.substr(0, 2).toLowerCase() === ‘0x’ ? trimedStr.substr(2) : trimedStr;
const len = rawStr.length;
if (len % 2 !== 0) {
return ‘’;
}
let curCharCode;
const resultStr = [];
for (let i = 0; i < len; i += 2) {
curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
resultStr.push(String.fromCharCode(curCharCode));
}
return resultStr.join(‘’);
},
// string to hex
stringtoHex(str) {
if (str == null || str === ‘’ || typeof str === ‘undefined’) return ‘’;
const hexCharCode = [];
// hexCharCode.push(‘0x’);
for (let i = 0; i < str.length; i += 1) {
hexCharCode.push(str.charCodeAt(i).toString(16).toUpperCase());
}
return hexCharCode.join(‘’);
},
// 获取当前时间
getCurrentMoment() {
return moment();
},
getYears(end, begin) {
end = end ? moment(end) : moment().add(20, ‘years’);
begin = begin ? moment(begin) : this.getCurrentMoment();
const years = [];
years.push(end.format(‘YYYY’));
end.subtract(1, ‘y’);
while (end >= begin) {
years.push(end.format(‘YYYY’));
end = end.subtract(1, ‘y’);
}
return years;
},
// 计算文件大小
humanFileSize(bytes, s