2017-07-03 16:16:05 +08:00
|
|
|
|
|
|
|
import fs from 'fs-extra'
|
|
|
|
import path from 'path'
|
2017-07-04 16:43:07 +08:00
|
|
|
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)=> {
|
|
|
|
num = num || 0;
|
|
|
|
return {
|
|
|
|
errcode: num,
|
|
|
|
errmsg: errmsg || 'success',
|
|
|
|
data: data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const MSGTYPE = {
|
|
|
|
'log' : 'Log',
|
|
|
|
'warn' : 'warning',
|
|
|
|
'error': 'Error'
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.log = (msg, type) => {
|
|
|
|
if(!msg) return;
|
|
|
|
type = type || 'log';
|
|
|
|
let f;
|
|
|
|
switch(type){
|
|
|
|
case 'log': f = console.log; break;
|
|
|
|
case 'warn': f = console.warn; break;
|
|
|
|
case 'error': f= console.error; break;
|
|
|
|
default : f = console.log; break;
|
|
|
|
}
|
|
|
|
f(type + ':', msg);
|
|
|
|
let date = new Date();
|
|
|
|
let year = date.getFullYear();
|
|
|
|
let month = date.getMonth();
|
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
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;
|
|
|
|
else msg = JSON.stringify(msg);
|
|
|
|
}
|
|
|
|
let data= (new Date).toLocaleTimeString() + "\t|\t" + type + "\t|\t" + msg;
|
|
|
|
fs.writeFileSync(logfile, data, {
|
|
|
|
flag: 'w+'
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exports.fileExist = (filePath) =>{
|
|
|
|
try {
|
|
|
|
return fs.statSync(filePath).isFile();
|
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:43:07 +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)=>{
|
|
|
|
return Math.floor(Math.random() * (max - min) + min);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.json_parse = (json)=>{
|
|
|
|
try{
|
|
|
|
return JSON.parse(json);
|
|
|
|
}catch(e){
|
|
|
|
return json
|
|
|
|
}
|
2017-07-11 12:12:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.randStr = ()=> {
|
|
|
|
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-04 16:43:07 +08:00
|
|
|
}
|