yapi/server/utils/commons.js

247 lines
5.2 KiB
JavaScript
Raw Normal View History

import fs from 'fs-extra';
import path from 'path';
import yapi from '../yapi.js';
import sha1 from 'sha1';
2017-08-16 17:12:01 +08:00
import logModel from '../models/log.js';
2017-08-16 17:16:39 +08:00
import json5 from 'json5';
2017-07-03 16:16:05 +08:00
exports.resReturn = (data, num, errmsg) => {
2017-07-03 16:16:05 +08:00
num = num || 0;
2017-07-03 16:16:05 +08:00
return {
errcode: num,
errmsg: errmsg || 'success',
data: data
};
};
2017-07-03 16:16:05 +08:00
exports.log = (msg, type) => {
if (!msg) {
return;
}
2017-07-03 16:16:05 +08:00
type = type || 'log';
2017-07-03 16:16:05 +08:00
let f;
switch (type) {
2017-07-27 15:06:42 +08:00
case 'log':
2017-07-27 19:49:26 +08:00
f = console.log; // eslint-disable-line
2017-07-27 15:06:42 +08:00
break;
case 'warn':
2017-07-27 19:49:26 +08:00
f = console.warn; // eslint-disable-line
2017-07-27 15:06:42 +08:00
break;
case 'error':
2017-07-27 19:49:26 +08:00
f = console.error; // eslint-disable-line
2017-07-27 15:06:42 +08:00
break;
default:
2017-07-27 19:49:26 +08:00
f = console.log; // eslint-disable-line
2017-07-27 15:06:42 +08:00
break;
2017-07-03 16:16:05 +08:00
}
2017-07-03 16:16:05 +08:00
f(type + ':', msg);
2017-07-03 16:16:05 +08:00
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth();
let logfile = path.join(yapi.WEBROOT_LOG, year + '-' + month + '.log');
2017-07-03 16:16:05 +08:00
if (typeof msg === 'object') {
if (msg instanceof Error) msg = msg.message;
2017-07-03 16:16:05 +08:00
else msg = JSON.stringify(msg);
}
let data = (new Date).toLocaleTimeString() + '\t|\t' + type + '\t|\t' + msg;
2017-07-03 16:16:05 +08:00
fs.writeFileSync(logfile, data, {
flag: 'w+'
});
};
2017-07-03 16:16:05 +08:00
exports.fileExist = (filePath) => {
2017-07-03 16:16:05 +08:00
try {
return fs.statSync(filePath).isFile();
} catch (err) {
return false;
}
};
2017-07-03 16:16:05 +08:00
exports.time = () => {
return Date.parse(new Date()) / 1000;
};
exports.fieldSelect = (data, field) => {
if (!data || !field || !Array.isArray(field)) {
return null;
}
var arr = {};
field.forEach((f) => {
data[f] && (arr[f] = data[f]);
});
return arr;
};
2017-07-10 11:56:53 +08:00
exports.rand = (min, max) => {
2017-07-10 11:56:53 +08:00
return Math.floor(Math.random() * (max - min) + min);
};
2017-07-10 11:56:53 +08:00
exports.json_parse = (json) => {
try {
2017-08-16 10:56:42 +08:00
return json5.parse(json);
} catch (e) {
return json;
2017-07-10 11:56:53 +08:00
}
};
2017-07-11 12:12:43 +08:00
exports.randStr = () => {
return Math.random().toString(36).substr(2);
};
2017-07-11 12:12:43 +08:00
exports.generatePassword = (password, passsalt) => {
return sha1(password + sha1(passsalt));
};
2017-07-11 12:12:43 +08:00
exports.expireDate = (day) => {
let date = new Date();
date.setTime(date.getTime() + day * 86400000);
return date;
};
2017-07-11 16:50:17 +08:00
exports.sendMail = (options, cb) => {
if (!yapi.mail) return false;
options.subject = options.subject ? options.subject + '-yapi平台' : 'ypai平台';
cb = cb || function (err) {
if (err) {
yapi.commons.log('send mail ' + options.to + ' error,' + err.message, 'error');
} else {
yapi.commons.log('send mail ' + options.to + ' success');
2017-07-11 16:50:17 +08:00
}
};
2017-07-26 14:22:59 +08:00
try {
yapi.mail.sendMail({
2017-07-27 09:11:43 +08:00
from: yapi.WEBCONFIG.mail.from,
2017-07-26 14:22:59 +08:00
to: options.to,
subject: 'yapi平台',
html: options.contents
}, cb);
2017-07-26 14:22:59 +08:00
} catch (e) {
2017-07-27 19:49:26 +08:00
console.error(e.message); // eslint-disable-line
}
};
exports.validateSearchKeyword = keyword => {
if (/^\*|\?|\+|\$|\^|\\|\.$/.test(keyword)) {
return false;
}
return true;
};
exports.filterRes = (list, rules) => {
return list.map(item => {
let filteredRes = {};
rules.forEach(rule => {
if (typeof rule == 'string') {
filteredRes[rule] = item[rule];
} else if (typeof rule == 'object') {
filteredRes[rule.alias] = item[rule.key];
}
});
return filteredRes;
});
};
2017-07-24 11:24:08 +08:00
exports.verifyPath = (path) => {
2017-07-26 11:34:25 +08:00
if (/^\/[a-zA-Z0-9\-\/_:\.]+$/.test(path)) {
2017-07-24 11:24:08 +08:00
if (path[path.length - 1] === '/') {
return false;
} else {
return true;
2017-07-24 11:24:08 +08:00
}
} else {
return false;
}
};
2017-07-26 14:22:59 +08:00
function trim(str) {
if (!str) {
return str;
}
2017-07-26 14:22:59 +08:00
str = str + '';
return str.replace(/(^\s*)|(\s*$)/g, '');
2017-07-26 14:22:59 +08:00
}
function ltrim(str) {
if (!str) {
return str;
}
2017-07-26 14:22:59 +08:00
str = str + '';
return str.replace(/(^\s*)/g, '');
2017-07-26 14:22:59 +08:00
}
function rtrim(str) {
if (!str) {
return str;
}
2017-07-26 14:22:59 +08:00
str = str + '';
return str.replace(/(\s*$)/g, '');
2017-07-26 14:22:59 +08:00
}
exports.trim = trim;
exports.ltrim = ltrim;
exports.rtrim = rtrim;
exports.handleParams = (params, keys) => {
if (!params || typeof params !== 'object' || !keys || typeof keys !== 'object') {
return false;
}
2017-07-26 14:22:59 +08:00
for (var key in keys) {
var filter = keys[key];
if (params[key]) {
switch (filter) {
2017-07-27 15:06:42 +08:00
case 'string': params[key] = trim(params[key] + '');
break;
case 'number': params[key] = parseInt(params[key], 10);
break;
2017-07-26 14:22:59 +08:00
default: params[key] = trim(params + '');
}
}
}
2017-07-26 14:22:59 +08:00
return params;
2017-08-16 17:12:01 +08:00
};
exports.saveLog = (logData) => {
try {
let logInst = yapi.getInst(logModel);
let data = {
content: logData.content,
type: logData.type,
uid: logData.uid,
username: logData.username,
typeid: logData.typeid
};
logInst.save(data).then(
);
} catch(e) {
yapi.commons.log(e, 'error'); // eslint-disable-line
}
};