fix: add server_dist/ files

This commit is contained in:
祝鑫奔 2017-07-18 18:10:55 +08:00
parent e0dcad930e
commit a57766f203
15 changed files with 2272 additions and 0 deletions

53
server_dist/app.js Normal file
View File

@ -0,0 +1,53 @@
'use strict';
var _yapi = require('./yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _commons = require('./utils/commons');
var _commons2 = _interopRequireDefault(_commons);
var _db = require('./utils/db.js');
var _db2 = _interopRequireDefault(_db);
var _mockServer = require('./middleware/mockServer.js');
var _mockServer2 = _interopRequireDefault(_mockServer);
var _koa = require('koa');
var _koa2 = _interopRequireDefault(_koa);
var _koaConvert = require('koa-convert');
var _koaConvert2 = _interopRequireDefault(_koaConvert);
var _koaStatic = require('koa-static');
var _koaStatic2 = _interopRequireDefault(_koaStatic);
var _koaBodyparser = require('koa-bodyparser');
var _koaBodyparser2 = _interopRequireDefault(_koaBodyparser);
var _router = require('./router.js');
var _router2 = _interopRequireDefault(_router);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_yapi2.default.commons = _commons2.default;
_yapi2.default.connect = _db2.default.connect();
var app = new _koa2.default();
app.use(_mockServer2.default);
app.use((0, _koaBodyparser2.default)());
app.use(_router2.default.routes());
app.use(_router2.default.allowedMethods());
app.use((0, _koaStatic2.default)(_yapi2.default.path.join(_yapi2.default.WEBROOT, 'static')));
app.listen(_yapi2.default.WEBCONFIG.port);
_commons2.default.log('the server is start at port ' + _yapi2.default.WEBCONFIG.port);

View File

@ -0,0 +1,314 @@
'use strict';
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _project = require('../models/project.js');
var _project2 = _interopRequireDefault(_project);
var _user = require('../models/user.js');
var _user2 = _interopRequireDefault(_user);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var jwt = require('jsonwebtoken');
var baseController = function () {
function baseController(ctx) {
(0, _classCallCheck3.default)(this, baseController);
this.ctx = ctx;
//网站上线后role对象key是不能修改的value可以修改
this.roles = {
admin: 'Admin',
member: '网站会员'
};
}
(0, _createClass3.default)(baseController, [{
key: 'init',
value: function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
var ignoreRouter;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
this.$user = null;
ignoreRouter = ['/user/login_by_token', '/user/login', '/user/reg', '/user/status', '/user/logout'];
if (!(ignoreRouter.indexOf(ctx.path) > -1)) {
_context.next = 6;
break;
}
this.$auth = true;
_context.next = 8;
break;
case 6:
_context.next = 8;
return this.checkLogin(ctx);
case 8:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function init(_x) {
return _ref.apply(this, arguments);
}
return init;
}()
}, {
key: 'getUid',
value: function getUid(ctx) {
return this.$uid;
}
}, {
key: 'checkLogin',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) {
var token, uid, userInst, result, decoded;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
token = ctx.cookies.get('_yapi_token');
uid = ctx.cookies.get('_yapi_uid');
_context2.prev = 2;
if (!(!token || !uid)) {
_context2.next = 5;
break;
}
return _context2.abrupt('return', false);
case 5:
userInst = _yapi2.default.getInst(_user2.default); //创建user实体
_context2.next = 8;
return userInst.findById(uid);
case 8:
result = _context2.sent;
decoded = jwt.verify(token, result.passsalt);
if (!(decoded.uid == uid)) {
_context2.next = 15;
break;
}
this.$uid = uid;
this.$auth = true;
this.$user = result;
return _context2.abrupt('return', true);
case 15:
return _context2.abrupt('return', false);
case 18:
_context2.prev = 18;
_context2.t0 = _context2['catch'](2);
return _context2.abrupt('return', false);
case 21:
case 'end':
return _context2.stop();
}
}
}, _callee2, this, [[2, 18]]);
}));
function checkLogin(_x2) {
return _ref2.apply(this, arguments);
}
return checkLogin;
}()
}, {
key: 'getLoginStatus',
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) {
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return this.checkLogin(ctx);
case 2:
_context3.t0 = _context3.sent;
if (!(_context3.t0 === true)) {
_context3.next = 5;
break;
}
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(_yapi2.default.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time'])));
case 5:
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 300, 'Please login.'));
case 6:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function getLoginStatus(_x3) {
return _ref3.apply(this, arguments);
}
return getLoginStatus;
}()
}, {
key: 'getRole',
value: function getRole() {
return this.$user.role;
}
}, {
key: 'jungeProjectAuth',
value: function () {
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(id) {
var model, result;
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
model = _yapi2.default.getInst(_project2.default);
if (!(this.getRole() === 'admin')) {
_context4.next = 3;
break;
}
return _context4.abrupt('return', true);
case 3:
if (id) {
_context4.next = 5;
break;
}
return _context4.abrupt('return', false);
case 5:
_context4.next = 7;
return model.get(id);
case 7:
result = _context4.sent;
if (!(result.uid === this.getUid())) {
_context4.next = 10;
break;
}
return _context4.abrupt('return', true);
case 10:
return _context4.abrupt('return', false);
case 11:
case 'end':
return _context4.stop();
}
}
}, _callee4, this);
}));
function jungeProjectAuth(_x4) {
return _ref4.apply(this, arguments);
}
return jungeProjectAuth;
}()
}, {
key: 'jungeMemberAuth',
value: function () {
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(id, member_uid) {
var model, result;
return _regenerator2.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
model = _yapi2.default.getInst(_project2.default);
if (!(this.getRole() === 'admin')) {
_context5.next = 3;
break;
}
return _context5.abrupt('return', true);
case 3:
if (!(!id || !member_uid)) {
_context5.next = 5;
break;
}
return _context5.abrupt('return', false);
case 5:
_context5.next = 7;
return model.checkMemberRepeat(id, member_uid);
case 7:
result = _context5.sent;
if (!(result > 0)) {
_context5.next = 10;
break;
}
return _context5.abrupt('return', true);
case 10:
return _context5.abrupt('return', false);
case 11:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));
function jungeMemberAuth(_x5, _x6) {
return _ref5.apply(this, arguments);
}
return jungeMemberAuth;
}()
}]);
return baseController;
}();
module.exports = baseController;

