feat: 添加project/download接口

This commit is contained in:
wenbo.dong 2017-08-10 10:24:24 +08:00
parent 5dd297366a
commit 8fbdfd7eaa
13 changed files with 194 additions and 121 deletions

View File

@ -1,11 +1,9 @@
import React, { Component } from 'react';
import './Follows.scss';
import Subnav from '../../components/Subnav/Subnav.js';
console.log('js in');
class Follows extends Component {
render () {
console.log('render');
return (
<div>
<Subnav

View File

@ -187,13 +187,14 @@ class UpDateModal extends Component {
rules: [{
required: false,
whitespace: true,
message: "请输入环境名称",
validator(rule, value, callback) {
if (value) {
if (value.length === 0) {
callback('请输入环境域名');
} else if (!/\S/.test(value)) {
callback('请输入环境域名');
} else if (/prd/.test(value)) {
callback('环境域名不能是"prd"');
} else {
return callback();
}
@ -319,7 +320,7 @@ class UpDateModal extends Component {
label={(
<span>
基本路径&nbsp;
<Tooltip title="基本路径为空根路径">
<Tooltip title="基本路径为空表示根路径">
<Icon type="question-circle-o" />
</Tooltip>
</span>

View File

@ -5,6 +5,8 @@ import interfaceModel from '../models/interface.js';
import groupModel from '../models/group';
import commons from '../utils/commons.js';
import userModel from '../models/user.js';
import Mock from 'mockjs';
const send = require('koa-send');
class projectController extends baseController {
@ -77,7 +79,7 @@ class projectController extends baseController {
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的项目名');
}
if (!params.prd_host) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目domain不能为空');
}
@ -98,7 +100,7 @@ class projectController extends baseController {
return ctx.body = yapi.commons.resReturn(null, 401, '已存在domain和basepath');
}
let data = {
name: params.name,
@ -513,6 +515,41 @@ class projectController extends baseController {
return ctx.body = yapi.commons.resReturn(queryList, 0, 'ok');
}
/**
* 下载项目的 Mock 数据
* @interface /project/download
* @method GET
* @category project
* @foldnumber 10
* @param {String} project_id
*/
async download(ctx) {
const project_id = ctx.request.query.project_id;
let interfaceInst = yapi.getInst(interfaceModel);
let count = await interfaceInst.list(project_id);
console.log(count);
const arr = JSON.stringify(count.map(function(item) {
// 返回的json模板数据: item.res_body
const mockData = Mock.mock(
yapi.commons.json_parse(item.res_body)
);
return {
path: item.path,
mock: mockData
}
}));
// console.log(arr);
const fileName = 'mock.js';
ctx.attachment(fileName);
await send(ctx, fileName, { root: __dirname + '/public' });
const res = `
var data = ${arr}`
.trim();
return ctx.body = res;
}
}
module.exports = projectController;

View File

@ -46,7 +46,7 @@ module.exports = async (ctx, next) => {
try {
interfaceData = await interfaceInst.getByPath(project._id, ctx.path.substr(project.basepath.length), ctx.method);
interfaceData = await interfaceInst.getByPath(project._id, ctx.path.substr(project.basepath.length), ctx.method);
if (!interfaceData || interfaceData.length === 0) {
//非正常跨域预检请求回应
if(ctx.method === 'OPTIONS'){
@ -65,9 +65,10 @@ module.exports = async (ctx, next) => {
ctx.set("Access-Control-Allow-Origin", "*")
if (interfaceData.res_body_type === 'json') {
try{
return ctx.body = Mock.mock(
const res = Mock.mock(
yapi.commons.json_parse(interfaceData.res_body)
);
return ctx.body = res;
}catch(e){
return ctx.body = {
errcode: 400,
@ -80,4 +81,4 @@ module.exports = async (ctx, next) => {
} catch (e) {
return ctx.body = yapi.commons.resReturn(null, 409, e.message);
}
};
};

View File

@ -74,6 +74,7 @@ class interfaceModel extends baseModel {
}
list(project_id) {
console.log(project_id);
return this.model.find({
project_id: project_id
})
@ -86,7 +87,7 @@ class interfaceModel extends baseModel {
_id: id
});
}
up(id, data) {
data.up_time = yapi.commons.time();
return this.model.update({
@ -95,4 +96,4 @@ class interfaceModel extends baseModel {
}
}
module.exports = interfaceModel;
module.exports = interfaceModel;

View File

@ -140,6 +140,14 @@ class projectModel extends baseModel {
})
.limit(10);
}
download(id) {
console.log('models in download');
// return this.model.find({
// name: new RegExp(id, 'ig')
// })
// .limit(10);
}
}
module.exports = projectModel;
module.exports = projectModel;

View File

@ -47,168 +47,173 @@ const INTERFACE_CONFIG = {
const routerConfig = {
"group": [
{
{
"action": "list",
"path": "list",
"method": "get"
},
{
{
"action": "add",
"path": "add",
"method": "post"
},
{
{
"action": "up",
"path": "up",
"method": "post"
},
{
{
"action": "del",
"path": "del",
"method": "post"
},
{
{
"action": "addMember",
"path": "add_member",
"method": "post"
},
{
{
"action": "delMember",
"path": "del_member",
"method": "post"
},
{
{
"action": "getMemberList",
"path": "members",
"method": "get"
}
],
"user": [
{
{
"action": "login",
"path": "login",
"method": "post"
},
{
{
"action": "reg",
"path": "reg",
"method": "post"
},
{
{
"action": "list",
"path": "list",
"method": "get"
},
{
{
"action": "findById",
"path": "find",
"method": "get"
},
{
{
"action": "update",
"path": "update",
"method": "post"
},
{
{
"action": "del",
"path": "del",
"method": "post"
},
{
{
"action": "getLoginStatus",
"path": "status",
"method": "get"
},
{
{
"action": "logout",
"path": "logout",
"method": "get"
},
{
{
"action": "loginByToken",
"path": "login_by_token",
"method": "post"
},
{
{
"action": "changePassword",
"path": "change_password",
"method": "post"
},
{
{
"action": "search",
"path": "search",
"method": "get"
},
{
{
"action": "nav",
"path": "nav",
"method": "get"
}
],
"project": [
{
{
"action": "add",
"path": "add",
"method": "post"
},
{
{
"action": "list",
"path": "list",
"method": "get"
},
{
{
"action": "get",
"path": "get",
"method": "get"
},
{
{
"action": "up",
"path": "up",
"method": "post"
},
{
{
"action": "del",
"path": "del",
"method": "post"
},
{
{
"action": "addMember",
"path": "add_member",
"method": "post"
},
{
{
"action": "delMember",
"path": "del_member",
"method": "post"
},
{
{
"action": "getMemberList",
"path": "get_member_list",
"method": "get"
},
{
{
"action": "search",
"path": "search",
"method": "get"
},
{
"action": "download",
"path": "download",
"method": "get"
}
],
"interface": [
{
{
"action": "add",
"path": "add",
"method": "post"
},
{
{
"action": "list",
"path": "list",
"method": "get"
},
{
{
"action": "get",
"path": "get",
"method": "get"
},
{
{
"action": "up",
"path": "up",
"method": "post"
@ -242,7 +247,7 @@ for(let ctrl in routerConfig){
actions.forEach( (item) => {
createAction(ctrl, item.action, item.path, item.method);
} )
}
}
/**
*
@ -265,4 +270,4 @@ function createAction(controller, action, path, method) {
});
}
module.exports = router;
module.exports = router;

View File

@ -1,65 +0,0 @@
'use strict';
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);
var _interfaceCase = require('../models/interfaceCase.js');
var _interfaceCase2 = _interopRequireDefault(_interfaceCase);
var _base = require('./base.js');
var _base2 = _interopRequireDefault(_base);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var interfaceCaseController = function (_baseController) {
(0, _inherits3.default)(interfaceCaseController, _baseController);
function interfaceCaseController(ctx) {
(0, _classCallCheck3.default)(this, interfaceCaseController);
var _this = (0, _possibleConstructorReturn3.default)(this, (interfaceCaseController.__proto__ || (0, _getPrototypeOf2.default)(interfaceCaseController)).call(this, ctx));
_this.Model = _yapi2.default.getInst(_interfaceCase2.default);
return _this;
}
(0, _createClass3.default)(interfaceCaseController, [{
key: 'list',
value: function list(ctx) {}
}, {
key: 'get',
value: function get(ctx) {}
}, {
key: 'up',
value: function up(ctx) {}
}, {
key: 'del',
value: function del(ctx) {}
}]);
return interfaceCaseController;
}(_base2.default);
module.exports = interfaceCaseController;

View File

@ -1,5 +1,9 @@
'use strict';
var _stringify = require('babel-runtime/core-js/json/stringify');
var _stringify2 = _interopRequireDefault(_stringify);
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
@ -56,8 +60,14 @@ var _user = require('../models/user.js');
var _user2 = _interopRequireDefault(_user);
var _mockjs = require('mockjs');
var _mockjs2 = _interopRequireDefault(_mockjs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var send = require('koa-send');
var projectController = function (_baseController) {
(0, _inherits3.default)(projectController, _baseController);
@ -1161,6 +1171,68 @@ var projectController = function (_baseController) {
return search;
}()
/**
* 下载项目的 Mock 数据
* @interface /project/download
* @method GET
* @category project
* @foldnumber 10
* @param {String} project_id
*/
}, {
key: 'download',
value: function () {
var _ref12 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(ctx) {
var project_id, interfaceInst, count, arr, fileName, res;
return _regenerator2.default.wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
project_id = ctx.request.query.project_id;
interfaceInst = _yapi2.default.getInst(_interface2.default);
_context12.next = 4;
return interfaceInst.list(project_id);
case 4:
count = _context12.sent;
console.log(count);
arr = (0, _stringify2.default)(count.map(function (item) {
// 返回的json模板数据: item.res_body
var mockData = _mockjs2.default.mock(_yapi2.default.commons.json_parse(item.res_body));
return {
path: item.path,
mock: mockData
};
}));
// console.log(arr);
fileName = 'mock.js';
ctx.attachment(fileName);
_context12.next = 11;
return send(ctx, fileName, { root: __dirname + '/public' });
case 11:
res = ('\n var data = ' + arr).trim();
return _context12.abrupt('return', ctx.body = res);
case 13:
case 'end':
return _context12.stop();
}
}
}, _callee12, this);
}));
function download(_x13) {
return _ref12.apply(this, arguments);
}
return download;
}()
}]);
return projectController;
}(_base2.default);

View File

@ -28,7 +28,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
module.exports = function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) {
var hostname, config, projectInst, projects, matchProject, maxBasepath, i, l, _project, project, interfaceData, interfaceInst;
var hostname, config, projectInst, projects, matchProject, maxBasepath, i, l, _project, project, interfaceData, interfaceInst, res;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
@ -138,15 +138,16 @@ module.exports = function () {
ctx.set("Access-Control-Allow-Origin", "*");
if (!(interfaceData.res_body_type === 'json')) {
_context.next = 47;
_context.next = 48;
break;
}
_context.prev = 40;
return _context.abrupt('return', ctx.body = _mockjs2.default.mock(_yapi2.default.commons.json_parse(interfaceData.res_body)));
res = _mockjs2.default.mock(_yapi2.default.commons.json_parse(interfaceData.res_body));
return _context.abrupt('return', ctx.body = res);
case 44:
_context.prev = 44;
case 45:
_context.prev = 45;
_context.t1 = _context['catch'](40);
return _context.abrupt('return', ctx.body = {
errcode: 400,
@ -154,20 +155,20 @@ module.exports = function () {
data: interfaceData.res_body
});
case 47:
case 48:
return _context.abrupt('return', ctx.body = interfaceData.res_body);
case 50:
_context.prev = 50;
case 51:
_context.prev = 51;
_context.t2 = _context['catch'](25);
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 409, _context.t2.message));
case 53:
case 54:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[10, 16], [25, 50], [40, 44]]);
}, _callee, undefined, [[10, 16], [25, 51], [40, 45]]);
}));
return function (_x, _x2) {

View File

@ -117,6 +117,7 @@ var interfaceModel = function (_baseModel) {
}, {
key: 'list',
value: function list(project_id) {
console.log(project_id);
return this.model.find({
project_id: project_id
}).sort({ _id: -1 }).exec();

View File

@ -184,6 +184,15 @@ var projectModel = function (_baseModel) {
name: new RegExp(keyword, 'ig')
}).limit(10);
}
}, {
key: 'download',
value: function download(id) {
console.log('models in download');
// return this.model.find({
// name: new RegExp(id, 'ig')
// })
// .limit(10);
}
}]);
return projectModel;
}(_base2.default);

View File

@ -194,6 +194,10 @@ var routerConfig = {
"action": "search",
"path": "search",
"method": "get"
}, {
"action": "download",
"path": "download",
"method": "get"
}],
"interface": [{
"action": "add",