|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { appendFile } = require('node:fs/promises');
|
|
|
|
const md5 = require('md5');
|
|
|
|
|
|
|
|
class Common {
|
|
|
|
static sortDict(obj) { //dict按key排序
|
|
|
|
return Object.keys(obj).sort().reduce(function(result, key) {
|
|
|
|
result[key] = obj[key];
|
|
|
|
return result;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
static sign(params, token) { //对参数做MD5签名
|
|
|
|
return md5( JSON.stringify(Common.sortDict(params)) + token );
|
|
|
|
}
|
|
|
|
|
|
|
|
static isPhoneNumber(number) {
|
|
|
|
return /^1[3-9][0-9]{9}$/i.test(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isVerifyCode(code) {
|
|
|
|
return /^[0-9]{4}$/i.test(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getTimestampInSeconds() {
|
|
|
|
return Math.floor(Date.now() / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
//复制参数对象
|
|
|
|
static copyBodyParams(dict, ignoreKeys) {
|
|
|
|
let params = {};
|
|
|
|
|
|
|
|
for (const key in dict) {
|
|
|
|
if (typeof(ignoreKeys) == 'undefined' || ignoreKeys.find((item) => item == key) == false) {
|
|
|
|
params[key] = dict[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
static saveLog(filePath, content) {
|
|
|
|
try {
|
|
|
|
const promise = appendFile(filePath, content);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`log save to %s failed, exception: %s`, filePath, JSON.stringify(err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.default = Common;
|