View File

@ -0,0 +1,341 @@
'use strict';
var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _group = require('../models/group.js');
var _group2 = _interopRequireDefault(_group);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _base = require('./base.js');
var _base2 = _interopRequireDefault(_base);
var _project = require('../models/project.js');
var _project2 = _interopRequireDefault(_project);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//
var groupController = function (_baseController) {
(0, _inherits3.default)(groupController, _baseController);
function groupController(ctx) {
(0, _classCallCheck3.default)(this, groupController);
return (0, _possibleConstructorReturn3.default)(this, (groupController.__proto__ || (0, _getPrototypeOf2.default)(groupController)).call(this, ctx));
}
/**
* 添加项目分组
* @interface /group/add
* @method POST
* @category group
* @foldnumber 10
* @param {String} group_name 项目分组名称不能为空
* @param {String} [group_desc] 项目分组描述
* @returns {Object}
* @example ./api/group/add.json
*/
(0, _createClass3.default)(groupController, [{
key: 'add',
value: function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
var params, groupInst, checkRepeat, data, result;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
params = ctx.request.body;
if (params.group_name) {
_context.next = 3;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目分组名不能为空'));
case 3:
groupInst = _yapi2.default.getInst(_group2.default);
_context.next = 6;
return groupInst.checkRepeat(params.group_name);
case 6:
checkRepeat = _context.sent;
if (!(checkRepeat > 0)) {
_context.next = 9;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '项目分组名已存在'));
case 9:
data = {
group_name: params.group_name,
group_desc: params.group_desc,
uid: '0',
add_time: _yapi2.default.commons.time(),
up_time: _yapi2.default.commons.time()
};
_context.prev = 10;
_context.next = 13;
return groupInst.save(data);
case 13:
result = _context.sent;
result = _yapi2.default.commons.fieldSelect(result, ['_id', 'group_name', 'group_desc', 'uid']);
ctx.body = _yapi2.default.commons.resReturn(result);
_context.next = 21;
break;
case 18:
_context.prev = 18;
_context.t0 = _context['catch'](10);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t0.message);
case 21:
case 'end':
return _context.stop();
}
}
}, _callee, this, [[10, 18]]);
}));
function add(_x) {
return _ref.apply(this, arguments);
}
return add;
}()
/**
* 获取项目分组列表
* @interface /group/list
* @method get
* @category group
* @foldnumber 10
* @returns {Object}
* @example ./api/group/list.json
*/
}, {
key: 'list',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) {
var groupInst, result;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
groupInst = _yapi2.default.getInst(_group2.default);
_context2.next = 4;
return groupInst.list();
case 4:
result = _context2.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context2.next = 11;
break;
case 8:
_context2.prev = 8;
_context2.t0 = _context2['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context2.t0.message);
case 11:
case 'end':
return _context2.stop();
}
}
}, _callee2, this, [[0, 8]]);
}));
function list(_x2) {
return _ref2.apply(this, arguments);
}
return list;
}()
/**
* 删除项目分组
* @interface /group/del
* @method post
* @param {String} id 项目分组id
* @category group
* @foldnumber 10
* @returns {Object}
* @example ./api/group/del.json
*/
}, {
key: 'del',
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) {
var groupInst, projectInst, id, count, result;
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.prev = 0;
groupInst = _yapi2.default.getInst(_group2.default);
projectInst = _yapi2.default.getInst(_project2.default);
id = ctx.request.body.id;
if (id) {
_context3.next = 6;
break;
}
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, 'id不能为空'));
case 6:
count = projectInst.countByGroupId(id);
if (!(count > 0)) {
_context3.next = 9;
break;
}
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 403, '请先删除该分组下的项目'));
case 9:
_context3.next = 11;
return groupInst.del(id);
case 11:
result = _context3.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context3.next = 18;
break;
case 15:
_context3.prev = 15;
_context3.t0 = _context3['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, e.message);
case 18:
case 'end':
return _context3.stop();
}
}
}, _callee3, this, [[0, 15]]);
}));
function del(_x3) {
return _ref3.apply(this, arguments);
}
return del;
}()
/**
* 更新项目分组
* @interface /group/up
* @method post
* @param {String} id 项目分组id
* @param {String} group_name 项目分组名称
* @param {String} group_desc 项目分组描述
* @category group
* @foldnumber 10
* @returns {Object}
* @example ./api/group/up.json
*/
}, {
key: 'up',
value: function () {
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(ctx) {
var groupInst, id, data, result;
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.prev = 0;
groupInst = _yapi2.default.getInst(_group2.default);
id = ctx.request.body.id;
data = {};
ctx.request.body.group_name && (data.group_name = ctx.request.body.group_name);
ctx.request.body.group_desc && (data.group_desc = ctx.request.body.group_desc);
if ((0, _keys2.default)(data).length === 0) {
ctx.body = _yapi2.default.commons.resReturn(null, 404, '分组名和分组描述不能为空');
}
_context4.next = 9;
return groupInst.up(id, data);
case 9:
result = _context4.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context4.next = 16;
break;
case 13:
_context4.prev = 13;
_context4.t0 = _context4['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, e.message);
case 16:
case 'end':
return _context4.stop();
}
}
}, _callee4, this, [[0, 13]]);
}));
function up(_x4) {
return _ref4.apply(this, arguments);
}
return up;
}()
}]);
return groupController;
}(_base2.default);
module.exports = groupController;

