/** * 通用js方法封装处理 */ import CryptoJS from 'crypto-js' import JSEncrypt from 'jsencrypt' export function replaceAll (text, stringToFind, stringToReplace) { if (stringToFind == stringToReplace) return this var temp = text var index = temp.indexOf(stringToFind) while (index != -1) { temp = temp.replace(stringToFind, stringToReplace) index = temp.indexOf(stringToFind) } return temp } export function encryptPsd (data) { const RamKey = getRamNumber() const RamIv = getRamNumber() const key = CryptoJS.enc.Utf8.parse(RamKey) const iv = CryptoJS.enc.Utf8.parse(RamIv) const srcs = CryptoJS.enc.Utf8.parse(data) var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }) return { key: RamKey, iv: RamIv, password: CryptoJS.enc.Base64.stringify(encrypted.ciphertext) } } export function encryptUpdatePsd (RamIv,RamKey,data) { const key = CryptoJS.enc.Utf8.parse(RamKey) const iv = CryptoJS.enc.Utf8.parse(RamIv) const srcs = CryptoJS.enc.Utf8.parse(data) var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }) return CryptoJS.enc.Base64.stringify(encrypted.ciphertext) } export function getRamNumber () { var result = '' for (var i = 0; i < 16; i++) { result += Math.floor(Math.random() * 16).toString(16)// 获取0-15并通过toString转16进制 } // 默认字母小写,手动转大写 return result.toUpperCase()// 另toLowerCase()转小写 } export function JSEncryptData (iv, key) { const encrypt = new JSEncrypt() // 将公钥存入内 const publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuG6ZABNgLQCI9iVPDuJEb7xb5zR9UGKyKXshLuJFZxyNtaEzfpjSMqsshUYA1QpwUvkmZE0lcRE4F4QtZO9rDnH2PoW1FWRbjgg0+MKWOmZr9hSPKM/BIE+knTtTaj4/0TK7QwDd9q/QAIduC1hIyxIIkxfWx0gFuTnxui0waCwIDAQAB' encrypt.setPublicKey(publicKey) const ivData = encrypt.encrypt(iv) const keyData = encrypt.encrypt(key) return { ivData: ivData, keyData: keyData } } /** * Parse the time to string * @param {(Object|string|number)} time * @param {string} cFormat * @returns {string | null} */ export function parseTime (time, cFormat) { if (arguments.length === 0 || !time) { return null } const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if ((typeof time === 'string')) { if ((/^[0-9]+$/.test(time))) { // support "1548221490638" time = parseInt(time) } else { // support safari // https://stackoverflow.com/questions/4310953/invalid-date-in-safari time = time.replace(new RegExp(/-/gm), '/') } } if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 } date = new Date(time) } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { const value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] } return value.toString().padStart(2, '0') }) return time_str } export function getLastYear (yearNum = 1) { const today = new Date() // 当天 today.setFullYear(today.getFullYear() - yearNum) return today } export function getLastDay (yearNum = 1) { const today = new Date() // 当天 today.setDate(today.getDate() - yearNum) return today } export function transformTree (array, id_key, parentId_key, isRoot) { if (!array) return [] const idsObj = array.reduce((pre, cur) => Object.assign(pre, { [cur[id_key]]: cur }), {}) return Object.values(idsObj).reduce((pre, cur) => { const parent = idsObj[cur[parentId_key]] if (!isRoot(cur, parent)) { !parent.children && (parent.children = []) const children = parent.children !children.includes(cur) && children.push(cur) } else { pre.push(cur) } return pre }, []) } //判断值为空的状态 export function formatValue(type,value){ if(type === 'number'){ if(value === null || value === undefined){ return 0 } return value }else if(type ==='string'){ if(value === null || value === undefined){ return '' } return value } return value } export function dataConversion(target) { let date = new Date(target); let y = date.getFullYear(); // 年 let MM = date.getMonth() + 1; // 月 MM = MM < 10 ? ('0' + MM) : MM; let d = date.getDate(); // 日 d = d < 10 ? ('0' + d) : d; let h = date.getHours(); // 时 h = h < 10 ? ('0' + h) : h; let m = date.getMinutes(); // 分 m = m < 10 ? ('0' + m) : m; let s = date.getSeconds(); // 秒 s = s < 10 ? ('0' + s) : s; return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s; } export function chartYIndex(value) { if (value.name.toLowerCase().includes('pcs') || value.name.includes('有功功率')) { return 0 } else if (value.name.toLowerCase().includes('bms') || value.name.toLowerCase().includes('soc')) { return 1 } else { return 0 } } export function beforeDayTime() { // 获取当前时间 var now = new Date(); // 将时间调整到24小时前 now.setHours(now.getHours() - 24); // 格式化输出开始时间 var year_start = now.getFullYear(); var month_start = now.getMonth() + 1; month_start = month_start < 10 ? "0" + month_start : month_start; var day_start = now.getDate(); day_start = day_start < 10 ? "0" + day_start : day_start; var hours_start = now.getHours(); hours_start = hours_start < 10 ? "0" + hours_start : hours_start; var minutes_start = now.getMinutes(); minutes_start = minutes_start < 10 ? "0" + minutes_start : minutes_start; var seconds_start = now.getSeconds(); seconds_start = seconds_start < 10 ? "0" + seconds_start : seconds_start; var formattedTime_start = year_start + "-" + month_start + "-" + day_start + " " + hours_start + ":" + minutes_start + ":" + seconds_start; // 获取当前时间 var now_end = new Date(); // 格式化输出结束时间 var year_end = now_end.getFullYear(); var month_end = now_end.getMonth() + 1; month_end = month_end < 10 ? "0" + month_end : month_end; var day_end = now_end.getDate(); day_end = day_end < 10 ? "0" + day_end : day_end; var hours_end = now_end.getHours(); hours_end = hours_end < 10 ? "0" + hours_end : hours_end; var minutes_end = now_end.getMinutes(); minutes_end = minutes_end < 10 ? "0" + minutes_end : minutes_end; var seconds_end = now_end.getSeconds(); seconds_end = seconds_end < 10 ? "0" + seconds_end : seconds_end; var formattedTime_end = year_end + "-" + month_end + "-" + day_end + " " + hours_end + ":" + minutes_end + ":" + seconds_end; return [formattedTime_start, formattedTime_end]; }