yapi/server_dist/controllers/interface.js

521 lines
21 KiB
JavaScript
Raw Normal View History

2017-07-03 16:16:05 +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);
2017-07-10 11:56:53 +08:00
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);
2017-07-03 16:16:05 +08:00
var _interface = require('../models/interface.js');
var _interface2 = _interopRequireDefault(_interface);
2017-07-10 11:56:53 +08:00
var _base = require('./base.js');
var _base2 = _interopRequireDefault(_base);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
2017-07-03 16:16:05 +08:00
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2017-07-10 11:56:53 +08:00
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不能为空
2017-07-20 16:44:41 +08:00
* @param {String} title 接口标题不能为空
2017-07-10 11:56:53 +08:00
* @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"]四种
2017-07-20 19:47:10 +08:00
* @param {Mixed} [req_params_form] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_params_form[].name] 请求参数名
* @param {String} [req_params_form[].value] 请求参数值可填写生成规则mock@email随机生成一条email
* @param {String} [req_params_form[].type] 请求参数类型["text", "file"]两种
* @param {String} [req_params_other] 非form类型的请求参数可保存到此字段
2017-07-10 11:56:53 +08:00
* @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';
2017-07-20 19:47:10 +08:00
params.method = params.method.toUpperCase();
2017-07-10 11:56:53 +08:00
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
if (params.project_id) {
2017-07-20 19:47:10 +08:00
_context.next = 6;
2017-07-10 11:56:53 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空'));
2017-07-20 19:47:10 +08:00
case 6:
2017-07-10 11:56:53 +08:00
if (params.path) {
2017-07-20 19:47:10 +08:00
_context.next = 8;
2017-07-10 11:56:53 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口请求路径不能为空'));
2017-07-20 19:47:10 +08:00
case 8:
_context.next = 10;
2017-07-10 11:56:53 +08:00
return this.Model.checkRepeat(params.path, params.method);
2017-07-20 19:47:10 +08:00
case 10:
2017-07-10 11:56:53 +08:00
checkRepeat = _context.sent;
if (!(checkRepeat > 0)) {
2017-07-20 19:47:10 +08:00
_context.next = 13;
2017-07-10 11:56:53 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']'));
2017-07-20 19:47:10 +08:00
case 13:
_context.prev = 13;
2017-07-10 11:56:53 +08:00
data = {
project_id: params.project_id,
title: params.title,
2017-07-10 11:56:53 +08:00
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()
};
2017-07-20 16:44:41 +08:00
if (params.req_params_form) data.req_params_form = params.req_params_form;
if (params.req_params_other) data.req_params_other = params.req_params_other;
2017-07-10 11:56:53 +08:00
2017-07-20 19:47:10 +08:00
_context.next = 19;
2017-07-10 11:56:53 +08:00
return this.Model.save(data);
2017-07-20 19:47:10 +08:00
case 19:
2017-07-10 11:56:53 +08:00
result = _context.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
2017-07-20 19:47:10 +08:00
_context.next = 26;
2017-07-10 11:56:53 +08:00
break;
2017-07-20 19:47:10 +08:00
case 23:
_context.prev = 23;
_context.t0 = _context['catch'](13);
2017-07-10 11:56:53 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t0.message);
2017-07-20 19:47:10 +08:00
case 26:
2017-07-10 11:56:53 +08:00
case 'end':
return _context.stop();
}
2017-07-03 16:16:05 +08:00
}
2017-07-20 19:47:10 +08:00
}, _callee, this, [[13, 23]]);
2017-07-10 11:56:53 +08:00
}));
function add(_x) {
return _ref.apply(this, arguments);
}
return add;
}()
2017-07-11 18:22:20 +08:00
/**
* 添加项目分组
* @interface /interface/get
* @method GET
* @category interface
* @foldnumber 10
* @param {Number} id 接口id不能为空
* @returns {Object}
* @example ./api/interface/get.json
*/
2017-07-10 11:56:53 +08:00
}, {
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();
}
2017-07-03 16:16:05 +08:00
}
2017-07-10 11:56:53 +08:00
}, _callee2, this, [[3, 10]]);
}));
function get(_x2) {
return _ref2.apply(this, arguments);
}
return get;
}()
2017-07-11 18:22:20 +08:00
/**
2017-07-20 19:47:10 +08:00
* 接口列表
2017-07-11 18:22:20 +08:00
* @interface /interface/list
* @method GET
* @category interface
* @foldnumber 10
* @param {Number} project_id 项目id不能为空
* @returns {Object}
* @example ./api/interface/list.json
*/
2017-07-10 11:56:53 +08:00
}, {
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;
}()
2017-07-11 18:22:20 +08:00
/**
* 编辑接口
2017-07-11 18:22:20 +08:00
* @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"]四种
2017-07-20 19:47:10 +08:00
* @param {Mixed} [req_params_form] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_params_form[].name] 请求参数名
* @param {String} [req_params_form[].value] 请求参数值可填写生成规则mock@email随机生成一条email
* @param {String} [req_params_form[].type] 请求参数类型["text", "file"]两种
* @param {String} [req_params_other] 非form类型的请求参数可保存到此字段
2017-07-11 18:22:20 +08:00
* @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
*/
2017-07-10 11:56:53 +08:00
}, {
key: 'up',
value: function () {
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(ctx) {
2017-07-20 19:47:10 +08:00
var params, id, interfaceData, checkRepeat, data, result;
2017-07-10 11:56:53 +08:00
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';
2017-07-20 19:47:10 +08:00
params.method = params.method.toUpperCase();
2017-07-10 11:56:53 +08:00
id = ctx.request.body.id;
if (id) {
2017-07-20 19:47:10 +08:00
_context4.next = 6;
2017-07-10 11:56:53 +08:00
break;
}
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口id不能为空'));
2017-07-20 19:47:10 +08:00
case 6:
_context4.next = 8;
return this.Model.get(id);
case 8:
interfaceData = _context4.sent;
if (!(params.path && params.path !== interfaceData.path && params.method !== interfaceData.method)) {
_context4.next = 15;
2017-07-10 11:56:53 +08:00
break;
}
2017-07-20 19:47:10 +08:00
_context4.next = 12;
2017-07-10 11:56:53 +08:00
return this.Model.checkRepeat(params.path, params.method);
2017-07-20 19:47:10 +08:00
case 12:
2017-07-10 11:56:53 +08:00
checkRepeat = _context4.sent;
if (!(checkRepeat > 0)) {
2017-07-20 19:47:10 +08:00
_context4.next = 15;
2017-07-10 11:56:53 +08:00
break;
}
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']'));
2017-07-20 19:47:10 +08:00
case 15:
2017-07-10 11:56:53 +08:00
data = {
up_time: _yapi2.default.commons.time()
};
if (params.path) data.path = params.path;
2017-07-20 16:44:41 +08:00
if (params.title) data.title = params.title;
2017-07-10 11:56:53 +08:00
if (params.desc) data.desc = params.desc;
if (params.method) data.method = params.method;
if (params.req_headers) data.req_headers = params.req_headers;
2017-07-20 16:44:41 +08:00
if (params.req_params_form) data.req_params_form = params.req_params_form;
if (params.req_params_other) data.req_params_other = params.req_params_other;
2017-07-10 11:56:53 +08:00
if (params.res_body_type) data.res_body_type = params.res_body_type;
if (params.res_body) data.res_body = params.res_body;
2017-07-20 19:47:10 +08:00
_context4.prev = 25;
_context4.next = 28;
2017-07-10 11:56:53 +08:00
return this.Model.up(id, data);
2017-07-20 19:47:10 +08:00
case 28:
2017-07-10 11:56:53 +08:00
result = _context4.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
2017-07-20 19:47:10 +08:00
_context4.next = 35;
2017-07-10 11:56:53 +08:00
break;
2017-07-20 19:47:10 +08:00
case 32:
_context4.prev = 32;
_context4.t0 = _context4['catch'](25);
2017-07-10 11:56:53 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context4.t0.message);
2017-07-20 19:47:10 +08:00
case 35:
2017-07-10 11:56:53 +08:00
case 'end':
return _context4.stop();
}
}
2017-07-20 19:47:10 +08:00
}, _callee4, this, [[25, 32]]);
2017-07-10 11:56:53 +08:00
}));
function up(_x4) {
return _ref4.apply(this, arguments);
}
return up;
}()
2017-07-11 18:22:20 +08:00
/**
* 删除接口
* @interface /interface/del
* @method GET
* @category interface
* @foldnumber 10
* @param {Number} id 接口id不能为空
* @returns {Object}
* @example ./api/interface/del.json
*/
2017-07-10 11:56:53 +08:00
}, {
key: 'del',
value: function () {
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) {
2017-07-11 18:22:20 +08:00
var id, data, result;
2017-07-10 11:56:53 +08:00
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;
2017-07-24 10:59:17 +08:00
return this.Model.get(ctx.request.body.id);
2017-07-10 11:56:53 +08:00
case 6:
2017-07-11 18:22:20 +08:00
data = _context5.sent;
if (!(data.uid != this.getUid())) {
_context5.next = 13;
break;
}
_context5.next = 10;
return this.jungeProjectAuth(data.project_id);
case 10:
2017-07-10 11:56:53 +08:00
_context5.t0 = _context5.sent;
if (!(_context5.t0 !== true)) {
2017-07-11 18:22:20 +08:00
_context5.next = 13;
2017-07-10 11:56:53 +08:00
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限'));
2017-07-11 18:22:20 +08:00
case 13:
_context5.next = 15;
2017-07-10 11:56:53 +08:00
return this.Model.del(id);
2017-07-11 18:22:20 +08:00
case 15:
2017-07-10 11:56:53 +08:00
result = _context5.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
2017-07-11 18:22:20 +08:00
_context5.next = 22;
2017-07-10 11:56:53 +08:00
break;
2017-07-11 18:22:20 +08:00
case 19:
_context5.prev = 19;
2017-07-10 11:56:53 +08:00
_context5.t1 = _context5['catch'](0);
2017-07-24 10:59:17 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context5.t1.message);
2017-07-10 11:56:53 +08:00
2017-07-11 18:22:20 +08:00
case 22:
2017-07-10 11:56:53 +08:00
case 'end':
return _context5.stop();
}
}
2017-07-11 18:22:20 +08:00
}, _callee5, this, [[0, 19]]);
2017-07-10 11:56:53 +08:00
}));
function del(_x5) {
return _ref5.apply(this, arguments);
}
return del;
}()
}]);
return interfaceController;
}(_base2.default);
module.exports = interfaceController;