View File

@ -0,0 +1,506 @@
'use strict';
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _interface = require('../models/interface.js');
var _interface2 = _interopRequireDefault(_interface);
var _base = require('./base.js');
var _base2 = _interopRequireDefault(_base);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var interfaceController = function (_baseController) {
(0, _inherits3.default)(interfaceController, _baseController);
function interfaceController(ctx) {
(0, _classCallCheck3.default)(this, interfaceController);
var _this = (0, _possibleConstructorReturn3.default)(this, (interfaceController.__proto__ || (0, _getPrototypeOf2.default)(interfaceController)).call(this, ctx));
_this.Model = _yapi2.default.getInst(_interface2.default);
return _this;
}
/**
* 添加项目分组
* @interface /interface/add
* @method POST
* @category interface
* @foldnumber 10
* @param {Number} project_id 项目id不能为空
* @param {String} path 接口请求路径不能为空
* @param {String} method 请求方式
* @param {Array} [req_headers] 请求的header信息
* @param {String} [req_headers[].name] 请求的header信息名
* @param {String} [req_headers[].value] 请求的header信息值
* @param {Boolean} [req_headers[].required] 是否是必须默认为否
* @param {String} [req_headers[].desc] header描述
* @param {String} [req_params_type] 请求参数方式["form", "json", "text", "xml"]四种
* @param {Mixed} [req_params] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_params[].name] 请求参数名
* @param {String} [req_params[].value] 请求参数值可填写生成规则mock@email随机生成一条email
* @param {String} [req_params[].type] 请求参数类型["text", "file"]两种
* @param {String} [res_body_type] 相应信息的数据格式["json", "text", "xml"]三种
* @param {String} [res_body] 响应信息可填写任意字符串如果res_body_type是json,则会调用mock功能
* @param {String} [desc] 接口描述
* @returns {Object}
* @example ./api/interface/add.json
*/
(0, _createClass3.default)(interfaceController, [{
key: 'add',
value: function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
var params, checkRepeat, data, result;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
params = ctx.request.body;
params.method = params.method || 'GET';
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
if (params.project_id) {
_context.next = 5;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空'));
case 5:
if (params.path) {
_context.next = 7;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口请求路径不能为空'));
case 7:
_context.next = 9;
return this.Model.checkRepeat(params.path, params.method);
case 9:
checkRepeat = _context.sent;
if (!(checkRepeat > 0)) {
_context.next = 12;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']'));
case 12:
_context.prev = 12;
data = {
project_id: params.project_id,
path: params.path,
desc: params.desc,
method: params.method,
req_headers: params.req_headers,
req_params_type: params.req_params_type,
res_body: params.res_body,
res_body_type: params.res_body_type,
uid: this.getUid(),
add_time: _yapi2.default.commons.time(),
up_time: _yapi2.default.commons.time()
};
if (data.req_params_type === 'form') data.req_params_form = params.req_params;else data.req_params_other = params.req_params;
_context.next = 17;
return this.Model.save(data);
case 17:
result = _context.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context.next = 24;
break;
case 21:
_context.prev = 21;
_context.t0 = _context['catch'](12);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t0.message);
case 24:
case 'end':
return _context.stop();
}
}
}, _callee, this, [[12, 21]]);
}));
function add(_x) {
return _ref.apply(this, arguments);
}
return add;
}()
/**
* 添加项目分组
* @interface /interface/get
* @method GET
* @category interface
* @foldnumber 10
* @param {Number} id 接口id不能为空
* @returns {Object}
* @example ./api/interface/get.json
*/
}, {
key: 'get',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) {
var params, result;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
params = ctx.request.query;
if (params.id) {
_context2.next = 3;
break;
}
return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口id不能为空'));
case 3:
_context2.prev = 3;
_context2.next = 6;
return this.Model.get(params.id);
case 6:
result = _context2.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context2.next = 13;
break;
case 10:
_context2.prev = 10;
_context2.t0 = _context2['catch'](3);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context2.t0.message);
case 13:
case 'end':
return _context2.stop();
}
}
}, _callee2, this, [[3, 10]]);
}));
function get(_x2) {
return _ref2.apply(this, arguments);
}
return get;
}()
/**
* 添加项目分组
* @interface /interface/list
* @method GET
* @category interface
* @foldnumber 10
* @param {Number} project_id 项目id不能为空
* @returns {Object}
* @example ./api/interface/list.json
*/
}, {
key: 'list',
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) {
var project_id, result;
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
project_id = ctx.request.query.project_id;
if (project_id) {
_context3.next = 3;
break;
}
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空'));
case 3:
_context3.prev = 3;
_context3.next = 6;
return this.Model.list(project_id);
case 6:
result = _context3.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context3.next = 13;
break;
case 10:
_context3.prev = 10;
_context3.t0 = _context3['catch'](3);
ctx.body = _yapi2.default.commons.resReturn(null, 402, e.message);
case 13:
case 'end':
return _context3.stop();
}
}
}, _callee3, this, [[3, 10]]);
}));
function list(_x3) {
return _ref3.apply(this, arguments);
}
return list;
}()
/**
* 添加项目分组
* @interface /interface/up
* @method POST
* @category interface
* @foldnumber 10
* @param {Number} id 接口id不能为空
* @param {String} [path] 接口请求路径
* @param {String} [method] 请求方式
* @param {Array} [req_headers] 请求的header信息
* @param {String} [req_headers[].name] 请求的header信息名
* @param {String} [req_headers[].value] 请求的header信息值
* @param {Boolean} [req_headers[].required] 是否是必须默认为否
* @param {String} [req_headers[].desc] header描述
* @param {String} [req_params_type] 请求参数方式["form", "json", "text", "xml"]四种
* @param {Mixed} [req_params] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_params[].name] 请求参数名
* @param {String} [req_params[].value] 请求参数值可填写生成规则mock@email随机生成一条email
* @param {String} [req_params[].type] 请求参数类型["text", "file"]两种
* @param {String} [res_body_type] 相应信息的数据格式["json", "text", "xml"]三种
* @param {String} [res_body] 响应信息可填写任意字符串如果res_body_type是json,则会调用mock功能
* @param {String} [desc] 接口描述
* @returns {Object}
* @example ./api/interface/up.json
*/
}, {
key: 'up',
value: function () {
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(ctx) {
var params, id, checkRepeat, data, result;
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
params = ctx.request.body;
params.method = params.method || 'GET';
id = ctx.request.body.id;
if (id) {
_context4.next = 5;
break;
}
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口id不能为空'));
case 5:
if (!params.path) {
_context4.next = 11;
break;
}
_context4.next = 8;
return this.Model.checkRepeat(params.path, params.method);
case 8:
checkRepeat = _context4.sent;
if (!(checkRepeat > 0)) {
_context4.next = 11;
break;
}
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']'));
case 11:
data = {
up_time: _yapi2.default.commons.time()
};
if (params.path) data.path = params.path;
if (params.desc) data.desc = params.desc;
if (params.method) data.method = params.method;
if (params.req_headers) data.req_headers = params.req_headers;
if (params.req_params_type === 'form') data.req_params_form = params.req_params;else data.req_params_other = params.req_params;
if (params.res_body_type) data.res_body_type = params.res_body_type;
if (params.res_body) data.res_body = params.res_body;
_context4.prev = 19;
_context4.next = 22;
return this.Model.up(id, data);
case 22:
result = _context4.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context4.next = 29;
break;
case 26:
_context4.prev = 26;
_context4.t0 = _context4['catch'](19);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context4.t0.message);
case 29:
case 'end':
return _context4.stop();
}
}
}, _callee4, this, [[19, 26]]);
}));
function up(_x4) {
return _ref4.apply(this, arguments);
}
return up;
}()
/**
* 删除接口
* @interface /interface/del
* @method GET
* @category interface
* @foldnumber 10
* @param {Number} id 接口id不能为空
* @returns {Object}
* @example ./api/interface/del.json
*/
}, {
key: 'del',
value: function () {
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) {
var id, data, result;
return _regenerator2.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.prev = 0;
id = ctx.request.body.id;
if (id) {
_context5.next = 4;
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口id不能为空'));
case 4:
_context5.next = 6;
return this.Model.get(params.id);
case 6:
data = _context5.sent;
if (!(data.uid != this.getUid())) {
_context5.next = 13;
break;
}
_context5.next = 10;
return this.jungeProjectAuth(data.project_id);
case 10:
_context5.t0 = _context5.sent;
if (!(_context5.t0 !== true)) {
_context5.next = 13;
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限'));
case 13:
_context5.next = 15;
return this.Model.del(id);
case 15:
result = _context5.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context5.next = 22;
break;
case 19:
_context5.prev = 19;
_context5.t1 = _context5['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, e.message);
case 22:
case 'end':
return _context5.stop();
}
}
}, _callee5, this, [[0, 19]]);
}));
function del(_x5) {
return _ref5.apply(this, arguments);
}
return del;
}()
}]);
return interfaceController;
}(_base2.default);
module.exports = interfaceController;

