You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.7 KiB
JavaScript

const formatTime = time => {
var date = new Date(time);
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('-')
// + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
const decimal2Hex = (d) => {
try {
let i = d
if(typeof i === 'string') {
const regex = /^[0-9]+$/
if(regex.test(i)) {
i = parseInt(i)
return i.toString(16).toUpperCase()
}
} else if(typeof i === 'number') {
return i.toString(16).toUpperCase()
}
} catch (error) {
console.error(error)
}
return d
}
const encodeQueryParam = (params) => {
console.log(params)
if (!params) return {};
const queryParam = {};
// 格式化查询参数
const { terms } = params;
const { sorts } = params;
Object.keys(params).forEach((key) => {
if (key === 'terms') {
let index = 0;
if (!terms) return;
Object.keys(terms).forEach((k) => {
if (
!(terms[k] === '' || terms[k] === undefined || terms[k].length === 0 || terms[k] === {})
) {
if (k.indexOf('$LIKE') > -1 && terms[k].toString().indexOf('%') === -1) {
terms[k] = `%${terms[k]}%`;
}
if (k.indexOf('$IN') > -1) {
terms[k] = terms[k].toString();
} else if (k.indexOf('$START') > -1) {
terms[k] = `%${terms[k]}`;
} else if (k.indexOf('$END') > -1) {
terms[k] = `${terms[k]}%`;
}
if (k.indexOf('@') > -1) {
const temp = k.split('@');
queryParam[`terms[${index}].column`] = temp[0];
queryParam[`terms[${index}].type`] = temp[1];
} else if (k.indexOf('#') > -1) {
const temp = k.split('#');
queryParam[`terms[${index}].column`] = temp[0];
queryParam[`terms[${index}].termType`] = temp[1];
} else {
queryParam[`terms[${index}].column`] = k;
}
queryParam[`terms[${index}].value`] = terms[k];
index += 1;
}
});
} else if (key === 'sorts') {
if (!sorts) return;
if (Object.keys(sorts).length > 0) {
queryParam[`sorts[0].name`] = sorts.field;
queryParam[`sorts[0].order`] = (sorts.order || '').replace('end', '');
}
} else {
queryParam[key] = params[key];
}
});
console.log(queryParam)
return queryParam;
}
module.exports = {
formatTime: formatTime,
encodeQueryParam: encodeQueryParam,
decimal2Hex: decimal2Hex
}