From 28040a9096432a64226201e429f5eccd29f8137b Mon Sep 17 00:00:00 2001 From: suxiaoxin Date: Mon, 25 Sep 2017 00:07:23 +0800 Subject: [PATCH] opti: plugin --- server/controllers/interface.js | 5 ++- server/controllers/project.js | 2 + server/plugin.js | 76 ++++++++++++++++++++++++++++++--- static/lib/qsso-auth.js | 73 ------------------------------- 4 files changed, 77 insertions(+), 79 deletions(-) delete mode 100755 static/lib/qsso-auth.js diff --git a/server/controllers/interface.js b/server/controllers/interface.js index 61329754..610f865b 100755 --- a/server/controllers/interface.js +++ b/server/controllers/interface.js @@ -155,7 +155,7 @@ class interfaceController extends baseController { } let result = await this.Model.save(data); - + yapi.emitHook('interface_add', result._id).then(); this.catModel.get(params.catid).then((cate) => { let username = this.getUsername(); let title = `用户 "${username}" 为分类 "${cate.name}" 添加了接口 "${data.title}"` @@ -209,6 +209,7 @@ class interfaceController extends baseController { return ctx.body = yapi.commons.resReturn(null, 406, '没有权限'); } } + yapi.emitHook('interface_get', params.id).then(); ctx.body = yapi.commons.resReturn(result); } catch (e) { ctx.body = yapi.commons.resReturn(null, 402, e.message); @@ -243,6 +244,7 @@ class interfaceController extends baseController { try { let result = await this.Model.list(project_id); ctx.body = yapi.commons.resReturn(result); + yapi.emitHook('interface_list', project_id).then(); } catch (err) { ctx.body = yapi.commons.resReturn(null, 402, err.message); } @@ -483,6 +485,7 @@ class interfaceController extends baseController { } ctx.body = yapi.commons.resReturn(result); + yapi.emitHook('interface_update', id).then(); } catch (e) { ctx.body = yapi.commons.resReturn(null, 402, e.message); } diff --git a/server/controllers/project.js b/server/controllers/project.js index 17e40fb6..12cc63ce 100755 --- a/server/controllers/project.js +++ b/server/controllers/project.js @@ -318,6 +318,8 @@ class projectController extends baseController { let cat = await catInst.list(params.id); result.cat = cat; result.role = await this.getProjectRole(params.id, 'project'); + + yapi.emitHook('project_add', params.id).then(); ctx.body = yapi.commons.resReturn(result); } catch (e) { ctx.body = yapi.commons.resReturn(null, 402, e.message); diff --git a/server/plugin.js b/server/plugin.js index 4c1fccce..5a6dacdf 100755 --- a/server/plugin.js +++ b/server/plugin.js @@ -3,36 +3,102 @@ const plugin_path = yapi.path.join(yapi.WEBROOT, 'plugins'); const plugin_system_path = yapi.path.join(yapi.WEBROOT, 'exts'); const initPlugins = require('../common/lib.js').initPlugins; var extConfig = require('../common/config.js').exts; + +/** + * 钩子配置 + */ var hooks = { - //第三方sso登录钩子,暂只支持设置一个 + /** + * 第三方sso登录钩子,暂只支持设置一个 + * @param ctx + */ 'third_login': { type: 'single', listener: null }, - //增加接口成功后触发 + /** + * 客户端增加接口成功后触发 + * @param id 接口id + */ 'interface_add': { type: 'multi', listener: [] }, - //删除接口成功后触发 + /** + * 客户端删除接口成功后触发 + * @param id 接口id + */ 'interface_del': { type: 'multi', listener: [] }, + /** + * 客户端更新接口成功后触发 + * @param id 接口id + */ + 'interface_update':{ + type: 'multi', + listener: [] + }, + /** + * 客户端获取接口数据列表 + * @param id project_id + */ + 'interface_list':{ + type: 'multi', + listener: [] + }, + /** + * 客户端获取一条接口信息触发 + * @param id 接口id + */ + 'interface_get':{ + type: 'multi', + listener: [] + }, + /** + * 客户端增加一个新项目 + * @param id 项目id + */ 'project_add':{ type: 'multi', listener: [] }, + /** + * 客户端删除删除一个项目 + * @param id 项目id + */ 'project_del':{ type: 'multi', listener: [] }, - //MockServer生成mock数据后触发 + /** + * MockServer生成mock数据后触发 + * @param context Object + * { + * projectData: project, + interfaceData: interfaceData, + ctx: ctx, + mockJson: res + * } + * + */ mock_after: { type: 'multi', listener: [] }, - //增加路由的钩子 + /** + * 增加路由的钩子 + * type Sync + * @param addPluginRouter Function + * addPLuginPLugin(config) + * config = { + * path, // String + * method, // String + * controller // Class 继承baseController的class + * action // String controller的Action + * } + */ add_router: { type: 'multi', listener: [] diff --git a/static/lib/qsso-auth.js b/static/lib/qsso-auth.js deleted file mode 100755 index becd4e62..00000000 --- a/static/lib/qsso-auth.js +++ /dev/null @@ -1,73 +0,0 @@ -// file_id:2D7ABF69-3BC0-4175-98C9-5C3D5CB00158 -- nerver change this !! - -/* - * file: qsso-auth.js - * URL: https://qsso.corp.qunar.com/lib/qsso-auth.js - * written by zhibin.ning - * version: 0.1 - * - */ - - -if (!window['QSSO']) { -var QSSO = (function () { - - var AUTH_SERVER = 'https://qsso.corp.qunar.com', - LOGIN_PAGE = '/login.php', - SORRY_PAGE = '/sorry.html'; - - if (location.hostname.match(/qunar\.ctripgroup\.com$/i)) { - AUTH_SERVER = 'https://qunar.ctripgroup.com/sec/qsso/api'; - } - - var qualifyURL = function (url, encode) { - var url = url || ''; - var ret = location.protocol + '//' + location.host + (url.substr(0,1) === '/' ? '' : location.pathname.match(/.*\//)) + url; - if (encode) { - ret = encodeURIComponent(ret); - } - return ret; - }; - - var URLStringify = function (o) { - var ret = []; - for (var i in o) { - // ret.push( encodeURIComponent(i) + '=' + encodeURIComponent(o[i]) ); - ret.push(i + '=' + o[i]); - } - return ret.join('&'); - }; - - return { - 'auth': function (loginURI, opt_ext) { - if (!location.hostname.match(/\.qunar(man|ops)?\.com$|\.qunarman\.com$|qunar\.it$|\.928383\.com$|^928383\.com$|qunar\.ctripgroup\.c(om|n)$|\.ctrip(corp)?\.com$|^opsdata\.me$|\.mofun\.com$/i)) { - location = AUTH_SERVER + SORRY_PAGE + '?host=' + qualifyURL('', true); - return; - } - var ret = qualifyURL(loginURI, true); - - var redirectURL = AUTH_SERVER + LOGIN_PAGE + '?ret=' + ret + (opt_ext ? '&ext=' + encodeURIComponent(URLStringify(opt_ext)) : ''); - // console.log(redirectURL); - location = redirectURL; - }, - - 'attach': function (eid, loginURI, opt_ext) { - QSSO.login = function() { - QSSO.auth(loginURI, opt_ext); - - }; - document.getElementById(eid).onclick = QSSO.login; - if (location.hash.match('qsso-auto-login')) { - QSSO.login(); - } - } - }; - - -})(); - - -/* todo add QSSO login UI, frame login, etc */ - - -}