57
server_dist/install.js Normal file
View File

@ -0,0 +1,57 @@
'use strict';
var _yapi = require('./yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _commons = require('./utils/commons');
var _commons2 = _interopRequireDefault(_commons);
var _db = require('./utils/db.js');
var _db2 = _interopRequireDefault(_db);
var _user = require('./models/user.js');
var _user2 = _interopRequireDefault(_user);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_yapi2.default.commons = _commons2.default;
_yapi2.default.connect = _db2.default.connect();
function install() {
var exist = _yapi2.default.commons.fileExist(_yapi2.default.path.join(_yapi2.default.WEBROOT_RUNTIME, 'init.lock'));
if (exist) {
return _yapi2.default.commons.log('runtime/init.lock文件已存在请确认您是否已安装。如果需要重新安装请删掉runtime/init.lock文件');
process.exit(1);
}
setupSql();
}
function setupSql() {
var userInst = _yapi2.default.getInst(_user2.default);
var passsalt = _yapi2.default.commons.randStr();
var result = userInst.save({
username: _yapi2.default.WEBCONFIG.adminAccount.substr(0, _yapi2.default.WEBCONFIG.adminAccount.indexOf('@')),
email: _yapi2.default.WEBCONFIG.adminAccount,
password: _yapi2.default.commons.generatePassword('qunar.com', passsalt),
passsalt: passsalt,
role: 'admin',
add_time: _yapi2.default.commons.time(),
up_time: _yapi2.default.commons.time()
});
result.then(function (success) {
console.log('\u521D\u59CB\u5316\u7BA1\u7406\u5458\u8D26\u53F7 "' + _yapi2.default.WEBCONFIG.adminAccount + '" \u6210\u529F');
_yapi2.default.fs.ensureFileSync(_yapi2.default.path.join(_yapi2.default.WEBROOT_RUNTIME, 'init.lock'));
process.exit(1);
}, function (err) {
console.log('\u521D\u59CB\u5316\u7BA1\u7406\u5458\u8D26\u53F7 "' + _yapi2.default.WEBCONFIG.adminAccount + '" \u5931\u8D25, ' + err.message);
process.exit(1);
});
}
install();

View File

@ -0,0 +1,36 @@
'use strict';
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var jwt = require('jsonwebtoken');
//检查token是否过期
module.exports = function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) {
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return next();
case 2:
case 'end':
return _context.stop();
}
}
}, _callee, undefined);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();

