fix: 增加一个websocket路由钩子 钩子名add_ws_router 参数同 add_router

This commit is contained in:
wnstar 2017-11-15 12:37:23 +08:00
parent c161b91f83
commit d7f5603519
4 changed files with 147 additions and 98 deletions

View File

@ -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: []
}
};

View File

@ -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;

View File

@ -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')
}
});
}

View File

@ -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());