mock平台

plugin.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. const yapi = require('./yapi.js');
  2. const plugin_path = yapi.path.join(yapi.WEBROOT, 'node_modules');
  3. const plugin_system_path = yapi.path.join(yapi.WEBROOT, 'exts');
  4. const initPlugins = require('../common/plugin.js').initPlugins;
  5. var extConfig = require('../common/config.js').exts;
  6. /**
  7. * 钩子配置
  8. */
  9. var hooks = {
  10. /**
  11. * 第三方sso登录钩子,暂只支持设置一个
  12. * @param ctx
  13. * @return 必需返回一个 promise 对象,resolve({username: '', email: ''})
  14. */
  15. third_login: {
  16. type: 'single',
  17. listener: null
  18. },
  19. /**
  20. * 客户端增加接口成功后触发
  21. * @param data 接口的详细信息
  22. */
  23. interface_add: {
  24. type: 'multi',
  25. listener: []
  26. },
  27. /**
  28. * 客户端删除接口成功后触发
  29. * @param data 接口id
  30. */
  31. interface_del: {
  32. type: 'multi',
  33. listener: []
  34. },
  35. /**
  36. * 客户端更新接口成功后触发
  37. * @param id 接口id
  38. */
  39. interface_update: {
  40. type: 'multi',
  41. listener: []
  42. },
  43. /**
  44. * 客户端获取接口数据列表
  45. * @param list 返回接口的数据列表
  46. */
  47. interface_list: {
  48. type: 'multi',
  49. listener: []
  50. },
  51. /**
  52. * 客户端获取一条接口信息触发
  53. * @param data 接口的详细信息
  54. */
  55. interface_get: {
  56. type: 'multi',
  57. listener: []
  58. },
  59. /**
  60. * 客户端增加一个新项目
  61. * @param id 项目id
  62. */
  63. project_add: {
  64. type: 'multi',
  65. listener: []
  66. },
  67. /**
  68. * 客户端更新一个新项目
  69. * @param id 项目id
  70. */
  71. project_up: {
  72. type: 'multi',
  73. listener: []
  74. },
  75. /**
  76. * 客户端获取一个项目
  77. * @param id 项目id
  78. */
  79. project_get: {
  80. type: 'multi',
  81. listener: []
  82. },
  83. /**
  84. * 客户端删除删除一个项目
  85. * @param id 项目id
  86. */
  87. project_del: {
  88. type: 'multi',
  89. listener: []
  90. },
  91. /**
  92. * 导出 markdown 数据
  93. * @param context Object
  94. * {
  95. * projectData: project,
  96. interfaceData: interfaceData,
  97. ctx: ctx,
  98. mockJson: res
  99. * }
  100. *
  101. */
  102. export_markdown: {
  103. type: 'multi',
  104. listener: []
  105. },
  106. /**
  107. * MockServer生成mock数据后触发
  108. * @param context Object
  109. * {
  110. * projectData: project,
  111. interfaceData: interfaceData,
  112. ctx: ctx,
  113. mockJson: res
  114. * }
  115. *
  116. */
  117. mock_after: {
  118. type: 'multi',
  119. listener: []
  120. },
  121. /**
  122. * 增加路由的钩子
  123. * type Sync
  124. * @param addPluginRouter Function
  125. * @info
  126. * addPLuginPLugin(config)
  127. *
  128. * config = {
  129. * path, // String 路由名称
  130. * method, // String 请求方法 get post ...
  131. * controller // Class 继承baseController的class
  132. * action // String controller的Action
  133. * }
  134. *
  135. * 示例:
  136. * config = {
  137. * path: "export/pdf",
  138. * method: "get",
  139. * controller: controller,
  140. * action: "exportPdf"
  141. * }
  142. */
  143. add_router: {
  144. type: 'multi',
  145. listener: []
  146. },
  147. /**
  148. * 增加websocket路由的钩子
  149. * type Sync
  150. * @param addPluginRouter Function
  151. * @info
  152. * addPLuginPLugin(config)
  153. *
  154. * config = {
  155. * path, // String 路由名称
  156. * method, // String 请求方法 get post ...
  157. * controller // Class 继承baseController的class
  158. * action // String controller的Action
  159. * }
  160. *
  161. * 示例:
  162. * config = {
  163. * path: "export/pdf",
  164. * method: "get",
  165. * controller: controller,
  166. * action: "exportPdf"
  167. * }
  168. */
  169. add_ws_router: {
  170. type: 'multi',
  171. listener: []
  172. },
  173. import_data: {
  174. type: 'multi',
  175. listener: []
  176. },
  177. /**
  178. * addNoticePlugin(config)
  179. *
  180. * config.weixin = {
  181. * title: 'wechat',
  182. * hander: (emails, title, content)=> {...}
  183. * }
  184. */
  185. addNotice:{
  186. type: 'multi',
  187. listener: []
  188. }
  189. };
  190. function bindHook(name, listener) {
  191. if (!name) throw new Error('缺少hookname');
  192. if (name in hooks === false) {
  193. throw new Error('不存在的hookname');
  194. }
  195. if (hooks[name].type === 'multi') {
  196. hooks[name].listener.push(listener);
  197. } else {
  198. if (typeof hooks[name].listener === 'function') {
  199. throw new Error('重复绑定singleHook(' + name + '), 请检查');
  200. }
  201. hooks[name].listener = listener;
  202. }
  203. }
  204. /**
  205. *
  206. * @param {*} hookname
  207. * @return promise
  208. */
  209. function emitHook(name) {
  210. if (hooks[name] && typeof hooks[name] === 'object') {
  211. let args = Array.prototype.slice.call(arguments, 1);
  212. if (hooks[name].type === 'single' && typeof hooks[name].listener === 'function') {
  213. return Promise.resolve(hooks[name].listener.apply(yapi, args));
  214. }
  215. let promiseAll = [];
  216. if (Array.isArray(hooks[name].listener)) {
  217. let listenerList = hooks[name].listener;
  218. for (let i = 0, l = listenerList.length; i < l; i++) {
  219. promiseAll.push(Promise.resolve(listenerList[i].apply(yapi, args)));
  220. }
  221. }
  222. return Promise.all(promiseAll);
  223. }
  224. }
  225. yapi.bindHook = bindHook;
  226. yapi.emitHook = emitHook;
  227. yapi.emitHookSync = emitHook;
  228. let pluginsConfig = initPlugins(yapi.WEBCONFIG.plugins, 'plugin');
  229. pluginsConfig.forEach(plugin => {
  230. if (!plugin || plugin.enable === false || plugin.server === false) return null;
  231. if (
  232. !yapi.commons.fileExist(
  233. yapi.path.join(plugin_path, 'yapi-plugin-' + plugin.name + '/server.js')
  234. )
  235. ) {
  236. throw new Error(`config.json配置了插件${plugin},但plugins目录没有找到此插件,请安装此插件`);
  237. }
  238. let pluginModule = require(yapi.path.join(
  239. plugin_path,
  240. 'yapi-plugin-' + plugin.name + '/server.js'
  241. ));
  242. pluginModule.call(yapi, plugin.options);
  243. });
  244. extConfig = initPlugins(extConfig, 'ext');
  245. extConfig.forEach(plugin => {
  246. if (!plugin || plugin.enable === false || plugin.server === false) return null;
  247. if (
  248. !yapi.commons.fileExist(
  249. yapi.path.join(plugin_system_path, 'yapi-plugin-' + plugin.name + '/server.js')
  250. )
  251. ) {
  252. throw new Error(`config.json配置了插件${plugin},但plugins目录没有找到此插件,请安装此插件`);
  253. }
  254. let pluginModule = require(yapi.path.join(
  255. plugin_system_path,
  256. 'yapi-plugin-' + plugin.name + '/server.js'
  257. ));
  258. pluginModule.call(yapi, plugin.options);
  259. });
  260. //delete bindHook方法,避免误操作
  261. delete yapi.bindHook;