View File

@ -0,0 +1,150 @@
'use strict';
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _project2 = require('../models/project.js');
var _project3 = _interopRequireDefault(_project2);
var _interface = require('../models/interface.js');
var _interface2 = _interopRequireDefault(_interface);
var _mockjs = require('mockjs');
var _mockjs2 = _interopRequireDefault(_mockjs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
module.exports = function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) {
var hostname, config, projectInst, projects, matchProject, maxBasepath, i, l, _project, project, interfaceData, interfaceInst;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_yapi2.default.commons.log('mock Server running...');
hostname = ctx.protocol + "://" + ctx.hostname;
config = _yapi2.default.WEBCONFIG;
if (!(ctx.hostname === config.webhost)) {
_context.next = 8;
break;
}
if (!next) {
_context.next = 7;
break;
}
_context.next = 7;
return next();
case 7:
return _context.abrupt('return', true);
case 8:
projectInst = _yapi2.default.getInst(_project3.default), projects = void 0;
_context.prev = 9;
_context.next = 12;
return projectInst.getByDomain(hostname);
case 12:
projects = _context.sent;
_context.next = 18;
break;
case 15:
_context.prev = 15;
_context.t0 = _context['catch'](9);
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 403, _context.t0.message));
case 18:
matchProject = false, maxBasepath = 0;
for (i = 0, l = projects.length; i < l; i++) {
_project = projects[i];
if (ctx.path && ctx.path.indexOf(_project.basepath) === 0 && _project.basepath[_project.basepath.length - 1] === '/') {
matchProject.push(_project);
if (_project.basepath.length > maxBasepath) {
maxBasepath = _project.basepath.length;
matchProject = _project;
}
}
}
if (!(matchProject === false)) {
_context.next = 22;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '不存在的domain'));
case 22:
project = matchProject, interfaceData = void 0;
interfaceInst = _yapi2.default.getInst(_interface2.default);
_context.prev = 24;
_context.next = 27;
return interfaceInst.getByPath(project._id, ctx.path.substr(project.basepath.length));
case 27:
interfaceData = _context.sent;
if (!(!interfaceData || interfaceData.length === 0)) {
_context.next = 30;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '不存在的api'));
case 30:
if (!(interfaceData.length > 1)) {
_context.next = 32;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '存在多个api请检查数据库'));
case 32:
interfaceData = interfaceData[0];
if (!(interfaceData.res_body_type === 'json')) {
_context.next = 35;
break;
}
return _context.abrupt('return', ctx.body = _mockjs2.default.mock(_yapi2.default.commons.json_parse(interfaceData.res_body)));
case 35:
return _context.abrupt('return', ctx.body = interfaceData.res_body);
case 38:
_context.prev = 38;
_context.t1 = _context['catch'](24);
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 409, _context.t1.message));
case 41:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[9, 15], [24, 38]]);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();

View File

@ -0,0 +1,44 @@
"use strict";
var _regenerator = require("babel-runtime/regenerator");
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator");
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
module.exports = function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) {
var path;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
path = ctx.path;
console.log(path);
console.log(ctx.hostname);
if (!next) {
_context.next = 6;
break;
}
_context.next = 6;
return next();
case 6:
case "end":
return _context.stop();
}
}
}, _callee, undefined);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();

