fix: 预览页修改

This commit is contained in:
qitmac000249 2017-08-22 11:23:00 +08:00
parent 077d557875
commit 052dd68f5a
8 changed files with 224 additions and 94 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
//
{
}

View File

@ -28,7 +28,7 @@ class View extends Component {
req_body_form(req_body_type,req_body_form){
if(req_body_type === 'json'){
return <div className="colBody">
return <div style={{display:this.props.curData.req_body_other?"":"none"}} className="colBody">
<span className="colKey">请求Body</span>
<div id="vreq_body_json" style={{ minHeight: "300px" }}></div>
</div>
@ -62,21 +62,21 @@ class View extends Component {
})
}
return <div className="colBody">
return <div style={{display:dataSource.length?"":"none"}} className="colBody">
<span className="colKey">请求Body</span>
<Table bordered size="small" pagination = {false} columns= {columns} dataSource = {dataSource} />
</div>
}else if(req_body_type === 'file'){
return <div className="colBody">
return <div style={{display:this.props.curData.req_body_other?"":"none"}} className="colBody">
<span className="colKey">请求Body</span>
<div>{this.props.curData.req_body_other}</div>
</div>
}else if(req_body_type === 'raw'){
return <div className="colBody">
return <div style={{display:this.props.curData.req_body_other?"":"none"}} className="colBody">
<span className="colKey">请求Body</span>
<div>{this.props.curData.req_body_other}</div>
</div>
@ -84,12 +84,12 @@ class View extends Component {
}
res_body(res_body_type,res_body){
if(res_body_type === 'json'){
return <div className="colBody">
return <div style={{display:this.props.curData.req_body_other?"":"none"}} className="colBody">
<span className="colKey">返回Body</span>
<div id="vres_body_json" style={{ minHeight: "300px" }}></div>
</div>
}else if(res_body_type === 'raw'){
return <div className="colBody">
return <div style={{display:this.props.curData.req_body_other?"":"none"}} className="colBody">
<span className="colKey">返回Body</span>
<div>{res_body}</div>
</div>
@ -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 {
<span className="colKey">更新时间</span>
<span className="colValue">{formatTime(this.props.curData.up_time)}</span>
</div>
<div className="colDesc">
{this.props.curData.desc?<div className="colDesc">
<span className="colKey">接口描述</span>
<span className="colValue">{this.props.curData.desc}</span>
</div>
<div className="colHeader">
</div>:""}
{dataSource.length?<div className="colHeader">
<span className="colKey">请求Headers</span>
<Table bordered size="small" pagination = {false} columns= {columns} dataSource = {dataSource} />
</div>
<div className="colQuery">
</div>:""}
{this.props.curData.req_query&&this.props.curData.req_query.length?<div className="colQuery">
<span className="colKey">Query</span>
{this.req_query(this.props.curData.req_query)}
</div>
<div className="colreqBodyType">
</div>:""}
{/*<div className="colreqBodyType">
<span className="colKey">请求Body类型</span>
<span className="colValue">{this.props.curData.req_body_type}</span>
</div>
</div>*/}
{this.req_body_form(this.props.curData.req_body_type,this.props.curData.req_body_form)}
<div className="colreqBodyType">
{/*<div className="colreqBodyType">
<span className="colKey">返回Body类型</span>
<span className="colValue">{this.props.curData.res_body_type}</span>
</div>
</div>*/}
{this.res_body(this.props.curData.res_body_type,this.props.curData.res_body)}
</div>;
if(!this.props.curData.title){

View File

@ -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}`,

View File

@ -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 });
}
}

View File

@ -169,6 +169,10 @@ const routerConfig = {
],
"project": [
{
"action": "upSet",
"path": "upset",
"method": "post"
},{
"action": "add",
"path": "add",
"method": "post"

View File

@ -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;

View File

@ -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;

View File

@ -183,6 +183,10 @@ var routerConfig = {
method: "post"
}],
"project": [{
"action": "upSet",
"path": "upset",
"method": "post"
}, {
"action": "add",
"path": "add",
"method": "post"