2017-09-03 08:43:13 +08:00
|
|
|
|
const path = require('path');
|
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
const nodemailer = require('nodemailer');
|
|
|
|
|
const config = require('../../config.json');
|
2017-07-26 11:26:28 +08:00
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
|
let insts = new Map();
|
2017-07-11 16:50:17 +08:00
|
|
|
|
let mail;
|
2017-07-04 16:43:07 +08:00
|
|
|
|
|
2017-07-05 17:59:53 +08:00
|
|
|
|
const WEBROOT = path.resolve(__dirname, '..'); //路径
|
2017-07-04 16:43:07 +08:00
|
|
|
|
const WEBROOT_SERVER = __dirname;
|
2017-07-27 14:21:22 +08:00
|
|
|
|
const WEBROOT_RUNTIME = path.resolve(__dirname, '../..');
|
2017-07-04 16:43:07 +08:00
|
|
|
|
const WEBROOT_LOG = path.join(WEBROOT_RUNTIME, 'log');
|
2017-07-26 22:03:18 +08:00
|
|
|
|
const WEBCONFIG = config;
|
2017-07-04 16:43:07 +08:00
|
|
|
|
|
2017-07-11 16:50:17 +08:00
|
|
|
|
fs.ensureDirSync(WEBROOT_LOG);
|
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
|
if (WEBCONFIG.mail) {
|
|
|
|
|
mail = nodemailer.createTransport(WEBCONFIG.mail);
|
2017-07-11 16:50:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 16:52:48 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取一个model实例,如果不存在则创建一个新的返回
|
|
|
|
|
* @param {*} m class
|
|
|
|
|
* @example
|
|
|
|
|
* yapi.getInst(groupModel, arg1, arg2)
|
|
|
|
|
*/
|
2017-07-26 22:03:18 +08:00
|
|
|
|
function getInst(m, ...args) {
|
|
|
|
|
if (!insts.get(m)) {
|
|
|
|
|
insts.set(m, new m(args));
|
2017-07-05 16:52:48 +08:00
|
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
return insts.get(m);
|
2017-07-05 16:52:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
|
function delInst(m) {
|
|
|
|
|
try {
|
|
|
|
|
insts.delete(m);
|
|
|
|
|
} catch (err) {
|
2017-07-27 19:49:26 +08:00
|
|
|
|
console.error(err); // eslint-disable-line
|
2017-07-05 16:52:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 10:24:13 +08:00
|
|
|
|
var hooks = {
|
|
|
|
|
'third_login': {
|
|
|
|
|
type: 'single',
|
|
|
|
|
listener: null
|
|
|
|
|
},
|
|
|
|
|
'add_interface': {
|
|
|
|
|
type: 'mulit',
|
|
|
|
|
listener: []
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function bindHook(name, listener){
|
|
|
|
|
if(!name) throw new Error('缺少hookname');
|
|
|
|
|
if(name in hooks === false){
|
|
|
|
|
throw new Error('不存在的hookname');
|
|
|
|
|
}
|
|
|
|
|
if(hooks[name].type === 'multi'){
|
|
|
|
|
hooks[name].listener.push(listener);
|
|
|
|
|
}else{
|
|
|
|
|
hooks[name].listener = listener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function emitHook(name){
|
|
|
|
|
if(!name) throw new Error('缺少hookname');
|
|
|
|
|
if(name in hooks === false){
|
|
|
|
|
throw new Error('不存在的hookname');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(hooks[name] && typeof hooks[name] === 'object'){
|
|
|
|
|
if(hooks[name].type === 'single' && typeof hooks[name].listener === 'function'){
|
|
|
|
|
return hooks[name].listener.call();
|
|
|
|
|
}
|
|
|
|
|
if(Array.isArray(hooks[name.listener])){
|
|
|
|
|
hooks[name].listener.forEach(listener=>{
|
|
|
|
|
listener.call()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 16:50:17 +08:00
|
|
|
|
let r = {
|
2017-07-04 16:43:07 +08:00
|
|
|
|
fs: fs,
|
|
|
|
|
path: path,
|
|
|
|
|
WEBROOT: WEBROOT,
|
|
|
|
|
WEBROOT_SERVER: WEBROOT_SERVER,
|
|
|
|
|
WEBROOT_RUNTIME: WEBROOT_RUNTIME,
|
|
|
|
|
WEBROOT_LOG: WEBROOT_LOG,
|
|
|
|
|
WEBCONFIG: WEBCONFIG,
|
2017-07-05 16:52:48 +08:00
|
|
|
|
getInst: getInst,
|
|
|
|
|
delInst: delInst,
|
2017-09-05 10:24:13 +08:00
|
|
|
|
getInsts: insts,
|
|
|
|
|
emitHook: emitHook,
|
|
|
|
|
bindHook: bindHook
|
2017-07-26 22:03:18 +08:00
|
|
|
|
};
|
|
|
|
|
if (mail) r.mail = mail;
|
2017-07-26 11:26:28 +08:00
|
|
|
|
module.exports = r;
|