View File

@ -0,0 +1,81 @@
'use strict';
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _mongoose = require('mongoose');
var _mongoose2 = _interopRequireDefault(_mongoose);
var _mongooseAutoIncrement = require('mongoose-auto-increment');
var _mongooseAutoIncrement2 = _interopRequireDefault(_mongooseAutoIncrement);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 所有的model都需要继承baseModel, 且需要 getSchema和getName方法不然会报错
*/
var baseModel = function () {
function baseModel() {
(0, _classCallCheck3.default)(this, baseModel);
this.schema = new _mongoose2.default.Schema(this.getSchema());
this.name = this.getName();
if (this.isNeedAutoIncrement() === true) {
this.schema.plugin(_mongooseAutoIncrement2.default.plugin, {
model: this.name,
field: this.getPrimaryKey(),
startAt: 101,
incrementBy: _yapi2.default.commons.rand(1, 100)
});
}
this.model = _yapi2.default.db(this.name, this.schema);
}
(0, _createClass3.default)(baseModel, [{
key: 'isNeedAutoIncrement',
value: function isNeedAutoIncrement() {
return true;
}
/**
* 可通过覆盖此方法生成其他自增字段
*/
}, {
key: 'getPrimaryKey',
value: function getPrimaryKey() {
return '_id';
}
/**
* 获取collection的schema结构
*/
}, {
key: 'getSchema',
value: function getSchema() {
_yapi2.default.commons.log('Model Class need getSchema function', 'error');
}
}, {
key: 'getName',
value: function getName() {
_yapi2.default.commons.log('Model Class need name', 'error');
}
}]);
return baseModel;
}();
module.exports = baseModel;

108
server_dist/models/group.js Normal file
View File

@ -0,0 +1,108 @@
'use strict';
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _mongoose = require('mongoose');
var _mongoose2 = _interopRequireDefault(_mongoose);
var _base = require('./base.js');
var _base2 = _interopRequireDefault(_base);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var groupModel = function (_baseModel) {
(0, _inherits3.default)(groupModel, _baseModel);
function groupModel() {
(0, _classCallCheck3.default)(this, groupModel);
return (0, _possibleConstructorReturn3.default)(this, (groupModel.__proto__ || (0, _getPrototypeOf2.default)(groupModel)).apply(this, arguments));
}
(0, _createClass3.default)(groupModel, [{
key: 'getName',
value: function getName() {
return 'group';
}
}, {
key: 'getSchema',
value: function getSchema() {
return {
uid: Number,
group_name: String,
group_desc: String,
add_time: Number,
up_time: Number
};
}
}, {
key: 'save',
value: function save(data) {
var m = new this.model(data);
return m.save();
}
}, {
key: 'checkRepeat',
value: function checkRepeat(name) {
return this.model.count({
group_name: name
});
}
}, {
key: 'list',
value: function list() {
return this.model.find().select("group_name _id group_desc add_time up_time").exec();
}
}, {
key: 'del',
value: function del(id) {
return this.model.deleteOne({
_id: id
});
}
}, {
key: 'up',
value: function up(id, data) {
return this.model.update({
_id: id
}, {
group_name: data.group_name,
group_desc: data.group_desc,
up_time: _yapi2.default.commons.time()
});
}
}, {
key: 'search',
value: function search(keyword) {
return this.model.find({
group_name: new RegExp(keyword, 'i')
}).limit(10);
}
}]);
return groupModel;
}(_base2.default);
module.exports = groupModel;

View File

@ -0,0 +1,142 @@
'use strict';
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _base = require('./base.js');
var _base2 = _interopRequireDefault(_base);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var interfaceModel = function (_baseModel) {
(0, _inherits3.default)(interfaceModel, _baseModel);
function interfaceModel() {
(0, _classCallCheck3.default)(this, interfaceModel);
return (0, _possibleConstructorReturn3.default)(this, (interfaceModel.__proto__ || (0, _getPrototypeOf2.default)(interfaceModel)).apply(this, arguments));
}
(0, _createClass3.default)(interfaceModel, [{
key: 'getName',
value: function getName() {
return 'interface';
}
}, {
key: 'getSchema',
value: function getSchema() {
return {
uid: { type: Number, required: true },
path: { type: String, required: true, validate: {
validator: function validator(v) {
return v && v[0] !== '/';
},
message: '接口路径第一位不能是/'
} },
method: { type: String, required: true },
project_id: { type: Number, required: true },
desc: String,
add_time: Number,
up_time: Number,
req_headers: [{
name: String, value: String, desc: String, required: Boolean
}],
req_params_type: {
type: String,
enum: ["form", "json", "text", "xml"]
},
req_params_form: [{
name: String, value: String, value_type: { type: String, enum: ["text", "file"] }, desc: String, required: Boolean
}],
req_params_other: String,
res_body_type: {
type: String,
enum: ["json", "text", "xml"]
},
res_body: String
};
}
}, {
key: 'save',
value: function save(data) {
var m = new this.model(data);
return m.save();
}
}, {
key: 'get',
value: function get(id) {
return this.model.findOne({
_id: id
}).exec();
}
}, {
key: 'getByPath',
value: function getByPath(project_id, path) {
return this.model.find({
project_id: project_id,
path: path
}).exec();
}
}, {
key: 'checkRepeat',
value: function checkRepeat(path, method) {
return this.model.count({
path: path,
method: method
});
}
}, {
key: 'countByProjectId',
value: function countByProjectId(id) {
return this.model.count({
project_id: id
});
}
}, {
key: 'list',
value: function list(group_id) {
return this.model.find({
group_id: group_id
}).exec();
}
}, {
key: 'del',
value: function del(id) {
return this.model.deleteOne({
_id: id
});
}
}, {
key: 'up',
value: function up(id, data) {
data.up_time = _yapi2.default.commons.time();
return this.model.update({
_id: id
}, data, { runValidators: true });
}
}]);
return interfaceModel;
}(_base2.default);
module.exports = interfaceModel;

