yapi/server_dist/controllers/interface.js

1185 lines
50 KiB
JavaScript
Raw Normal View History

2017-07-03 16:16:05 +08:00
'use strict';
2017-08-17 15:19:47 +08:00
var _stringify = require('babel-runtime/core-js/json/stringify');
var _stringify2 = _interopRequireDefault(_stringify);
2017-07-03 16:16:05 +08:00
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-08-18 20:35:31 +08:00
var _interfaceCat = require('../models/interfaceCat.js');
var _interfaceCat2 = _interopRequireDefault(_interfaceCat);
2017-08-28 18:01:28 +08:00
var _interfaceCase = require('../models/interfaceCase.js');
var _interfaceCase2 = _interopRequireDefault(_interfaceCase);
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-08-17 15:19:47 +08:00
var _user = require('../models/user.js');
var _user2 = _interopRequireDefault(_user);
var _project = require('../models/project.js');
var _project2 = _interopRequireDefault(_project);
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);
2017-08-18 20:35:31 +08:00
_this.catModel = _yapi2.default.getInst(_interfaceCat2.default);
_this.projectModel = _yapi2.default.getInst(_project2.default);
2017-08-28 18:01:28 +08:00
_this.caseModel = _yapi2.default.getInst(_interfaceCase2.default);
2017-07-10 11:56:53 +08:00
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描述
2017-08-15 12:08:59 +08:00
* @param {String} [req_body_type] 请求参数方式["form", "json", "text", "xml"]四种
* @param {Array} [req_params] name, desc两个参数
2017-08-15 12:08:59 +08:00
* @param {Mixed} [req_body_form] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_body_form[].name] 请求参数名
* @param {String} [req_body_form[].value] 请求参数值可填写生成规则mock@email随机生成一条email
* @param {String} [req_body_form[].type] 请求参数类型["text", "file"]两种
* @param {String} [req_body_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 () {
2017-08-29 11:17:54 +08:00
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(ctx) {
2017-08-24 19:27:48 +08:00
var _this2 = this;
var params, auth, checkRepeat, data, result;
2017-07-10 11:56:53 +08:00
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
params = ctx.request.body;
2017-07-26 14:22:59 +08:00
params = _yapi2.default.commons.handleParams(params, {
2017-07-26 17:56:51 +08:00
project_id: 'number',
2017-07-26 14:22:59 +08:00
title: 'string',
path: 'string',
method: 'string',
2017-08-18 20:35:31 +08:00
desc: 'string',
catid: 'number'
2017-07-26 14:22:59 +08:00
});
2017-08-24 14:17:19 +08:00
_context.next = 4;
return this.checkAuth(params.project_id, 'project', 'edit');
case 4:
auth = _context.sent;
if (auth) {
_context.next = 7;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '没有权限'));
case 7:
2017-07-10 11:56:53 +08:00
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-08-24 14:17:19 +08:00
_context.next = 12;
2017-07-10 11:56:53 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空'));
2017-08-24 14:17:19 +08:00
case 12:
2017-07-10 11:56:53 +08:00
if (params.path) {
2017-08-24 14:17:19 +08:00
_context.next = 14;
2017-07-10 11:56:53 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口请求路径不能为空'));
2017-08-24 14:17:19 +08:00
case 14:
2017-07-24 11:24:08 +08:00
if (_yapi2.default.commons.verifyPath(params.path)) {
2017-08-24 14:17:19 +08:00
_context.next = 16;
2017-07-24 11:24:08 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/'));
2017-07-10 11:56:53 +08:00
2017-08-24 14:17:19 +08:00
case 16:
_context.next = 18;
2017-07-26 17:56:51 +08:00
return this.Model.checkRepeat(params.project_id, params.path, params.method);
2017-07-24 11:24:08 +08:00
2017-08-24 14:17:19 +08:00
case 18:
2017-07-10 11:56:53 +08:00
checkRepeat = _context.sent;
if (!(checkRepeat > 0)) {
2017-08-24 14:17:19 +08:00
_context.next = 21;
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-08-24 14:17:19 +08:00
case 21:
_context.prev = 21;
2017-07-10 11:56:53 +08:00
data = {
project_id: params.project_id,
2017-08-18 20:35:31 +08:00
catid: params.catid,
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,
2017-08-15 12:08:59 +08:00
req_body_type: params.req_body_type,
2017-07-10 11:56:53 +08:00
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-08-15 12:08:59 +08:00
if (params.req_query) {
data.req_query = params.req_query;
2017-07-27 15:50:04 +08:00
}
2017-08-15 12:08:59 +08:00
if (params.req_body_form) {
data.req_body_form = params.req_body_form;
}
2017-08-16 10:56:42 +08:00
if (params.req_params && Array.isArray(params.req_params) && params.req_params.length > 0) {
data.type = 'var';
data.req_params = params.req_params;
2017-08-16 10:56:42 +08:00
} else {
data.type = 'static';
}
2017-08-15 12:08:59 +08:00
if (params.req_body_other) {
data.req_body_other = params.req_body_other;
2017-07-27 15:50:04 +08:00
}
2017-07-10 11:56:53 +08:00
2017-08-24 14:17:19 +08:00
_context.next = 29;
2017-07-10 11:56:53 +08:00
return this.Model.save(data);
2017-08-24 14:17:19 +08:00
case 29:
2017-07-10 11:56:53 +08:00
result = _context.sent;
2017-08-24 19:27:48 +08:00
// let project = await this.projectModel.get(params.project_id);
this.catModel.get(params.catid).then(function (cate) {
var username = _this2.getUsername();
_yapi2.default.commons.saveLog({
content: '\u7528\u6237 "' + username + '" \u4E3A\u5206\u7C7B "' + cate.name + '" \u6DFB\u52A0\u4E86\u63A5\u53E3 "' + data.title + '"',
type: 'project',
uid: _this2.getUid(),
username: username,
typeid: params.project_id
});
});
2017-08-24 19:27:48 +08:00
2017-07-10 11:56:53 +08:00
ctx.body = _yapi2.default.commons.resReturn(result);
2017-08-24 19:27:48 +08:00
_context.next = 37;
2017-07-10 11:56:53 +08:00
break;
2017-08-24 19:27:48 +08:00
case 34:
_context.prev = 34;
2017-08-24 14:17:19 +08:00
_context.t0 = _context['catch'](21);
2017-07-10 11:56:53 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t0.message);
2017-08-24 19:27:48 +08:00
case 37:
2017-07-10 11:56:53 +08:00
case 'end':
return _context.stop();
}
2017-07-03 16:16:05 +08:00
}
2017-08-24 19:27:48 +08:00
}, _callee, this, [[21, 34]]);
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 () {
2017-08-29 11:17:54 +08:00
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(ctx) {
2017-07-10 11:56:53 +08:00
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 () {
2017-08-29 11:17:54 +08:00
var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(ctx) {
2017-07-10 11:56:53 +08:00
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);
2017-07-27 15:50:04 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context3.t0.message);
2017-07-10 11:56:53 +08:00
case 13:
case 'end':
return _context3.stop();
}
}
}, _callee3, this, [[3, 10]]);
}));
function list(_x3) {
return _ref3.apply(this, arguments);
}
return list;
}()
2017-08-18 20:35:31 +08:00
}, {
key: 'listByCat',
value: function () {
2017-08-29 11:17:54 +08:00
var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(ctx) {
2017-08-18 20:35:31 +08:00
var catid, result;
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
catid = ctx.request.query.catid;
if (catid) {
_context4.next = 3;
break;
}
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'catid不能为空'));
case 3:
_context4.prev = 3;
_context4.next = 6;
return this.Model.listByCatid(catid);
case 6:
result = _context4.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context4.next = 13;
break;
case 10:
_context4.prev = 10;
_context4.t0 = _context4['catch'](3);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context4.t0.message);
case 13:
case 'end':
return _context4.stop();
}
}
}, _callee4, this, [[3, 10]]);
}));
function listByCat(_x4) {
return _ref4.apply(this, arguments);
}
return listByCat;
}()
}, {
key: 'listByMenu',
value: function () {
2017-08-29 11:17:54 +08:00
var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(ctx) {
2017-08-18 20:35:31 +08:00
var project_id, result, newResult, i, item, list, j;
return _regenerator2.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
project_id = ctx.request.query.project_id;
if (project_id) {
_context5.next = 3;
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空'));
case 3:
_context5.prev = 3;
_context5.next = 6;
return this.catModel.list(project_id);
case 6:
result = _context5.sent;
newResult = [];
i = 0;
case 9:
if (!(i < result.length)) {
_context5.next = 20;
break;
}
item = result[i].toObject();
_context5.next = 13;
2017-08-24 12:16:43 +08:00
return this.Model.listByCatid(item._id, '_id title method path');
2017-08-18 20:35:31 +08:00
case 13:
list = _context5.sent;
for (j = 0; j < list.length; j++) {
list[j] = list[j].toObject();
}
item.list = list;
newResult[i] = item;
case 17:
i++;
_context5.next = 9;
break;
case 20:
ctx.body = _yapi2.default.commons.resReturn(newResult);
_context5.next = 26;
break;
case 23:
_context5.prev = 23;
_context5.t0 = _context5['catch'](3);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context5.t0.message);
case 26:
case 'end':
return _context5.stop();
}
}
}, _callee5, this, [[3, 23]]);
}));
function listByMenu(_x5) {
return _ref5.apply(this, arguments);
}
return listByMenu;
}()
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描述
2017-08-15 12:08:59 +08:00
* @param {String} [req_body_type] 请求参数方式["form", "json", "text", "xml"]四种
* @param {Mixed} [req_body_form] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_body_form[].name] 请求参数名
* @param {String} [req_body_form[].value] 请求参数值可填写生成规则mock@email随机生成一条email
* @param {String} [req_body_form[].type] 请求参数类型["text", "file"]两种
* @param {String} [req_body_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 () {
2017-08-29 11:17:54 +08:00
var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(ctx) {
2017-08-24 19:27:48 +08:00
var _this3 = this;
var params, id, interfaceData, auth, checkRepeat, data, result, username, cateid;
2017-08-18 20:35:31 +08:00
return _regenerator2.default.wrap(function _callee6$(_context6) {
2017-07-10 11:56:53 +08:00
while (1) {
2017-08-18 20:35:31 +08:00
switch (_context6.prev = _context6.next) {
2017-07-10 11:56:53 +08:00
case 0:
params = ctx.request.body;
2017-07-27 15:50:04 +08:00
2017-07-26 14:22:59 +08:00
params = _yapi2.default.commons.handleParams(params, {
title: 'string',
path: 'string',
method: 'string',
2017-08-18 20:35:31 +08:00
desc: 'string',
catid: 'number'
2017-07-26 14:22:59 +08:00
});
2017-08-24 14:17:19 +08:00
2017-07-10 11:56:53 +08:00
params.method = params.method || 'GET';
2017-07-20 19:47:10 +08:00
params.method = params.method.toUpperCase();
2017-07-27 15:50:04 +08:00
2017-07-10 11:56:53 +08:00
id = ctx.request.body.id;
if (id) {
2017-08-18 20:35:31 +08:00
_context6.next = 7;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-18 20:35:31 +08:00
return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口id不能为空'));
2017-07-10 11:56:53 +08:00
2017-07-26 14:22:59 +08:00
case 7:
2017-08-18 20:35:31 +08:00
_context6.next = 9;
2017-07-20 19:47:10 +08:00
return this.Model.get(id);
2017-07-26 14:22:59 +08:00
case 9:
2017-08-18 20:35:31 +08:00
interfaceData = _context6.sent;
2017-08-24 14:17:19 +08:00
_context6.next = 12;
return this.checkAuth(interfaceData.project_id, 'project', 'edit');
case 12:
auth = _context6.sent;
2017-07-20 19:47:10 +08:00
2017-08-24 14:17:19 +08:00
if (auth) {
_context6.next = 15;
break;
}
return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '没有权限'));
case 15:
2017-07-24 11:24:08 +08:00
if (!(params.path && !_yapi2.default.commons.verifyPath(params.path))) {
2017-08-24 14:17:19 +08:00
_context6.next = 17;
2017-07-24 11:24:08 +08:00
break;
}
2017-08-18 20:35:31 +08:00
return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/'));
2017-07-24 11:24:08 +08:00
2017-08-24 14:17:19 +08:00
case 17:
2017-07-20 19:47:10 +08:00
if (!(params.path && params.path !== interfaceData.path && params.method !== interfaceData.method)) {
2017-08-24 14:17:19 +08:00
_context6.next = 23;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-24 14:17:19 +08:00
_context6.next = 20;
2017-07-26 17:56:51 +08:00
return this.Model.checkRepeat(interfaceData.project_id, params.path, params.method);
2017-07-10 11:56:53 +08:00
2017-08-24 14:17:19 +08:00
case 20:
2017-08-18 20:35:31 +08:00
checkRepeat = _context6.sent;
2017-07-10 11:56:53 +08:00
if (!(checkRepeat > 0)) {
2017-08-24 14:17:19 +08:00
_context6.next = 23;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-18 20:35:31 +08:00
return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']'));
2017-07-10 11:56:53 +08:00
2017-08-24 14:17:19 +08:00
case 23:
2017-07-10 11:56:53 +08:00
data = {
up_time: _yapi2.default.commons.time()
};
2017-07-27 15:50:04 +08:00
if (params.path) {
data.path = params.path;
}
if (params.title) {
data.title = params.title;
}
if (params.desc) {
data.desc = params.desc;
}
if (params.method) {
data.method = params.method;
}
2017-07-10 11:56:53 +08:00
2017-08-18 20:35:31 +08:00
if (params.catid) {
data.catid = params.catid;
}
2017-07-27 15:50:04 +08:00
if (params.req_headers) {
data.req_headers = params.req_headers;
}
2017-07-10 11:56:53 +08:00
2017-08-15 12:08:59 +08:00
if (params.req_body_form) {
data.req_body_form = params.req_body_form;
}
2017-08-16 10:56:42 +08:00
if (params.req_params && Array.isArray(params.req_params) && params.req_params.length > 0) {
data.type = 'var';
data.req_params = params.req_params;
2017-08-16 10:56:42 +08:00
} else {
data.type = 'static';
}
2017-08-16 10:56:42 +08:00
2017-08-15 12:08:59 +08:00
if (params.req_query) {
data.req_query = params.req_query;
2017-07-27 15:50:04 +08:00
}
2017-08-15 12:08:59 +08:00
if (params.req_body_other) {
data.req_body_other = params.req_body_other;
2017-07-27 15:50:04 +08:00
}
2017-07-10 11:56:53 +08:00
2017-08-18 12:37:32 +08:00
if (params.req_body_type) {
data.req_body_type = params.req_body_type;
}
2017-07-27 15:50:04 +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-10 11:56:53 +08:00
2017-08-20 22:13:46 +08:00
if (params.status) {
data.status = params.status;
}
2017-08-24 14:17:19 +08:00
_context6.prev = 38;
_context6.next = 41;
2017-07-10 11:56:53 +08:00
return this.Model.up(id, data);
2017-08-24 14:17:19 +08:00
case 41:
2017-08-18 20:35:31 +08:00
result = _context6.sent;
username = this.getUsername();
2017-08-24 19:27:48 +08:00
if (params.catid) {
this.catModel.get(+params.catid).then(function (cate) {
_yapi2.default.commons.saveLog({
content: '\u7528\u6237 "' + username + '" \u66F4\u65B0\u4E86\u5206\u7C7B "' + cate.name + '" \u4E0B\u7684\u63A5\u53E3 "' + data.title + '"',
type: 'project',
uid: _this3.getUid(),
username: username,
typeid: cate.project_id
});
});
} else {
cateid = interfaceData.catid;
this.catModel.get(cateid).then(function (cate) {
_yapi2.default.commons.saveLog({
content: '\u7528\u6237 "' + username + '" \u66F4\u65B0\u4E86\u5206\u7C7B "' + cate.name + '" \u4E0B\u7684\u63A5\u53E3 "' + data.title + '"',
type: 'project',
uid: _this3.getUid(),
username: username,
typeid: cate.project_id
});
});
}
2017-07-10 11:56:53 +08:00
ctx.body = _yapi2.default.commons.resReturn(result);
2017-08-24 19:27:48 +08:00
_context6.next = 50;
2017-07-10 11:56:53 +08:00
break;
2017-08-24 19:27:48 +08:00
case 47:
_context6.prev = 47;
2017-08-24 14:17:19 +08:00
_context6.t0 = _context6['catch'](38);
2017-07-10 11:56:53 +08:00
2017-08-18 20:35:31 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context6.t0.message);
2017-07-10 11:56:53 +08:00
2017-08-24 19:27:48 +08:00
case 50:
2017-07-10 11:56:53 +08:00
case 'end':
2017-08-18 20:35:31 +08:00
return _context6.stop();
2017-07-10 11:56:53 +08:00
}
}
2017-08-24 19:27:48 +08:00
}, _callee6, this, [[38, 47]]);
2017-07-10 11:56:53 +08:00
}));
2017-08-18 20:35:31 +08:00
function up(_x6) {
return _ref6.apply(this, arguments);
2017-07-10 11:56:53 +08:00
}
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 () {
2017-08-29 11:17:54 +08:00
var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(ctx) {
2017-08-24 19:27:48 +08:00
var _this4 = this;
var id, data, auth, inter, result, username;
2017-08-18 20:35:31 +08:00
return _regenerator2.default.wrap(function _callee7$(_context7) {
2017-07-10 11:56:53 +08:00
while (1) {
2017-08-18 20:35:31 +08:00
switch (_context7.prev = _context7.next) {
2017-07-10 11:56:53 +08:00
case 0:
2017-08-18 20:35:31 +08:00
_context7.prev = 0;
2017-07-10 11:56:53 +08:00
id = ctx.request.body.id;
if (id) {
2017-08-18 20:35:31 +08:00
_context7.next = 4;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-18 20:35:31 +08:00
return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口id不能为空'));
2017-07-10 11:56:53 +08:00
case 4:
2017-08-18 20:35:31 +08:00
_context7.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-08-18 20:35:31 +08:00
data = _context7.sent;
2017-07-11 18:22:20 +08:00
if (!(data.uid != this.getUid())) {
2017-08-18 20:35:31 +08:00
_context7.next = 13;
2017-07-11 18:22:20 +08:00
break;
}
2017-08-18 20:35:31 +08:00
_context7.next = 10;
2017-08-24 14:17:19 +08:00
return this.checkAuth(data.project_id, 'project', 'danger');
2017-07-11 18:22:20 +08:00
case 10:
2017-08-24 14:17:19 +08:00
auth = _context7.sent;
2017-07-10 11:56:53 +08:00
2017-08-24 14:17:19 +08:00
if (auth) {
2017-08-18 20:35:31 +08:00
_context7.next = 13;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-24 14:17:19 +08:00
return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '没有权限'));
2017-07-10 11:56:53 +08:00
2017-07-11 18:22:20 +08:00
case 13:
2017-08-18 20:35:31 +08:00
_context7.next = 15;
return this.Model.get(id);
2017-07-10 11:56:53 +08:00
2017-07-11 18:22:20 +08:00
case 15:
inter = _context7.sent;
_context7.next = 18;
return this.Model.del(id);
case 18:
2017-08-18 20:35:31 +08:00
result = _context7.sent;
2017-08-28 18:01:28 +08:00
_context7.next = 21;
return this.caseModel.delByInterfaceId(id);
case 21:
username = this.getUsername();
2017-08-24 19:27:48 +08:00
this.catModel.get(inter.catid).then(function (cate) {
_yapi2.default.commons.saveLog({
content: '\u7528\u6237 "' + username + '" \u5220\u9664\u4E86\u5206\u7C7B "' + cate.name + '" \u4E0B\u7684\u63A5\u53E3 "' + inter.title + '"',
type: 'project',
uid: _this4.getUid(),
username: username,
typeid: cate.project_id
});
});
2017-07-10 11:56:53 +08:00
ctx.body = _yapi2.default.commons.resReturn(result);
2017-08-28 18:01:28 +08:00
_context7.next = 29;
2017-07-10 11:56:53 +08:00
break;
2017-08-28 18:01:28 +08:00
case 26:
_context7.prev = 26;
2017-08-24 14:17:19 +08:00
_context7.t0 = _context7['catch'](0);
2017-07-10 11:56:53 +08:00
2017-08-24 14:17:19 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context7.t0.message);
2017-07-10 11:56:53 +08:00
2017-08-28 18:01:28 +08:00
case 29:
2017-07-10 11:56:53 +08:00
case 'end':
2017-08-18 20:35:31 +08:00
return _context7.stop();
2017-07-10 11:56:53 +08:00
}
}
2017-08-28 18:01:28 +08:00
}, _callee7, this, [[0, 26]]);
2017-07-10 11:56:53 +08:00
}));
2017-08-18 20:35:31 +08:00
function del(_x7) {
return _ref7.apply(this, arguments);
2017-07-10 11:56:53 +08:00
}
return del;
}()
}, {
key: 'solveConflict',
value: function () {
2017-08-29 11:17:54 +08:00
var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8(ctx) {
2017-08-24 19:27:48 +08:00
var _this5 = this;
2017-08-17 15:19:47 +08:00
var id, result, userInst, userinfo, data;
2017-08-18 20:35:31 +08:00
return _regenerator2.default.wrap(function _callee8$(_context8) {
while (1) {
2017-08-18 20:35:31 +08:00
switch (_context8.prev = _context8.next) {
case 0:
2017-08-18 20:35:31 +08:00
_context8.prev = 0;
2017-08-17 15:19:47 +08:00
id = parseInt(ctx.query.id, 10), result = void 0, userInst = void 0, userinfo = void 0, data = void 0;
if (id) {
2017-08-18 20:35:31 +08:00
_context8.next = 4;
break;
}
2017-08-18 20:35:31 +08:00
return _context8.abrupt('return', ctx.websocket.send("id 参数有误"));
2017-08-17 15:19:47 +08:00
case 4:
2017-08-18 20:35:31 +08:00
_context8.next = 6;
2017-08-17 15:19:47 +08:00
return this.Model.get(id);
2017-08-17 15:19:47 +08:00
case 6:
2017-08-18 20:35:31 +08:00
result = _context8.sent;
2017-08-17 15:19:47 +08:00
userinfo;
if (!(result.edit_uid !== 0 && result.edit_uid !== this.getUid())) {
2017-08-18 20:35:31 +08:00
_context8.next = 16;
2017-08-17 15:19:47 +08:00
break;
}
userInst = _yapi2.default.getInst(_user2.default);
2017-08-18 20:35:31 +08:00
_context8.next = 12;
2017-08-17 15:19:47 +08:00
return userInst.findById(result.edit_uid);
case 12:
2017-08-18 20:35:31 +08:00
userinfo = _context8.sent;
2017-08-17 15:19:47 +08:00
data = {
errno: result.edit_uid,
data: { uid: result.edit_uid, username: userinfo.username }
};
2017-08-18 20:35:31 +08:00
_context8.next = 18;
2017-08-17 15:19:47 +08:00
break;
2017-08-17 15:19:47 +08:00
case 16:
this.Model.upEditUid(id, this.getUid()).then();
data = {
errno: 0,
data: result
};
case 18:
ctx.websocket.send((0, _stringify2.default)(data));
2017-08-17 12:03:55 +08:00
ctx.websocket.on('close', function () {
2017-08-24 19:27:48 +08:00
_this5.Model.upEditUid(id, 0).then();
2017-08-17 12:03:55 +08:00
});
2017-08-18 20:35:31 +08:00
_context8.next = 25;
2017-08-17 15:19:47 +08:00
break;
2017-08-17 12:03:55 +08:00
2017-08-17 15:19:47 +08:00
case 22:
2017-08-18 20:35:31 +08:00
_context8.prev = 22;
_context8.t0 = _context8['catch'](0);
2017-08-17 15:19:47 +08:00
2017-08-18 20:35:31 +08:00
_yapi2.default.commons.log(_context8.t0, 'error');
2017-08-17 15:19:47 +08:00
case 25:
case 'end':
2017-08-18 20:35:31 +08:00
return _context8.stop();
}
}
2017-08-18 20:35:31 +08:00
}, _callee8, this, [[0, 22]]);
}));
2017-08-18 20:35:31 +08:00
function solveConflict(_x8) {
return _ref8.apply(this, arguments);
}
return solveConflict;
}()
2017-08-18 20:35:31 +08:00
}, {
key: 'addCat',
value: function () {
2017-08-29 11:17:54 +08:00
var _ref9 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee9(ctx) {
2017-08-24 14:17:19 +08:00
var params, auth, result, username;
2017-08-18 20:35:31 +08:00
return _regenerator2.default.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
_context9.prev = 0;
params = ctx.request.body;
params = _yapi2.default.commons.handleParams(params, {
name: 'string',
project_id: 'number',
desc: 'string'
});
if (params.project_id) {
_context9.next = 5;
break;
}
return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空'));
case 5:
2017-08-24 14:17:19 +08:00
_context9.next = 7;
return this.checkAuth(params.project_id, 'project', 'edit');
case 7:
auth = _context9.sent;
if (auth) {
_context9.next = 10;
break;
}
return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '没有权限'));
case 10:
2017-08-18 20:35:31 +08:00
if (params.name) {
2017-08-24 14:17:19 +08:00
_context9.next = 12;
2017-08-18 20:35:31 +08:00
break;
}
return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '名称不能为空'));
2017-08-24 14:17:19 +08:00
case 12:
_context9.next = 14;
2017-08-18 20:35:31 +08:00
return this.catModel.save({
name: params.name,
project_id: params.project_id,
desc: params.desc,
uid: this.getUid(),
add_time: _yapi2.default.commons.time(),
up_time: _yapi2.default.commons.time()
});
2017-08-24 14:17:19 +08:00
case 14:
2017-08-18 20:35:31 +08:00
result = _context9.sent;
username = this.getUsername();
_yapi2.default.commons.saveLog({
content: '\u7528\u6237 "' + username + '" \u6DFB\u52A0\u4E86\u5206\u7C7B "' + params.name + '"',
type: 'project',
uid: this.getUid(),
username: username,
typeid: params.project_id
});
2017-08-18 20:35:31 +08:00
ctx.body = _yapi2.default.commons.resReturn(result);
2017-08-24 14:17:19 +08:00
_context9.next = 23;
2017-08-18 20:35:31 +08:00
break;
2017-08-24 14:17:19 +08:00
case 20:
_context9.prev = 20;
2017-08-18 20:35:31 +08:00
_context9.t0 = _context9['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context9.t0.message);
2017-08-24 14:17:19 +08:00
case 23:
2017-08-18 20:35:31 +08:00
case 'end':
return _context9.stop();
}
}
2017-08-24 14:17:19 +08:00
}, _callee9, this, [[0, 20]]);
2017-08-18 20:35:31 +08:00
}));
function addCat(_x9) {
return _ref9.apply(this, arguments);
}
return addCat;
}()
}, {
key: 'upCat',
value: function () {
2017-08-29 11:17:54 +08:00
var _ref10 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee10(ctx) {
2017-08-24 14:17:19 +08:00
var params, result, username, cate, auth;
2017-08-18 20:35:31 +08:00
return _regenerator2.default.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
_context10.prev = 0;
params = ctx.request.body;
_context10.next = 4;
return this.catModel.up(params.catid, {
2017-08-19 19:06:09 +08:00
name: params.name,
desc: params.desc,
2017-08-18 20:35:31 +08:00
up_time: _yapi2.default.commons.time()
});
case 4:
result = _context10.sent;
username = this.getUsername();
_context10.next = 8;
return this.catModel.get(params.catid);
case 8:
cate = _context10.sent;
2017-08-24 14:17:19 +08:00
_context10.next = 11;
return this.checkAuth(cate.project_id, 'project', 'edit');
2017-08-24 14:17:19 +08:00
case 11:
auth = _context10.sent;
if (auth) {
_context10.next = 14;
break;
}
return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '没有权限'));
case 14:
_yapi2.default.commons.saveLog({
content: '\u7528\u6237 "' + username + '" \u66F4\u65B0\u4E86\u5206\u7C7B "' + cate.name + '"',
type: 'project',
uid: this.getUid(),
username: username,
typeid: cate.project_id
});
2017-08-18 20:35:31 +08:00
ctx.body = _yapi2.default.commons.resReturn(result);
2017-08-24 14:17:19 +08:00
_context10.next = 21;
2017-08-18 20:35:31 +08:00
break;
2017-08-24 14:17:19 +08:00
case 18:
_context10.prev = 18;
2017-08-18 20:35:31 +08:00
_context10.t0 = _context10['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 400, _context10.t0.message);
2017-08-24 14:17:19 +08:00
case 21:
2017-08-18 20:35:31 +08:00
case 'end':
return _context10.stop();
}
}
2017-08-24 14:17:19 +08:00
}, _callee10, this, [[0, 18]]);
2017-08-18 20:35:31 +08:00
}));
function upCat(_x10) {
return _ref10.apply(this, arguments);
}
return upCat;
}()
}, {
key: 'delCat',
value: function () {
2017-08-29 11:17:54 +08:00
var _ref11 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee11(ctx) {
2017-08-24 19:27:48 +08:00
var id, catData, auth, username, result, r;
2017-08-18 20:35:31 +08:00
return _regenerator2.default.wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
_context11.prev = 0;
id = ctx.request.body.catid;
_context11.next = 4;
return this.catModel.get(id);
case 4:
catData = _context11.sent;
if (!catData) {
ctx.body = _yapi2.default.commons.resReturn(null, 400, "不存在的分类");
}
if (!(catData.uid !== this.getUid())) {
_context11.next = 12;
break;
}
_context11.next = 9;
return this.checkAuth(catData.project_id, 'project', 'danger');
case 9:
auth = _context11.sent;
if (auth) {
_context11.next = 12;
break;
}
return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '没有权限'));
case 12:
username = this.getUsername();
_yapi2.default.commons.saveLog({
2017-08-24 19:27:48 +08:00
content: '\u7528\u6237 "' + username + '" \u5220\u9664\u4E86\u5206\u7C7B "' + catData.name + '" \u53CA\u8BE5\u5206\u7C7B\u4E0B\u7684\u63A5\u53E3',
type: 'project',
uid: this.getUid(),
username: username,
2017-08-24 19:27:48 +08:00
typeid: catData.project_id
});
2017-08-24 19:27:48 +08:00
_context11.next = 16;
return this.catModel.del(id);
case 16:
result = _context11.sent;
_context11.next = 19;
return this.Model.delByCatid(id);
case 19:
r = _context11.sent;
2017-08-19 19:06:09 +08:00
return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(r));
2017-08-18 20:35:31 +08:00
2017-08-24 19:27:48 +08:00
case 23:
_context11.prev = 23;
2017-08-18 20:35:31 +08:00
_context11.t0 = _context11['catch'](0);
_yapi2.default.commons.resReturn(null, 400, _context11.t0.message);
2017-08-24 19:27:48 +08:00
case 26:
2017-08-18 20:35:31 +08:00
case 'end':
return _context11.stop();
}
}
2017-08-24 19:27:48 +08:00
}, _callee11, this, [[0, 23]]);
2017-08-18 20:35:31 +08:00
}));
function delCat(_x11) {
return _ref11.apply(this, arguments);
}
return delCat;
}()
2017-07-10 11:56:53 +08:00
}]);
return interfaceController;
}(_base2.default);
module.exports = interfaceController;