yapi/server_dist/router.js

394 lines
7.1 KiB
JavaScript
Raw Normal View History

2017-07-18 18:10:55 +08:00
'use strict';
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _koaRouter = require('koa-router');
var _koaRouter2 = _interopRequireDefault(_koaRouter);
var _interface = require('./controllers/interface.js');
var _interface2 = _interopRequireDefault(_interface);
var _group = require('./controllers/group.js');
var _group2 = _interopRequireDefault(_group);
var _user = require('./controllers/user.js');
var _user2 = _interopRequireDefault(_user);
2017-08-09 16:16:35 +08:00
var _interfaceCol = require('./controllers/interfaceCol.js');
var _interfaceCol2 = _interopRequireDefault(_interfaceCol);
2017-07-18 18:10:55 +08:00
var _yapi = require('./yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _project = require('./controllers/project.js');
var _project2 = _interopRequireDefault(_project);
2017-07-20 20:30:24 +08:00
var _log = require('./controllers/log.js');
2017-07-20 20:30:24 +08:00
var _log2 = _interopRequireDefault(_log);
2017-08-10 12:13:32 +08:00
var _follow = require('./controllers/follow.js');
var _follow2 = _interopRequireDefault(_follow);
2017-07-18 18:10:55 +08:00
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var router = (0, _koaRouter2.default)();
2017-08-08 14:45:19 +08:00
var authLevel = {
admin: 0,
owner: 10,
dev: 20,
member: 30,
guest: 100
};
2017-07-18 18:10:55 +08:00
var INTERFACE_CONFIG = {
2017-08-08 14:45:19 +08:00
interface: {
prefix: '/interface/',
controller: _interface2.default
},
user: {
prefix: '/user/',
controller: _user2.default
},
group: {
prefix: '/group/',
controller: _group2.default
},
project: {
prefix: '/project/',
controller: _project2.default
},
log: {
prefix: '/log/',
controller: _log2.default
2017-08-09 16:16:35 +08:00
},
2017-08-10 12:13:32 +08:00
follow: {
prefix: '/follow/',
controller: _follow2.default
},
2017-08-09 16:16:35 +08:00
col: {
prefix: '/col/',
controller: _interfaceCol2.default
2017-08-08 14:45:19 +08:00
}
};
var 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"
2017-08-15 17:55:52 +08:00
}, {
"action": "changeMemberRole",
"path": "change_member_role",
"method": "post"
2017-08-08 14:45:19 +08:00
}, {
"action": "delMember",
"path": "del_member",
"method": "post"
}, {
"action": "getMemberList",
2017-08-15 17:55:52 +08:00
"path": "get_member_list",
2017-08-08 14:45:19 +08:00
"method": "get"
2017-08-16 17:13:14 +08:00
}, {
action: 'get',
path: 'get',
method: 'get'
2017-08-08 14:45:19 +08:00
}],
"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": "project",
"path": "project",
2017-08-08 14:45:19 +08:00
"method": "get"
2017-08-10 15:22:42 +08:00
}, {
"action": "avatar",
"path": "avatar",
"method": "get"
}, {
action: "uploadAvatar",
path: "upload_avatar",
method: "post"
2017-08-08 14:45:19 +08:00
}],
"project": [{
2017-08-22 11:23:00 +08:00
"action": "upSet",
"path": "upset",
"method": "post"
}, {
2017-08-08 14:45:19 +08:00
"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"
2017-08-18 17:08:37 +08:00
}, {
"action": "changeMemberRole",
"path": "change_member_role",
"method": "post"
2017-08-08 14:45:19 +08:00
}, {
"action": "getMemberList",
"path": "get_member_list",
"method": "get"
}, {
"action": "search",
"path": "search",
"method": "get"
2017-08-10 10:24:24 +08:00
}, {
"action": "download",
"path": "download",
"method": "get"
2017-08-08 14:45:19 +08:00
}],
"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"
2017-08-18 20:35:31 +08:00
}, {
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'
2017-08-08 14:45:19 +08:00
}],
"log": [{
"action": "list",
"path": "list",
"method": "get"
2017-08-09 16:16:35 +08:00
}],
2017-08-10 12:13:32 +08:00
"follow": [{
"action": "list",
"path": "list",
"method": "get"
}, {
"action": "add",
"path": "add",
"method": "post"
}, {
"action": "del",
"path": "del",
"method": "post"
}],
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",
2017-08-22 18:05:20 +08:00
method: "get"
2017-08-09 20:01:51 +08:00
}, {
action: "delCase",
path: "del_case",
2017-08-22 18:05:20 +08:00
method: "get"
2017-08-08 14:45:19 +08:00
}]
2017-07-18 18:10:55 +08:00
};
2017-08-08 14:45:19 +08:00
var _loop = function _loop(ctrl) {
var actions = routerConfig[ctrl];
actions.forEach(function (item) {
createAction(ctrl, item.action, item.path, item.method);
});
};
for (var ctrl in routerConfig) {
_loop(ctrl);
}
2017-07-18 18:10:55 +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) {
var _this = this;
router[method]("/api" + INTERFACE_CONFIG[controller].prefix + path, function () {
2017-08-22 18:05:20 +08:00
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
2017-08-08 14:45:19 +08:00
var inst;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
inst = new INTERFACE_CONFIG[controller].controller(ctx);
_context.next = 3;
return inst.init(ctx);
case 3:
if (!(inst.$auth === true)) {
_context.next = 8;
break;
}
_context.next = 6;
return inst[action].call(inst, ctx);
case 6:
_context.next = 9;
break;
case 8:
2017-08-15 12:08:59 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 40011, '请登录...');
2017-08-08 14:45:19 +08:00
case 9:
case 'end':
return _context.stop();
}
}
}, _callee, _this);
}));
return function (_x) {
return _ref.apply(this, arguments);
};
}());
2017-07-18 18:10:55 +08:00
}
module.exports = router;