146
server_dist/router.js Normal file
View File

@ -0,0 +1,146 @@
'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);
var _yapi = require('./yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _project = require('./controllers/project.js');
var _project2 = _interopRequireDefault(_project);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var router = (0, _koaRouter2.default)();
var INTERFACE_CONFIG = {
interface: {
prefix: '/interface/',
controller: _interface2.default
},
user: {
prefix: '/user/',
controller: _user2.default
},
group: {
prefix: '/group/',
controller: _group2.default
},
project: {
prefix: '/project/',
controller: _project2.default
}
};
//group
createAction('group', 'list', 'get', 'list');
createAction('group', 'add', 'post', 'add');
createAction('group', 'up', 'post', 'up');
createAction('group', 'del', 'post', 'del');
//user
createAction('user', 'login', 'post', 'login');
createAction('user', 'reg', 'post', 'reg');
createAction('user', 'list', 'get', 'list');
createAction('user', 'find', 'get', 'findById');
createAction('user', 'update', 'post', 'update');
createAction('user', 'del', 'post', 'del');
createAction('user', 'status', 'get', 'getLoginStatus');
createAction('user', 'logout', 'get', 'logout');
createAction('user', 'login_by_token', 'post', 'loginByToken');
createAction('user', 'change_password', 'post', 'changePassword');
createAction('user', 'search', 'get', 'search');
//project
createAction('project', 'add', 'post', 'add');
createAction('project', 'list', 'get', 'list');
createAction('project', 'get', 'get', 'get');
createAction('project', 'up', 'post', 'up');
createAction('project', 'del', 'post', 'del');
createAction('project', 'add_member', 'post', 'addMember');
createAction('project', 'del_member', 'post', 'delMember');
createAction('project', 'get_member_list', 'get', 'getMemberList');
createAction('project', 'search', 'get', 'search');
//interface
createAction('interface', 'add', 'post', 'add');
createAction('interface', 'list', 'get', 'list');
createAction('interface', 'get', 'get', 'get');
createAction('interface', 'up', 'post', 'up');
createAction('interface', 'del', 'post', 'del');
/**
*
* @param {*} controller controller_name
* @param {*} path request_path
* @param {*} method request_method , post get put delete ...
* @param {*} action controller_action_name
*/
function createAction(controller, path, method, action) {
var _this = this;
router[method](INTERFACE_CONFIG[controller].prefix + path, function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
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:
ctx.body = _yapi2.default.commons.resReturn(null, 400, 'Without Permission.');
case 9:
case 'end':
return _context.stop();
}
}
}, _callee, _this);
}));
return function (_x) {
return _ref.apply(this, arguments);
};
}());
}
module.exports = router;

View File

@ -0,0 +1,158 @@
'use strict';
var _stringify = require('babel-runtime/core-js/json/stringify');
var _stringify2 = _interopRequireDefault(_stringify);
var _typeof2 = require('babel-runtime/helpers/typeof');
var _typeof3 = _interopRequireDefault(_typeof2);
var _fsExtra = require('fs-extra');
var _fsExtra2 = _interopRequireDefault(_fsExtra);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _sha = require('sha1');
var _sha2 = _interopRequireDefault(_sha);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.resReturn = function (data, num, errmsg) {
num = num || 0;
return {
errcode: num,
errmsg: errmsg || 'success',
data: data
};
};
var MSGTYPE = {
'log': 'Log',
'warn': 'warning',
'error': 'Error'
};
exports.log = function (msg, type) {
if (!msg) return;
type = type || 'log';
var f = void 0;
switch (type) {
case 'log':
f = console.log;break;
case 'warn':
f = console.warn;break;
case 'error':
f = console.error;break;
default:
f = console.log;break;
}
f(type + ':', msg);
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var logfile = _path2.default.join(_yapi2.default.WEBROOT_LOG, year + '-' + month + '.log');
if ((typeof msg === 'undefined' ? 'undefined' : (0, _typeof3.default)(msg)) === 'object') {
if (msg instanceof Error) msg = msg.message;else msg = (0, _stringify2.default)(msg);
}
var data = new Date().toLocaleTimeString() + "\t|\t" + type + "\t|\t" + msg;
_fsExtra2.default.writeFileSync(logfile, data, {
flag: 'w+'
});
};
exports.fileExist = function (filePath) {
try {
return _fsExtra2.default.statSync(filePath).isFile();
} catch (err) {
return false;
}
};
exports.time = function () {
return Date.parse(new Date()) / 1000;
};
exports.fieldSelect = function (data, field) {
if (!data || !field || !Array.isArray(field)) return null;
var arr = {};
field.forEach(function (f) {
data[f] && (arr[f] = data[f]);
});
return arr;
};
exports.rand = function (min, max) {
return Math.floor(Math.random() * (max - min) + min);
};
exports.json_parse = function (json) {
try {
return JSON.parse(json);
} catch (e) {
return json;
}
};
exports.randStr = function () {
return Math.random().toString(36).substr(2);
};
exports.generatePassword = function (password, passsalt) {
return (0, _sha2.default)(password + (0, _sha2.default)(passsalt));
};
exports.expireDate = function (day) {
var date = new Date();
date.setTime(date.getTime() + day * 86400000);
return date;
};
exports.sendMail = function (options, cb) {
if (!_yapi2.default.mail) return false;
options.subject = options.subject ? options.subject + '-yapi平台' : 'ypai平台';
cb = cb || function (err, info) {
if (err) {
_yapi2.default.commons.log('send mail ' + options.to + ' error,' + err.message, 'error');
} else {
_yapi2.default.commons.log('send mail ' + options.to + ' success');
}
};
_yapi2.default.mail.sendMail({
from: _yapi2.default.WEBCONFIG.mail.auth.user,
to: options.to,
subject: 'yapi平台',
html: options.contents
}, cb);
};
exports.validateSearchKeyword = function (keyword) {
if (/^\*|\?|\+|\$|\^|\\|\.$/.test(keyword)) {
return false;
}
return true;
};
exports.filterRes = function (list, rules) {
return list.map(function (item) {
var filteredRes = {};
rules.forEach(function (rule) {
if (typeof rule == 'string') {
filteredRes[rule] = item[rule];
} else if ((typeof rule === 'undefined' ? 'undefined' : (0, _typeof3.default)(rule)) == 'object') {
filteredRes[rule.alias] = item[rule.key];
}
});
return filteredRes;
});
};

