j7/utils.js
2022-03-15 13:15:30 +08:00

174 lines
3.7 KiB
JavaScript

const fs = require('fs');
const crypto = require('crypto');
const crc32 = require('crc-32');
const serverEnv = process.env['SERVER_ENV'];
function rspErr(rsp, errCode, errMsg) {
rsp.send(jsonEncode({
'errcode': errCode,
'errmsg': errMsg
}));
}
function rspOk(rsp) {
rsp.send(jsonEncode({
'errcode': 0,
'errmsg': ''
}));
}
function rspData(rsp, data) {
data['errcode'] = 0;
data['errmsg'] = '';
rsp.send(jsonEncode(data));
}
function readJsonFromFile(url) {
const jsondata = fs.readFileSync(url, "utf8");
const 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) {
const year = date.getFullYear();
const month = date.getMonth() + 1;//月份是从0开始的
const day = date.getDate();
const hour = date.getHours();
const min = date.getMinutes();
const 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);
}
function pad(num, n) {
let result = num.toString();
let len = result.length;
while (len < n) {
result = '0' + result;
len++;
}
return result;
}
function randRange(min, max) {
if (min >= max) {
return min;
}
return min + Math.random() * max;
}
function jsonEncode(obj) {
return JSON.stringify(obj);
}
function isArray(val) {
return Array.isArray(val);
}
function isObject(val) {
return typeof(obj) == "object";
}
function crc32Str(str) {
return crc32.str('' + str);
}
function createAccountId(channel, gameId, openId) {
const accountId = `${channel}_${gameId}_${openId}`;
return accountId;
}
function extractChannel(accountId) {
const ss = accountId.split('_');
return ss[0];
}
function extractGameId(accountId) {
const ss = accountId.split('_');
return ss[1];
}
function extractOpenId(accountId) {
const channel = extractChannel(accountId);
const gameId = extractGameId(accountId);
const openId = accountId.substr(accountId.indexOf(`${channel}_${gameId}_`));
return openId;
}
function createSessionId(accountId, registerTime, secretKey) {
const nowTime = getUtcTime();
const rand = randRange(1, 1000000000);
const sessionId = '' +
nowTime + '_' +
registerTime + '_' +
rand + '_' +
md5Str('' + accountId + secretKey + registerTime + nowTime);
return sessionId;
}
function md5Str(data) {
const hash = crypto.createHash('md5');
return hash.update(data).digest('hex');
}
function getVal(obj, key, defVal = null) {
if (!obj){
return val;
}
return key in obj ? obj[key] : defVal;
}
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;
exports.pad = pad;
exports.randRange = randRange;
exports.jsonEncode = jsonEncode;
exports.isArray = isArray;
exports.isObject = isObject;
exports.crc32Str = crc32Str;
exports.createAccountId = createAccountId;
exports.extractChannel = extractChannel;
exports.extractGameId = extractGameId;
exports.extractOpenId = extractOpenId;
exports.createSessionId = createSessionId;
exports.md5Str = md5Str;
exports.getVal = getVal;