import moment from 'moment'
/**
* 转化时间格式
*/
export function renderTime(date) {
if (date) {
return moment(date).format('YYYY-MM-DD HH:mm:ss')
}
}
export function renderTime2(date) {
if (date) {
return moment(date).format('YYYY-MM-DD')
}
}
/**
* 定义一个深拷贝函数
*/
export function deepClone(target) {
let result
if (typeof target === 'object') {
if (Array.isArray(target)) {
result = []
for (const i in target) {
result.push(deepClone(target[i]))
}
} else if (target === null) {
result = null
} else if (target.constructor === RegExp) {
result = target
} else {
result = {}
for (const i in target) {
result[i] = deepClone(target[i])
}
}
} else {
result = target
}
return result
}
// 先判断是不是正确的JSON格式
export function isJSON(str) {
if (typeof str === 'string') {
try {
const obj = JSON.parse(str)
if (typeof obj === 'object' && obj) {
return true
} else {
return false
}
} catch (e) {
return false
}
}
}
// JSON格式化(先判断是不是正确的JSON格式)
export function formatRequest(str) {
let json
if (isJSON(str)) {
json = JSON.stringify(JSON.parse(str), null, 2)
} else {
json = str
}
return json
}
// 两个数组的数据不同,但是可以根据数组的相同id做数据融合
export function concatArr(arr1, arr2) {
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i].demanderId === arr2[j].accountId) {
arr1[i].orgId = arr2[j].orgId
arr1[i].orgStatus = arr2[j].orgStatus
arr1[i].sysVersion = arr2[j].sysVersion
arr1[i].userStatus = arr2[j].userStatus
break
}
}
}
return arr1
}
解密:
base64解密:
// base64加密的数据
const data = 'eyJpZFR5cGUiOjEsIm9yZ0lkIjoiNzc4OCIsIm9yZ05hbWUiOiJvcmc3Nzg4IiwiZW50cnkiOiJodHRwOi8vMTAuNTguMTQuMTE6ODA4OCIsImV4aXQiOiJodHRwOi8vMTAuNTguMTQuMTE6ODA4MCJ9'
const Base64 = require('js-base64').Base64
// 解密
const info = await Base64.decode(data)
加密方式:
const demandDesWay1 = {
MD5: 'MD5',
SM3: 'SM3',
SHA1: 'SHA1',
SHA256: 'SHA256',
NONE: '无',
}
const crypto = require('crypto')
const sm3 = require('sm-crypto').sm3
export function MD5Set(val) {
const hash = crypto.createHash('md5')
hash.update(val)
return hash.digest('hex')
}
export function SHA1Set(val) {
const hash = crypto.createHash('sha1')
hash.update(val)
return hash.digest('hex')
}
export function SHA256Set(val) {
const hmac = crypto.createHmac('sha256', 'secret-key')
hmac.update(val)
return hmac.digest('hex')
}
export function SM3Set(val) {
const jsencrypt = sm3(val)
return jsencrypt
}
export function NONESet(val) {
return val
}