2022-01-24 17:31:39 +08:00

81 lines
1.7 KiB
JavaScript

const fs = require('fs');
const serverEnv = process.env['SERVER_ENV'];
function rspErr(rsp, errCode, errMsg) {
rsp.send(JSON.stringify({
'errcode': errCode,
'errmsg': errMsg
}));
}
function rspOk(rsp) {
rsp.send(JSON.stringify({
'errcode': 0,
'errmsg': ''
}));
}
function rspData(rsp, data) {
data['errcode'] = 0;
data['errmsg'] = '';
rsp.send(JSON.stringify(data));
}
function readJsonFromFile(url) {
let jsondata = fs.readFileSync(url, "utf8");
let json = JSON.parse(jsondata);
return json;
}
async function sleep(timeout) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve();
}, timeout);
});
}
function emptyReplace(val, newVal) {
return !val ? newVal : val;
}
function throwError(msg) {
console.log(msg);
throw msg;
}
function isOnlineEnv() {
return !serverEnv;
}
function getUtcTime() {
return Math.floor((new Date()).getTime() / 1000);
}
function formatDate(date) {
let year = date.getFullYear();
let month = date.getMonth() + 1;//月份是从0开始的
let day = date.getDate();
let hour = date.getHours();
let min = date.getMinutes();
let sec = date.getSeconds();
return year + '-' +
(month < 10 ? '0' + month : month) + '-' +
(day < 10 ? '0' + day : day) + ' ' +
(hour < 10 ? '0' + hour : hour) + ':' +
(min < 10 ? '0' + min : min) + ':' +
(sec < 10 ? '0' + sec : sec);
}
exports.rspErr = rspErr;
exports.rspOk = rspOk;
exports.rspData = rspData;
exports.readJsonFromFile = readJsonFromFile;
exports.sleep = sleep;
exports.emptyReplace = emptyReplace;
exports.throwError = throwError;
exports.isOnlineEnv = isOnlineEnv;
exports.getUtcTime = getUtcTime;
exports.formatDate = formatDate;