From d7f560351900bb905a8f62564ba341e674c42ca7 Mon Sep 17 00:00:00 2001 From: wnstar Date: Wed, 15 Nov 2017 12:37:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AAwebs?= =?UTF-8?q?ocket=E8=B7=AF=E7=94=B1=E9=92=A9=E5=AD=90=20=E9=92=A9=E5=AD=90?= =?UTF-8?q?=E5=90=8Dadd=5Fws=5Frouter=20=E5=8F=82=E6=95=B0=E5=90=8C=20add?= =?UTF-8?q?=5Frouter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/plugin.js | 26 +++++++ server/router.js | 147 +++++++++++++++++----------------------- server/utils/commons.js | 36 +++++++++- server/websocket.js | 36 +++++++--- 4 files changed, 147 insertions(+), 98 deletions(-) diff --git a/server/plugin.js b/server/plugin.js index ce2cdf17..2303b2e2 100755 --- a/server/plugin.js +++ b/server/plugin.js @@ -128,6 +128,32 @@ var hooks = { 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: [] } }; diff --git a/server/router.js b/server/router.js index 0a605fbf..954320da 100755 --- a/server/router.js +++ b/server/router.js @@ -9,41 +9,42 @@ const yapi = require('./yapi.js'); const projectController = require('./controllers/project.js'); const logController = require('./controllers/log.js'); const followController = require('./controllers/follow.js'); +const { createAction } = require("./utils/commons.js") const router = koaRouter(); const authLevel = { - admin: 0, - owner: 10, - dev: 20, - member:30, - guest:100 + admin: 0, + owner: 10, + dev: 20, + member: 30, + guest: 100 } let INTERFACE_CONFIG = { - interface: { - prefix: '/interface/', - controller: interfaceController - }, - user: { - prefix: '/user/', - controller: userController - }, - group: { - prefix: '/group/', - controller: groupController - }, - project: { - prefix: '/project/', - controller: projectController - }, - log: { - prefix: '/log/', - controller: logController + interface: { + prefix: '/interface/', + controller: interfaceController + }, + user: { + prefix: '/user/', + controller: userController + }, + group: { + prefix: '/group/', + controller: groupController + }, + project: { + prefix: '/project/', + controller: projectController + }, + log: { + prefix: '/log/', + controller: logController }, follow: { - prefix: '/follow/', - controller: followController + prefix: '/follow/', + controller: followController }, col: { prefix: '/col/', @@ -96,7 +97,7 @@ let routerConfig = { "action": "getMemberList", "path": "get_member_list", "method": "get" - },{ + }, { action: 'get', path: 'get', method: 'get' @@ -167,11 +168,11 @@ let routerConfig = { "action": "project", "path": "project", "method": "get" - },{ + }, { "action": "avatar", "path": "avatar", "method": "get" - },{ + }, { action: "uploadAvatar", path: "upload_avatar", method: "post" @@ -182,7 +183,7 @@ let routerConfig = { "action": "upSet", "path": "upset", "method": "post" - },{ + }, { "action": "add", "path": "add", "method": "post" @@ -246,7 +247,7 @@ let routerConfig = { }, { "action": "downloadCrx", - "path" : "download_crx", + "path": "download_crx", "method": "get" }, { @@ -283,19 +284,19 @@ let routerConfig = { action: 'listByCat', path: 'list_cat', method: 'get' - },{ + }, { action: 'listByMenu', path: 'list_menu', method: 'get' - },{ + }, { action: 'addCat', path: 'add_cat', method: 'post' - },{ + }, { action: 'upCat', path: 'up_cat', method: 'post' - },{ + }, { action: 'delCat', path: 'del_cat', method: 'post' @@ -312,11 +313,11 @@ let routerConfig = { "action": "list", "path": "list", "method": "get" - },{ + }, { "action": "add", "path": "add", "method": "post" - },{ + }, { "action": "del", "path": "del", "method": "post" @@ -325,48 +326,48 @@ let routerConfig = { action: "addCol", path: "add_col", method: "post" - },{ + }, { action: 'addCaseList', path: 'add_case_list', - method: 'post' - + method: 'post' + }, { action: "list", path: "list", method: "get" - },{ + }, { action: "getCaseList", path: "case_list", method: "get" - },{ + }, { action: "addCase", path: "add_case", method: "post" - },{ + }, { action: "upCase", path: "up_case", method: "post" - },{ + }, { action: "getCase", path: "case", method: "get" - },{ + }, { action: "upCol", path: "up_col", method: "post" - },{ + }, { action: "upCaseIndex", path: "up_col_index", method: "post" - },{ + }, { action: "delCol", path: "del_col", method: "get" - },{ + }, { action: "delCase", path: "del_case", method: "get" - },{ + }, { action: "runCaseScript", path: "run_script", method: "post" @@ -413,56 +414,30 @@ let routerConfig = { let pluginsRouterPath = []; -function addPluginRouter(config){ - if(!config.path || !config.controller || !config.action){ +function addPluginRouter(config) { + if (!config.path || !config.controller || !config.action) { throw new Error('Plugin Route config Error'); } let method = config.method || 'GET'; let routerPath = '/plugin/' + config.path; - if(pluginsRouterPath.indexOf(routerPath) > -1){ + if (pluginsRouterPath.indexOf(routerPath) > -1) { throw new Error('Plugin Route path conflict, please try rename the path') } pluginsRouterPath.push(routerPath); - createAction(config.controller, config.action, routerPath, method); + createAction(router, "/api", config.controller, config.action, routerPath, method,false); } yapi.emitHookSync('add_router', addPluginRouter); -for(let ctrl in routerConfig){ - let actions = routerConfig[ctrl]; - actions.forEach( (item) => { - let routerController = INTERFACE_CONFIG[ctrl].controller; - let routerPath = INTERFACE_CONFIG[ctrl].prefix + item.path; - createAction(routerController, item.action, routerPath, item.method); - }) +for (let ctrl in routerConfig) { + let actions = routerConfig[ctrl]; + actions.forEach((item) => { + let routerController = INTERFACE_CONFIG[ctrl].controller; + let routerPath = INTERFACE_CONFIG[ctrl].prefix + item.path; + createAction(router, "/api", routerController, item.action, routerPath, item.method); + }) } -/** - * - * @param {*} routerController controller - * @param {*} path routerPath - * @param {*} method request_method , post get put delete ... - * @param {*} action controller action_name - */ -function createAction(routerController, action, path, method) { - router[method]("/api" + path, async (ctx) => { - let inst = new routerController(ctx); - try{ - await inst.init(ctx); - - if (inst.$auth === true) { - await inst[action].call(inst, ctx); - } else { - ctx.body = yapi.commons.resReturn(null, 40011, '请登录...'); - } - }catch(err){ - ctx.body = yapi.commons.resReturn(null, 40011, '服务器出错...'); - yapi.commons.log(err, 'error') - } - - }); -} - module.exports = router; diff --git a/server/utils/commons.js b/server/utils/commons.js index 80fed6cf..ceb0044a 100755 --- a/server/utils/commons.js +++ b/server/utils/commons.js @@ -243,7 +243,7 @@ exports.handleParams = (params, keys) => { switch (filter) { case 'string': params[key] = trim(params[key] + ''); break; - case 'number': params[key] =!isNaN(params[key])? parseInt(params[key], 10) : 0; + case 'number': params[key] = !isNaN(params[key]) ? parseInt(params[key], 10) : 0; break; default: params[key] = trim(params + ''); } @@ -271,3 +271,37 @@ exports.saveLog = (logData) => { yapi.commons.log(e, 'error'); // eslint-disable-line } }; + + +/** + * + * @param {*} router router + * @param {*} baseurl base_url_path + * @param {*} routerController controller + * @param {*} path routerPath + * @param {*} method request_method , post get put delete ... + * @param {*} action controller action_name + * @param {*} ws enable ws + */ +exports.createAction = (router, baseurl, routerController, action, path, method, ws) => { + router[method](baseurl + path, async (ctx) => { + let inst = new routerController(ctx); + try { + await inst.init(ctx); + + if (inst.$auth === true) { + await inst[action].call(inst, ctx); + } else { + if (ws === true) { + ctx.ws.send('请登录...'); + } else { + ctx.body = yapi.commons.resReturn(null, 40011, '请登录...'); + } + } + } catch (err) { + ctx.body = yapi.commons.resReturn(null, 40011, '服务器出错...'); + yapi.commons.log(err, 'error') + } + + }); +} \ No newline at end of file diff --git a/server/websocket.js b/server/websocket.js index ddd578d2..df9a668d 100755 --- a/server/websocket.js +++ b/server/websocket.js @@ -1,19 +1,33 @@ const koaRouter = require('koa-router'); const interfaceController = require('./controllers/interface.js'); -const router = koaRouter(); +const yapi = require('./yapi.js'); +const router = koaRouter(); +const { createAction } = require("./utils/commons.js") + +let pluginsRouterPath = []; + + +function addPluginRouter(config) { + if (!config.path || !config.controller || !config.action) { + throw new Error('Plugin Route config Error'); + } + let method = config.method || 'GET'; + let routerPath = '/ws_plugin/' + config.path; + if (pluginsRouterPath.indexOf(routerPath) > -1) { + throw new Error('Plugin Route path conflict, please try rename the path') + } + pluginsRouterPath.push(routerPath); + createAction(router, "/api", config.controller, config.action, routerPath, method, true); +} function websocket(app) { console.log('load websocket...') - - router.get('/api/interface/solve_conflict', async function (ctx) { - let inst = new interfaceController(ctx); - await inst.init(ctx); - if (inst.$auth === true) { - await inst.solveConflict.call(inst, ctx); - } else { - ctx.ws.send('请登录...'); - } - }) + createAction(router, "/api", interfaceController, "solveConflict", "/interface/solve_conflict", "get") + + yapi.emitHookSync('add_ws_router', addPluginRouter); + + + app.ws.use(router.routes()) app.ws.use(router.allowedMethods());