From 052dd68f5a284a50559dcb52740a18fabd8278b4 Mon Sep 17 00:00:00 2001 From: qitmac000249 Date: Tue, 22 Aug 2017 11:23:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=A2=84=E8=A7=88=E9=A1=B5=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 + .../Project/Interface/InterfaceList/View.js | 42 ++-- server/controllers/project.js | 40 +++- server/models/follow.js | 11 +- server/router.js | 4 + server_dist/controllers/project.js | 203 ++++++++++++------ server_dist/models/follow.js | 11 +- server_dist/router.js | 4 + 8 files changed, 224 insertions(+), 94 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..d6b19400 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +// 将设置放入此文件中以覆盖默认值和用户设置。 +{ +} \ No newline at end of file diff --git a/client/containers/Project/Interface/InterfaceList/View.js b/client/containers/Project/Interface/InterfaceList/View.js index 1ae31240..b97a5fa8 100644 --- a/client/containers/Project/Interface/InterfaceList/View.js +++ b/client/containers/Project/Interface/InterfaceList/View.js @@ -28,7 +28,7 @@ class View extends Component { req_body_form(req_body_type,req_body_form){ if(req_body_type === 'json'){ - return
+ return
请求Body:
@@ -62,21 +62,21 @@ class View extends Component { }) } - return
+ return
请求Body: }else if(req_body_type === 'file'){ - return
+ return
请求Body:
{this.props.curData.req_body_other}
}else if(req_body_type === 'raw'){ - return
+ return
请求Body:
{this.props.curData.req_body_other}
@@ -84,12 +84,12 @@ class View extends Component { } res_body(res_body_type,res_body){ if(res_body_type === 'json'){ - return
+ return
返回Body:
}else if(res_body_type === 'raw'){ - return
+ return
返回Body:
{res_body}
@@ -129,7 +129,7 @@ class View extends Component { bindAceEditor(){ - if(!this.state.init&&(this.props.curData.req_body_type === "json"&&this.props.curData.title)){ + if(this.props.curData.req_body_type === "json"&&this.props.curData.title){ mockEditor({ container: 'vreq_body_json', data: this.props.curData.req_body_other, @@ -137,7 +137,7 @@ class View extends Component { onChange: function () {} }) } - if(!this.state.init&&(this.props.curData.res_body_type === "json"&&this.props.curData.title)){ + if(this.props.curData.title&&this.props.curData.req_body_type === "json"){ mockEditor({ container: 'vres_body_json', data: this.props.curData.res_body, @@ -147,9 +147,12 @@ class View extends Component { } } componentDidMount(){ - if(!this.state.init){ + if(this.props.curData.title){ this.bindAceEditor.bind(this)(); } + if(!this.props.curData.title&&this.state.init){ + this.setState({init: false}); + } } componentDidUpdate(){ this.bindAceEditor.bind(this)(); @@ -161,7 +164,6 @@ class View extends Component { } render () { const dataSource = []; - console.log(this.props.curData) if(this.props.curData.req_headers&&this.props.curData.req_headers.length){ this.props.curData.req_headers.map((item,i)=>{ dataSource.push({ @@ -216,27 +218,27 @@ class View extends Component { 更新时间: {formatTime(this.props.curData.up_time)}
-
+ {this.props.curData.desc?
接口描述: {this.props.curData.desc} -
-
+
:""} + {dataSource.length?
请求Headers:
- -
+
:""} + {this.props.curData.req_query&&this.props.curData.req_query.length?
Query: {this.req_query(this.props.curData.req_query)} -
-
+
:""} + {/*
请求Body类型: {this.props.curData.req_body_type} -
+ */} {this.req_body_form(this.props.curData.req_body_type,this.props.curData.req_body_form)} -
+ {/*
返回Body类型: {this.props.curData.res_body_type} -
+
*/} {this.res_body(this.props.curData.res_body_type,this.props.curData.res_body)} ; if(!this.props.curData.title){ diff --git a/server/controllers/project.js b/server/controllers/project.js index ffb7ef5d..e97e5491 100644 --- a/server/controllers/project.js +++ b/server/controllers/project.js @@ -8,6 +8,7 @@ import groupModel from '../models/group'; import commons from '../utils/commons.js'; import userModel from '../models/user.js'; import logModel from '../models/log.js'; +import followModel from '../models/follow.js'; import Mock from 'mockjs'; const send = require('koa-send'); @@ -18,6 +19,7 @@ class projectController extends baseController { this.Model = yapi.getInst(projectModel); this.groupModel = yapi.getInst(groupModel); this.logModel = yapi.getInst(logModel); + this.followModel = yapi.getInst(followModel); } handleBasepath(basepath) { @@ -442,7 +444,26 @@ class projectController extends baseController { * @returns {Object} * @example ./api/project/up.json */ - + async upSet(ctx){ + let id = ctx.request.body.id; + let data = {}; + data.color = ctx.request.body.color; + data.icon = ctx.request.body.icon; + if(!id){ + return ctx.body = yapi.commons.resReturn(null, 405, '项目id不能为空'); + } + try{ + let result = await this.Model.up(id, data); + ctx.body = yapi.commons.resReturn(result); + }catch(e){ + ctx.body = yapi.commons.resReturn(null, 402, e.message); + } + try{ + this.followModel.updateById(this.getUid(),id,data).then(); + }catch(e){ + yapi.commons.log(e, 'error'); // eslint-disable-line + } + } async up(ctx) { try { let id = ctx.request.body.id; @@ -494,7 +515,22 @@ class projectController extends baseController { if(params.color) data.color = params.color; if(params.icon) data.icon = params.icon; let result = await this.Model.up(id, data); - + // try{ + // let data = {}; + // if(params.name){ + // data.projectname = params.name; + // } + // if(params.icon){ + // data.icon = params.icon; + // } + // if(params.color){ + // data.color = params.color; + // } + // this.followModel.updateById(this.getUid(),id,data); + // }catch(e){ + // yapi.commons.log(e, 'error'); // eslint-disable-line + // } + let username = this.getUsername(); yapi.commons.saveLog({ content: `用户${username}更新了项目${projectData.name}`, diff --git a/server/models/follow.js b/server/models/follow.js index c720148d..bf2be750 100644 --- a/server/models/follow.js +++ b/server/models/follow.js @@ -62,13 +62,12 @@ class followModel extends baseModel { }); } - updateById(id,data){ + updateById(id,typeid,data){ + return this.model.update({ - _id: id - },{ - icon: data.icon, - color: data.color - }); + uid: id, + projectid: typeid + },data,{runValidators: true }); } } diff --git a/server/router.js b/server/router.js index 42820d60..68f42222 100644 --- a/server/router.js +++ b/server/router.js @@ -169,6 +169,10 @@ const routerConfig = { ], "project": [ { + "action": "upSet", + "path": "upset", + "method": "post" + },{ "action": "add", "path": "add", "method": "post" diff --git a/server_dist/controllers/project.js b/server_dist/controllers/project.js index c59cc6ee..26c85c0a 100644 --- a/server_dist/controllers/project.js +++ b/server_dist/controllers/project.js @@ -72,6 +72,10 @@ var _log = require('../models/log.js'); var _log2 = _interopRequireDefault(_log); +var _follow = require('../models/follow.js'); + +var _follow2 = _interopRequireDefault(_follow); + var _mockjs = require('mockjs'); var _mockjs2 = _interopRequireDefault(_mockjs); @@ -91,6 +95,7 @@ var projectController = function (_baseController) { _this.Model = _yapi2.default.getInst(_project2.default); _this.groupModel = _yapi2.default.getInst(_group2.default); _this.logModel = _yapi2.default.getInst(_log2.default); + _this.followModel = _yapi2.default.getInst(_follow2.default); return _this; } @@ -1012,15 +1017,76 @@ var projectController = function (_baseController) { */ }, { - key: 'up', + key: 'upSet', value: function () { var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(ctx) { - var id, params, projectData, checkRepeat, data, result, username; + var id, data, result; return _regenerator2.default.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: - _context10.prev = 0; + id = ctx.request.body.id; + data = {}; + + data.color = ctx.request.body.color; + data.icon = ctx.request.body.icon; + + if (id) { + _context10.next = 6; + break; + } + + return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '项目id不能为空')); + + case 6: + _context10.prev = 6; + _context10.next = 9; + return this.Model.up(id, data); + + case 9: + result = _context10.sent; + + ctx.body = _yapi2.default.commons.resReturn(result); + _context10.next = 16; + break; + + case 13: + _context10.prev = 13; + _context10.t0 = _context10['catch'](6); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context10.t0.message); + + case 16: + try { + this.followModel.updateById(this.getUid(), id, data).then(); + } catch (e) { + _yapi2.default.commons.log(e, 'error'); // eslint-disable-line + } + + case 17: + case 'end': + return _context10.stop(); + } + } + }, _callee10, this, [[6, 13]]); + })); + + function upSet(_x11) { + return _ref10.apply(this, arguments); + } + + return upSet; + }() + }, { + key: 'up', + value: function () { + var _ref11 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(ctx) { + var id, params, projectData, checkRepeat, data, result, username; + return _regenerator2.default.wrap(function _callee11$(_context11) { + while (1) { + switch (_context11.prev = _context11.next) { + case 0: + _context11.prev = 0; id = ctx.request.body.id; params = ctx.request.body; @@ -1035,39 +1101,39 @@ var projectController = function (_baseController) { }); if (id) { - _context10.next = 7; + _context11.next = 7; break; } - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '项目id不能为空')); + return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '项目id不能为空')); case 7: - _context10.next = 9; + _context11.next = 9; return this.checkAuth(id, 'project', 'edit'); case 9: - _context10.t0 = _context10.sent; + _context11.t0 = _context11.sent; - if (!(_context10.t0 !== true)) { - _context10.next = 12; + if (!(_context11.t0 !== true)) { + _context11.next = 12; break; } - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); + return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); case 12: - _context10.next = 14; + _context11.next = 14; return this.Model.get(id); case 14: - projectData = _context10.sent; + projectData = _context11.sent; if (!((params.basepath = this.handleBasepath(params.basepath)) === false)) { - _context10.next = 17; + _context11.next = 17; break; } - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, 'basepath格式有误')); + return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, 'basepath格式有误')); case 17: @@ -1076,22 +1142,22 @@ var projectController = function (_baseController) { } if (!params.name) { - _context10.next = 24; + _context11.next = 24; break; } - _context10.next = 21; + _context11.next = 21; return this.Model.checkNameRepeat(params.name); case 21: - checkRepeat = _context10.sent; + checkRepeat = _context11.sent; if (!(checkRepeat > 0)) { - _context10.next = 24; + _context11.next = 24; break; } - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的项目名')); + return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的项目名')); case 24: data = { @@ -1107,11 +1173,28 @@ var projectController = function (_baseController) { if (params.env) data.env = params.env; if (params.color) data.color = params.color; if (params.icon) data.icon = params.icon; - _context10.next = 33; + _context11.next = 33; return this.Model.up(id, data); case 33: - result = _context10.sent; + result = _context11.sent; + + // try{ + // let data = {}; + // if(params.name){ + // data.projectname = params.name; + // } + // if(params.icon){ + // data.icon = params.icon; + // } + // if(params.color){ + // data.color = params.color; + // } + // this.followModel.updateById(this.getUid(),id,data); + // }catch(e){ + // yapi.commons.log(e, 'error'); // eslint-disable-line + // } + username = this.getUsername(); _yapi2.default.commons.saveLog({ @@ -1124,25 +1207,25 @@ var projectController = function (_baseController) { color: params.color }); ctx.body = _yapi2.default.commons.resReturn(result); - _context10.next = 42; + _context11.next = 42; break; case 39: - _context10.prev = 39; - _context10.t1 = _context10['catch'](0); + _context11.prev = 39; + _context11.t1 = _context11['catch'](0); - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context10.t1.message); + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context11.t1.message); case 42: case 'end': - return _context10.stop(); + return _context11.stop(); } } - }, _callee10, this, [[0, 39]]); + }, _callee11, this, [[0, 39]]); })); - function up(_x11) { - return _ref10.apply(this, arguments); + function up(_x12) { + return _ref11.apply(this, arguments); } return up; @@ -1162,40 +1245,40 @@ var projectController = function (_baseController) { }, { key: 'search', value: function () { - var _ref11 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(ctx) { + var _ref12 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(ctx) { var q, projectList, groupList, projectRules, groupRules, queryList; - return _regenerator2.default.wrap(function _callee11$(_context11) { + return _regenerator2.default.wrap(function _callee12$(_context12) { while (1) { - switch (_context11.prev = _context11.next) { + switch (_context12.prev = _context12.next) { case 0: q = ctx.request.query.q; if (q) { - _context11.next = 3; + _context12.next = 3; break; } - return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'No keyword.')); + return _context12.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'No keyword.')); case 3: if (_yapi2.default.commons.validateSearchKeyword(q)) { - _context11.next = 5; + _context12.next = 5; break; } - return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'Bad query.')); + return _context12.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'Bad query.')); case 5: - _context11.next = 7; + _context12.next = 7; return this.Model.search(q); case 7: - projectList = _context11.sent; - _context11.next = 10; + projectList = _context12.sent; + _context12.next = 10; return this.groupModel.search(q); case 10: - groupList = _context11.sent; + groupList = _context12.sent; projectRules = ['_id', 'name', 'basepath', 'uid', 'env', 'members', { key: 'group_id', alias: 'groupId' }, { key: 'up_time', alias: 'upTime' }, { key: 'add_time', alias: 'addTime' }]; groupRules = ['_id', 'uid', { key: 'group_name', alias: 'groupName' }, { key: 'group_desc', alias: 'groupDesc' }, { key: 'add_time', alias: 'addTime' }, { key: 'up_time', alias: 'upTime' }]; @@ -1207,18 +1290,18 @@ var projectController = function (_baseController) { project: projectList, group: groupList }; - return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 0, 'ok')); + return _context12.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 0, 'ok')); case 17: case 'end': - return _context11.stop(); + return _context12.stop(); } } - }, _callee11, this); + }, _callee12, this); })); - function search(_x12) { - return _ref11.apply(this, arguments); + function search(_x13) { + return _ref12.apply(this, arguments); } return search; @@ -1237,36 +1320,36 @@ var projectController = function (_baseController) { }, { key: 'download', value: function () { - var _ref12 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(ctx) { + var _ref13 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(ctx) { var project_id, interfaceInst, count, arr, fileName, res; - return _regenerator2.default.wrap(function _callee12$(_context12) { + return _regenerator2.default.wrap(function _callee13$(_context13) { while (1) { - switch (_context12.prev = _context12.next) { + switch (_context13.prev = _context13.next) { case 0: project_id = ctx.request.query.project_id; interfaceInst = _yapi2.default.getInst(_interface2.default); // 根据 project_id 获取接口数据 - _context12.next = 4; + _context13.next = 4; return interfaceInst.list(project_id); case 4: - count = _context12.sent; + count = _context13.sent; if (project_id) { - _context12.next = 9; + _context13.next = 9; break; } - return _context12.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '项目id不能为空')); + return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '项目id不能为空')); case 9: if (count) { - _context12.next = 11; + _context13.next = 11; break; } - return _context12.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '项目id不存在')); + return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '项目id不存在')); case 11: arr = (0, _stringify2.default)(count.map(function (item) { @@ -1280,23 +1363,23 @@ var projectController = function (_baseController) { fileName = 'mock.js'; ctx.attachment(fileName); - _context12.next = 16; + _context13.next = 16; return send(ctx, fileName, { root: __dirname + '/public' }); case 16: res = ('\n var Mock = require(\'mockjs\');\n var xhook = require(\'xhook\');\n var data = ' + arr + ';\n function run() {\n xhook.before(function(request, callback) {\n setTimeout(function() {\n var res;\n data.forEach((item) => {\n // \u8BF7\u6C42\u7684\u63A5\u53E3\u5728 data \u4E2D\u5B58\u5728\n if(request.url === item.path) {\n res = {\n status: 200,\n text: Mock.mock(item.mock)\n }\n }\n });\n if (res) {\n callback(res);\n }else {\n callback({ status: 405, text: \'\u63A5\u53E3\u4E0D\u5B58\u5728\' });\n }\n }, 500);\n });\n }\n module.exports = run;').trim(); - return _context12.abrupt('return', ctx.body = res); + return _context13.abrupt('return', ctx.body = res); case 18: case 'end': - return _context12.stop(); + return _context13.stop(); } } - }, _callee12, this); + }, _callee13, this); })); - function download(_x13) { - return _ref12.apply(this, arguments); + function download(_x14) { + return _ref13.apply(this, arguments); } return download; diff --git a/server_dist/models/follow.js b/server_dist/models/follow.js index 5fd2bd08..8ac49948 100644 --- a/server_dist/models/follow.js +++ b/server_dist/models/follow.js @@ -106,13 +106,12 @@ var followModel = function (_baseModel) { } }, { key: 'updateById', - value: function updateById(id, data) { + value: function updateById(id, typeid, data) { + return this.model.update({ - _id: id - }, { - icon: data.icon, - color: data.color - }); + uid: id, + projectid: typeid + }, data, { runValidators: true }); } }]); return followModel; diff --git a/server_dist/router.js b/server_dist/router.js index 8728b0f9..e044e52b 100644 --- a/server_dist/router.js +++ b/server_dist/router.js @@ -183,6 +183,10 @@ var routerConfig = { method: "post" }], "project": [{ + "action": "upSet", + "path": "upset", + "method": "post" + }, { "action": "add", "path": "add", "method": "post"