yapi/server/router.js

297 lines
5.0 KiB
JavaScript
Raw Normal View History

import koaRouter from 'koa-router';
import interfaceController from './controllers/interface.js';
import groupController from './controllers/group.js';
import userController from './controllers/user.js';
2017-08-09 16:16:35 +08:00
import interfaceColController from './controllers/interfaceCol.js'
2017-07-03 16:16:05 +08:00
import yapi from './yapi.js';
import projectController from './controllers/project.js';
import logController from './controllers/log.js';
2017-07-03 16:16:05 +08:00
const router = koaRouter();
2017-07-03 16:16:05 +08:00
2017-08-08 14:45:19 +08:00
const authLevel = {
admin: 0,
owner: 10,
dev: 20,
member:30,
guest:100
}
const INTERFACE_CONFIG = {
interface: {
prefix: '/interface/',
controller: interfaceController
},
user: {
prefix: '/user/',
2017-07-06 18:25:53 +08:00
controller: userController
},
group: {
prefix: '/group/',
controller: groupController
2017-07-06 19:21:54 +08:00
},
project: {
prefix: '/project/',
controller: projectController
},
2017-07-20 20:30:24 +08:00
log: {
prefix: '/log/',
controller: logController
2017-08-09 16:16:35 +08:00
},
col: {
prefix: '/col/',
controller: interfaceColController
}
2017-07-03 16:16:05 +08:00
};
2017-08-08 14:45:19 +08:00
const routerConfig = {
"group": [
{
"action": "list",
"path": "list",
"method": "get"
},
{
"action": "add",
"path": "add",
"method": "post"
},
{
"action": "up",
"path": "up",
"method": "post"
},
{
"action": "del",
"path": "del",
"method": "post"
},
{
"action": "addMember",
"path": "add_member",
"method": "post"
},
{
"action": "delMember",
"path": "del_member",
"method": "post"
},
{
"action": "getMemberList",
"path": "members",
"method": "get"
}
],
"user": [
{
"action": "login",
"path": "login",
"method": "post"
},
{
"action": "reg",
"path": "reg",
"method": "post"
},
{
"action": "list",
"path": "list",
"method": "get"
},
{
"action": "findById",
"path": "find",
"method": "get"
},
{
"action": "update",
"path": "update",
"method": "post"
},
{
"action": "del",
"path": "del",
"method": "post"
},
{
"action": "getLoginStatus",
"path": "status",
"method": "get"
},
{
"action": "logout",
"path": "logout",
"method": "get"
},
{
"action": "loginByToken",
"path": "login_by_token",
"method": "post"
},
{
"action": "changePassword",
"path": "change_password",
"method": "post"
},
{
"action": "search",
"path": "search",
"method": "get"
},
{
"action": "nav",
"path": "nav",
"method": "get"
}
],
"project": [
{
"action": "add",
"path": "add",
"method": "post"
},
{
"action": "list",
"path": "list",
"method": "get"
},
{
"action": "get",
"path": "get",
"method": "get"
},
{
"action": "up",
"path": "up",
"method": "post"
},
{
"action": "del",
"path": "del",
"method": "post"
},
{
"action": "addMember",
"path": "add_member",
"method": "post"
},
{
"action": "delMember",
"path": "del_member",
"method": "post"
},
{
"action": "getMemberList",
"path": "get_member_list",
"method": "get"
},
{
"action": "search",
"path": "search",
"method": "get"
}
],
"interface": [
{
"action": "add",
"path": "add",
"method": "post"
},
{
"action": "list",
"path": "list",
"method": "get"
},
{
"action": "get",
"path": "get",
"method": "get"
},
{
"action": "up",
"path": "up",
"method": "post"
},
{
"action": "del",
"path": "del",
"method": "post"
}
],
"log": [
{
"action": "list",
"path": "list",
"method": "get"
}
2017-08-09 16:16:35 +08:00
],
"col": [{
2017-08-09 20:01:51 +08:00
action: "addCol",
path: "add_col",
2017-08-09 16:16:35 +08:00
method: "post"
}, {
action: "list",
path: "list",
method: "get"
2017-08-09 20:01:51 +08:00
},{
action: "getCaseList",
path: "case_list",
method: "get"
},{
action: "addCase",
path: "add_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: "post"
},{
action: "delCase",
path: "del_case",
method: "post"
}
]
2017-08-08 14:45:19 +08:00
}
2017-07-06 18:25:53 +08:00
2017-08-08 14:45:19 +08:00
for(let ctrl in routerConfig){
let actions = routerConfig[ctrl];
actions.forEach( (item) => {
createAction(ctrl, item.action, item.path, item.method);
} )
}
/**
2017-07-06 01:09:33 +08:00
*
* @param {*} controller controller_name
* @param {*} path request_path
* @param {*} method request_method , post get put delete ...
* @param {*} action controller_action_name
*/
2017-08-08 14:45:19 +08:00
function createAction(controller, action, path, method) {
router[method](INTERFACE_CONFIG[controller].prefix + path, async (ctx) => {
2017-07-10 11:59:13 +08:00
let inst = new INTERFACE_CONFIG[controller].controller(ctx);
2017-07-11 12:12:43 +08:00
await inst.init(ctx);
if (inst.$auth === true) {
2017-07-11 12:12:43 +08:00
await inst[action].call(inst, ctx);
} else {
2017-08-09 20:01:51 +08:00
ctx.body = yapi.commons.resReturn(null, 40011, '请登录.');
2017-07-11 12:12:43 +08:00
}
});
2017-07-06 01:09:33 +08:00
}
module.exports = router;