51
server_dist/utils/db.js Normal file
View File

@ -0,0 +1,51 @@
'use strict';
var _mongoose = require('mongoose');
var _mongoose2 = _interopRequireDefault(_mongoose);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _mongooseAutoIncrement = require('mongoose-auto-increment');
var _mongooseAutoIncrement2 = _interopRequireDefault(_mongooseAutoIncrement);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function model(model, schema) {
if (schema instanceof _mongoose2.default.Schema === false) {
schema = new _mongoose2.default.Schema(schema);
}
schema.set('autoIndex', false);
return _yapi2.default.connect.model(model, schema, model);
}
function connect() {
_mongoose2.default.Promise = global.Promise;
var config = _yapi2.default.WEBCONFIG;
var db = _mongoose2.default.connect('mongodb://' + config.db.servername + ':' + config.db.port + '/' + config.db.DATABASE, {
user: config.db.user,
pass: config.db.pass
});
db.then(function (res) {
_yapi2.default.commons.log('mongodb load success...');
}, function (err) {
_yapi2.default.commons.log(err, 'Mongo connect error');
});
_mongooseAutoIncrement2.default.initialize(db);
return db;
}
_yapi2.default.db = model;
module.exports = {
model: model,
connect: connect
};

85
server_dist/yapi.js Normal file
View File

@ -0,0 +1,85 @@
'use strict';
var _map = require('babel-runtime/core-js/map');
var _map2 = _interopRequireDefault(_map);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fsExtra = require('fs-extra');
var _fsExtra2 = _interopRequireDefault(_fsExtra);
var _nodemailer = require('nodemailer');
var _nodemailer2 = _interopRequireDefault(_nodemailer);
var _config = require('../runtime/config.json');
var _config2 = _interopRequireDefault(_config);
var _configDev = require('../runtime/config.dev.json');
var _configDev2 = _interopRequireDefault(_configDev);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var args = process.argv.splice(2);
var isDev = args[0] === 'dev' ? true : false;
var insts = new _map2.default();
var mail = void 0;
var config = isDev ? _configDev2.default : _config2.default;
var WEBROOT = _path2.default.resolve(__dirname, '..'); //路径
var WEBROOT_SERVER = __dirname;
var WEBROOT_RUNTIME = _path2.default.join(WEBROOT, 'runtime');
var WEBROOT_LOG = _path2.default.join(WEBROOT_RUNTIME, 'log');
var WEBCONFIG = config;
_fsExtra2.default.ensureDirSync(WEBROOT_LOG);
if (WEBCONFIG.mail) {
mail = _nodemailer2.default.createTransport(WEBCONFIG.mail);
}
/**
* 获取一个model实例如果不存在则创建一个新的返回
* @param {*} m class
* @example
* yapi.getInst(groupModel, arg1, arg2)
*/
function getInst(m) {
if (!insts.get(m)) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
insts.set(m, new m(args));
}
return insts.get(m);
}
function delInst(m) {
try {
insts.delete(m);
} catch (err) {
console.error(err);
}
}
var r = {
fs: _fsExtra2.default,
path: _path2.default,
WEBROOT: WEBROOT,
WEBROOT_SERVER: WEBROOT_SERVER,
WEBROOT_RUNTIME: WEBROOT_RUNTIME,
WEBROOT_LOG: WEBROOT_LOG,
WEBCONFIG: WEBCONFIG,
getInst: getInst,
delInst: delInst,
getInsts: insts
};
if (mail) r.mail = mail;
module.exports = r;