| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- const yapi = require('./yapi.js');
- const plugin_path = yapi.path.join(yapi.WEBROOT, 'node_modules');
- const plugin_system_path = yapi.path.join(yapi.WEBROOT, 'exts');
- const initPlugins = require('../common/plugin.js').initPlugins;
- var extConfig = require('../common/config.js').exts;
- /**
- * 钩子配置
- */
- var hooks = {
- /**
- * 第三方sso登录钩子,暂只支持设置一个
- * @param ctx
- * @return 必需返回一个 promise 对象,resolve({username: '', email: ''})
- */
- third_login: {
- type: 'single',
- listener: null
- },
- /**
- * 客户端增加接口成功后触发
- * @param data 接口的详细信息
- */
- interface_add: {
- type: 'multi',
- listener: []
- },
- /**
- * 客户端删除接口成功后触发
- * @param data 接口id
- */
- interface_del: {
- type: 'multi',
- listener: []
- },
- /**
- * 客户端更新接口成功后触发
- * @param id 接口id
- */
- interface_update: {
- type: 'multi',
- listener: []
- },
- /**
- * 客户端获取接口数据列表
- * @param list 返回接口的数据列表
- */
- interface_list: {
- type: 'multi',
- listener: []
- },
- /**
- * 客户端获取一条接口信息触发
- * @param data 接口的详细信息
- */
- interface_get: {
- type: 'multi',
- listener: []
- },
- /**
- * 客户端增加一个新项目
- * @param id 项目id
- */
- project_add: {
- type: 'multi',
- listener: []
- },
- /**
- * 客户端更新一个新项目
- * @param id 项目id
- */
- project_up: {
- type: 'multi',
- listener: []
- },
- /**
- * 客户端获取一个项目
- * @param id 项目id
- */
- project_get: {
- type: 'multi',
- listener: []
- },
- /**
- * 客户端删除删除一个项目
- * @param id 项目id
- */
- project_del: {
- type: 'multi',
- listener: []
- },
- /**
- * 导出 markdown 数据
- * @param context Object
- * {
- * projectData: project,
- interfaceData: interfaceData,
- ctx: ctx,
- mockJson: res
- * }
- *
- */
- export_markdown: {
- type: 'multi',
- listener: []
- },
- /**
- * MockServer生成mock数据后触发
- * @param context Object
- * {
- * projectData: project,
- interfaceData: interfaceData,
- ctx: ctx,
- mockJson: res
- * }
- *
- */
- mock_after: {
- type: 'multi',
- listener: []
- },
- /**
- * 增加路由的钩子
- * type Sync
- * @param addPluginRouter Function
- * @info
- * addPLuginPLugin(config)
- *
- * config = {
- * path, // String 路由名称
- * method, // String 请求方法 get post ...
- * controller // Class 继承baseController的class
- * action // String controller的Action
- * }
- *
- * 示例:
- * config = {
- * path: "export/pdf",
- * method: "get",
- * controller: controller,
- * action: "exportPdf"
- * }
- */
- add_router: {
- type: 'multi',
- listener: []
- },
- /**
- * 增加websocket路由的钩子
- * type Sync
- * @param addPluginRouter Function
- * @info
- * addPLuginPLugin(config)
- *
- * config = {
- * path, // String 路由名称
- * method, // String 请求方法 get post ...
- * controller // Class 继承baseController的class
- * action // String controller的Action
- * }
- *
- * 示例:
- * config = {
- * path: "export/pdf",
- * method: "get",
- * controller: controller,
- * action: "exportPdf"
- * }
- */
- add_ws_router: {
- type: 'multi',
- listener: []
- },
- import_data: {
- type: 'multi',
- listener: []
- },
- /**
- * addNoticePlugin(config)
- *
- * config.weixin = {
- * title: 'wechat',
- * hander: (emails, title, content)=> {...}
- * }
- */
- addNotice:{
- type: 'multi',
- 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 {
- if (typeof hooks[name].listener === 'function') {
- throw new Error('重复绑定singleHook(' + name + '), 请检查');
- }
- hooks[name].listener = listener;
- }
- }
- /**
- *
- * @param {*} hookname
- * @return promise
- */
- function emitHook(name) {
- if (hooks[name] && typeof hooks[name] === 'object') {
- let args = Array.prototype.slice.call(arguments, 1);
- if (hooks[name].type === 'single' && typeof hooks[name].listener === 'function') {
- return Promise.resolve(hooks[name].listener.apply(yapi, args));
- }
- let promiseAll = [];
- if (Array.isArray(hooks[name].listener)) {
- let listenerList = hooks[name].listener;
- for (let i = 0, l = listenerList.length; i < l; i++) {
- promiseAll.push(Promise.resolve(listenerList[i].apply(yapi, args)));
- }
- }
- return Promise.all(promiseAll);
- }
- }
- yapi.bindHook = bindHook;
- yapi.emitHook = emitHook;
- yapi.emitHookSync = emitHook;
- let pluginsConfig = initPlugins(yapi.WEBCONFIG.plugins, 'plugin');
- pluginsConfig.forEach(plugin => {
- if (!plugin || plugin.enable === false || plugin.server === false) return null;
- if (
- !yapi.commons.fileExist(
- yapi.path.join(plugin_path, 'yapi-plugin-' + plugin.name + '/server.js')
- )
- ) {
- throw new Error(`config.json配置了插件${plugin},但plugins目录没有找到此插件,请安装此插件`);
- }
- let pluginModule = require(yapi.path.join(
- plugin_path,
- 'yapi-plugin-' + plugin.name + '/server.js'
- ));
- pluginModule.call(yapi, plugin.options);
- });
- extConfig = initPlugins(extConfig, 'ext');
- extConfig.forEach(plugin => {
- if (!plugin || plugin.enable === false || plugin.server === false) return null;
- if (
- !yapi.commons.fileExist(
- yapi.path.join(plugin_system_path, 'yapi-plugin-' + plugin.name + '/server.js')
- )
- ) {
- throw new Error(`config.json配置了插件${plugin},但plugins目录没有找到此插件,请安装此插件`);
- }
- let pluginModule = require(yapi.path.join(
- plugin_system_path,
- 'yapi-plugin-' + plugin.name + '/server.js'
- ));
- pluginModule.call(yapi, plugin.options);
- });
- //delete bindHook方法,避免误操作
- delete yapi.bindHook;
|