From 8c928a1e1948a4d2999c6ccae7ec0a36a279fdee Mon Sep 17 00:00:00 2001 From: sean Date: Tue, 29 Aug 2017 13:56:49 +0800 Subject: [PATCH] opti: restful api --- server/controllers/interface.js | 40 ++++++++++++----- server_dist/controllers/interface.js | 64 +++++++++++++++++++--------- 2 files changed, 72 insertions(+), 32 deletions(-) diff --git a/server/controllers/interface.js b/server/controllers/interface.js index 1d3c2c64..e3422aed 100644 --- a/server/controllers/interface.js +++ b/server/controllers/interface.js @@ -2,6 +2,7 @@ import interfaceModel from '../models/interface.js'; import interfaceCatModel from '../models/interfaceCat.js'; import interfaceCaseModel from '../models/interfaceCase.js' +import _ from 'underscore'; import baseController from './base.js'; import yapi from '../yapi.js'; import userModel from '../models/user.js'; @@ -61,6 +62,7 @@ class interfaceController extends baseController { } params.method = params.method || 'GET'; params.method = params.method.toUpperCase(); + params.req_params = params.req_params || []; params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json'; if (!params.project_id) { @@ -105,7 +107,23 @@ class interfaceController extends baseController { if (params.req_body_form) { data.req_body_form = params.req_body_form; } - if (params.req_params && Array.isArray(params.req_params) && params.req_params.length > 0) { + + if (params.path.indexOf(":") > 0) { + let paths = params.path.split("/"), name, i; + for (i = 1; i < paths.length; i++) { + if (paths[i][0] === ':') { + name = paths[i].substr(1); + if (!_.find(params.req_params, { name: name })) { + params.req_params.push({ + name: name, + desc: '' + }) + } + } + } + } + + if ( params.req_params.length > 0) { data.type = 'var' data.req_params = params.req_params; } else { @@ -116,9 +134,9 @@ class interfaceController extends baseController { } let result = await this.Model.save(data); - + // let project = await this.projectModel.get(params.project_id); - this.catModel.get(params.catid).then((cate)=>{ + this.catModel.get(params.catid).then((cate) => { let username = this.getUsername(); yapi.commons.saveLog({ content: `用户 "${username}" 为分类 "${cate.name}" 添加了接口 "${data.title}"`, @@ -128,7 +146,7 @@ class interfaceController extends baseController { typeid: params.project_id }); }); - + ctx.body = yapi.commons.resReturn(result); } catch (e) { ctx.body = yapi.commons.resReturn(null, 402, e.message); @@ -278,7 +296,7 @@ class interfaceController extends baseController { return ctx.body = yapi.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/'); } - if (params.path && (params.path !== interfaceData.path || params.method !== interfaceData.method) ) { + if (params.path && (params.path !== interfaceData.path || params.method !== interfaceData.method)) { let checkRepeat = await this.Model.checkRepeat(interfaceData.project_id, params.path, params.method); if (checkRepeat > 0) { return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']'); @@ -346,7 +364,7 @@ class interfaceController extends baseController { let result = await this.Model.up(id, data); let username = this.getUsername(); if (params.catid) { - this.catModel.get(+params.catid).then((cate)=>{ + this.catModel.get(+params.catid).then((cate) => { yapi.commons.saveLog({ content: `用户 "${username}" 更新了分类 "${cate.name}" 下的接口 "${data.title}"`, type: 'project', @@ -357,7 +375,7 @@ class interfaceController extends baseController { }); } else { let cateid = interfaceData.catid; - this.catModel.get(cateid).then((cate)=>{ + this.catModel.get(cateid).then((cate) => { yapi.commons.saveLog({ content: `用户 "${username}" 更新了分类 "${cate.name}" 下的接口 "${data.title}"`, type: 'project', @@ -368,7 +386,7 @@ class interfaceController extends baseController { }); } - + ctx.body = yapi.commons.resReturn(result); } catch (e) { ctx.body = yapi.commons.resReturn(null, 402, e.message); @@ -408,7 +426,7 @@ class interfaceController extends baseController { let result = await this.Model.del(id); await this.caseModel.delByInterfaceId(id); let username = this.getUsername(); - this.catModel.get(inter.catid).then((cate)=>{ + this.catModel.get(inter.catid).then((cate) => { yapi.commons.saveLog({ content: `用户 "${username}" 删除了分类 "${cate.name}" 下的接口 "${inter.title}"`, type: 'project', @@ -417,7 +435,7 @@ class interfaceController extends baseController { typeid: cate.project_id }); }) - + ctx.body = yapi.commons.resReturn(result); } catch (err) { @@ -507,7 +525,7 @@ class interfaceController extends baseController { name: params.name, desc: params.desc, up_time: yapi.commons.time() - }); + }); let username = this.getUsername(); let cate = await this.catModel.get(params.catid); diff --git a/server_dist/controllers/interface.js b/server_dist/controllers/interface.js index f127239e..d2773b0e 100644 --- a/server_dist/controllers/interface.js +++ b/server_dist/controllers/interface.js @@ -44,6 +44,10 @@ var _interfaceCase = require('../models/interfaceCase.js'); var _interfaceCase2 = _interopRequireDefault(_interfaceCase); +var _underscore = require('underscore'); + +var _underscore2 = _interopRequireDefault(_underscore); + var _base = require('./base.js'); var _base2 = _interopRequireDefault(_base); @@ -113,7 +117,7 @@ var interfaceController = function (_baseController) { var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(ctx) { var _this2 = this; - var params, auth, checkRepeat, data, result; + var params, auth, checkRepeat, data, paths, name, i, result; return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { @@ -145,47 +149,48 @@ var interfaceController = function (_baseController) { case 7: params.method = params.method || 'GET'; params.method = params.method.toUpperCase(); + params.req_params = params.req_params || []; params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json'; if (params.project_id) { - _context.next = 12; + _context.next = 13; break; } return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); - case 12: + case 13: if (params.path) { - _context.next = 14; + _context.next = 15; break; } return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口请求路径不能为空')); - case 14: + case 15: if (_yapi2.default.commons.verifyPath(params.path)) { - _context.next = 16; + _context.next = 17; break; } return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/')); - case 16: - _context.next = 18; + case 17: + _context.next = 19; return this.Model.checkRepeat(params.project_id, params.path, params.method); - case 18: + case 19: checkRepeat = _context.sent; if (!(checkRepeat > 0)) { - _context.next = 21; + _context.next = 22; break; } return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']')); - case 21: - _context.prev = 21; + case 22: + _context.prev = 22; data = { project_id: params.project_id, catid: params.catid, @@ -210,7 +215,24 @@ var interfaceController = function (_baseController) { if (params.req_body_form) { data.req_body_form = params.req_body_form; } - if (params.req_params && Array.isArray(params.req_params) && params.req_params.length > 0) { + + if (params.path.indexOf(":") > 0) { + paths = params.path.split("/"), name = void 0, i = void 0; + + for (i = 1; i < paths.length; i++) { + if (paths[i][0] === ':') { + name = paths[i].substr(1); + if (!_underscore2.default.find(params.req_params, { name: name })) { + params.req_params.push({ + name: name, + desc: '' + }); + } + } + } + } + + if (params.req_params.length > 0) { data.type = 'var'; data.req_params = params.req_params; } else { @@ -220,10 +242,10 @@ var interfaceController = function (_baseController) { data.req_body_other = params.req_body_other; } - _context.next = 29; + _context.next = 31; return this.Model.save(data); - case 29: + case 31: result = _context.sent; @@ -240,21 +262,21 @@ var interfaceController = function (_baseController) { }); ctx.body = _yapi2.default.commons.resReturn(result); - _context.next = 37; + _context.next = 39; break; - case 34: - _context.prev = 34; - _context.t0 = _context['catch'](21); + case 36: + _context.prev = 36; + _context.t0 = _context['catch'](22); ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t0.message); - case 37: + case 39: case 'end': return _context.stop(); } } - }, _callee, this, [[21, 34]]); + }, _callee, this, [[22, 36]]); })); function add(_x) {