yapi/server/utils/commons.js

190 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-07-03 16:16:05 +08:00
import fs from 'fs-extra'
import path from 'path'
import yapi from '../yapi.js'
2017-07-11 12:12:43 +08:00
import sha1 from 'sha1'
2017-07-03 16:16:05 +08:00
exports.resReturn = (data, num, errmsg) => {
2017-07-03 16:16:05 +08:00
num = num || 0;
return {
errcode: num,
errmsg: errmsg || 'success',
data: data
}
}
const MSGTYPE = {
'log': 'Log',
'warn': 'warning',
2017-07-03 16:16:05 +08:00
'error': 'Error'
}
exports.log = (msg, type) => {
if (!msg) return;
2017-07-03 16:16:05 +08:00
type = type || 'log';
let f;
switch (type) {
2017-07-03 16:16:05 +08:00
case 'log': f = console.log; break;
case 'warn': f = console.warn; break;
case 'error': f = console.error; break;
default: f = console.log; break;
2017-07-03 16:16:05 +08:00
}
f(type + ':', msg);
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;
}
}
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);
}
exports.json_parse = (json) => {
try {
2017-07-10 11:56:53 +08:00
return JSON.parse(json);
} catch (e) {
2017-07-10 11:56:53 +08:00
return json
}
2017-07-11 12:12:43 +08:00
}
exports.randStr = () => {
2017-07-11 12:12:43 +08:00
return Math.random().toString(36).substr(2)
}
exports.generatePassword = (password, passsalt) => {
return sha1(password + sha1(passsalt));
}
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, info) {
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-11 16:50:17 +08:00
}
2017-07-26 14:22:59 +08:00
try {
yapi.mail.sendMail({
2017-07-26 14:22:59 +08:00
from: yapi.WEBCONFIG.mail.auth.user,
to: options.to,
subject: 'yapi平台',
html: options.contents
}, cb)
2017-07-26 14:22:59 +08:00
} catch (e) {
console.error(e.message)
}
}
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
}
} else {
return false;
}
2017-07-26 14:22:59 +08:00
}
function trim(str) {
if (!str) return str;
str = str + '';
return str.replace(/(^\s*)|(\s*$)/g, "");
}
function ltrim(str) {
if (!str) return str;
str = str + '';
return str.replace(/(^\s*)/g, "");
}
function rtrim(str) {
if (!str) return str;
str = str + '';
return str.replace(/(\s*$)/g, "");
}
exports.trim = trim;
exports.ltrim = ltrim;
exports.rtrim = rtrim;
exports.handleParams = (params, keys) => {
if (!params || typeof params !== 'object' || !keys || typeof keys !== 'object') return false;
for (var key in keys) {
var filter = keys[key];
if (params[key]) {
switch (filter) {
case 'string': params[key] = trim(params[key] + ''); break;
case 'number': params[key] = parseInt(params[key], 10); break;
default: params[key] = trim(params + '');
}
}
}
return params;
}