add interface module

This commit is contained in:
sean 2017-07-10 11:56:53 +08:00
parent 832f58baef
commit 72dae6a52f
39 changed files with 3354 additions and 703 deletions

1271
doc/build/api.html vendored

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,9 @@
<meta name="description" content="description of your site">
<meta name="author" content="author of the site">
<title>yapi 首页</title>
<link rel="stylesheet" href="source/main.css" />
<link rel="stylesheet" href="source/main.css" />
</head>
<body>
<div class="ydoc">
@ -53,7 +55,7 @@
<div class="ydoc-container">
<div class="ydoc-container-content" id="readme">
<div class="ydoc-container-content " id="readme">
<article class="markdown-body">
<h3 class="subject" id="Yapi是一个高效易用功能强大的api管理系统">Yapi是一个高效易用功能强大的api管理系统 <a class="hashlink" href="#Yapi是一个高效易用功能强大的api管理系统">#</a></h3><h4 class="subject" id="后台server如何启动和热更新">后台server如何启动和热更新 <a class="hashlink" href="#后台server如何启动和热更新">#</a></h4><ol>

View File

@ -132,23 +132,6 @@ $(document).ready(function() {
$(item).addClass('ydoc-example');
});
// $('code').each(function(i, block) {
// if (block.innerHTML.indexOf('\n') != -1) {
// var pn = block.parentNode;
// if (pn.tagName.toUpperCase() == 'PRE') {
// try {
// hljs.highlightBlock(block);
// } catch(e) {}
// } else {
// pn.innerHTML = '<pre><code>' + block.innerHTML + '</code></pre>';
// try {
// hljs.highlightBlock(pn.childNodes[0].childNodes[0]);
// } catch(e) {}
// }
// }
// });
var winHeight = $(window).height() - 44,
sidebar = $('.docs-sidebar');
var docSideNav = $('.docs-sidenav');
@ -205,46 +188,9 @@ $(document).ready(function() {
// 退出全屏浏览器窗口大小改变不触发resize
$(window).on('resize', function(e) {
resizeSidebar();
$contentLeftWidth = $contentLeft.width() - 1;
});
function resizeSidebar() {
var winHeight = $(window).height() - 44,
sidebar = $('.docs-sidebar');
var docSideNav = $('.docs-sidenav');
if (winWidth > 767) {
docSideNav.width($contentLeftWidth);
}
if (sidebar.height() > winHeight) {
sidebar.css('max-height', winHeight + 'px');
$('.docs-sidenav').css('max-height', winHeight + 'px');
$('.docs-sidenav').css({
'overflow-y': 'scroll',
'overflow-x': 'hidden'
});
var barScroll = false;
sidebar.on('mouseover', function() {
barScroll = true;
});
sidebar.on('mouseout', function() {
barScroll = false;
});
// scroll
if ($(window).scrollTop() > ($('.footer').offset().top - $(window).height())) {
winHeight = $(window).height() - $('.footer').outerHeight() - 44;
sidebar.css('max-height', winHeight + 'px');
$('.docs-sidenav').css('max-height', winHeight + 'px');
} else {
winHeight = $(window).height() - 44;
sidebar.css('max-height', winHeight + 'px');
$('.docs-sidenav').css('max-height', winHeight + 'px');
}
}
}
function sortAsOffset(propertyName) {
return function(obj1, obj2) {
var val1 = obj1[propertyName];

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -26,6 +26,7 @@
<div class="static-code-content" role="main">
<pre class="brush: js;">
import yapi from '../yapi.js'
import projectModel from '../models/project.js'
class baseController{
constructor(ctx){
console.log('baseControler init...')
@ -42,6 +43,28 @@ class baseController{
getRole(){
return 'admin'
}
async jungeProjectAuth(id){
let model = yapi.getInst(projectModel);
if(this.getRole() === 'admin') return true;
if(!id) return false;
let result = await model.get(id);
if(result.uid === this.getUid()){
return true;
}
return false;
}
async jungeMemberAuth(id, member_uid){
let model = yapi.getInst(projectModel);
if(this.getRole() === 'admin') return true;
if(!id || !member_uid) return false;
let result = await model.checkMemberRepeat(id, member_uid);
if(result > 0){
return true;
}
return false;
}
}
module.exports = baseController

View File

@ -77,13 +77,13 @@ class groupController extends baseController{
}
/**
* 添加项目分组
* 获取项目分组列表
* @interface /group/list
* @method get
* @category group
* @foldnumber 10
* @returns {Object}
* @example
* @example ./api/group/list.json
*/
async list(ctx) {
@ -96,6 +96,17 @@ class groupController extends baseController{
}
}
/**
* 删除项目分组
* @interface /group/del
* @method post
* @param {String} id 项目分组id
* @category group
* @foldnumber 10
* @returns {Object}
* @example ./api/group/del.json
*/
async del(ctx){
try{
var groupInst = yapi.getInst(groupModel);
@ -107,6 +118,19 @@ class groupController extends baseController{
}
}
/**
* 更新项目分组
* @interface /group/up
* @method post
* @param {String} id 项目分组id
* @param {String} group_name 项目分组名称
* @param {String} group_desc 项目分组描述
* @category group
* @foldnumber 10
* @returns {Object}
* @example ./api/group/up.json
*/
async up(ctx){
try{
var groupInst = yapi.getInst(groupModel);

View File

@ -25,27 +25,166 @@
<div class="ydoc-container-content">
<div class="static-code-content" role="main">
<pre class="brush: js;">
import { resReturn,log } from '../utils/commons';
import interfaceModel from '../models/interface.js'
import interfaceModel from '../models/interface.js'
import baseController from './base.js'
import yapi from '../yapi.js'
module.exports = {
async add(ctx) {
let data = {
title: 'yapi',
content: 'content',
uid: 'abc'
class interfaceController extends baseController{
constructor(ctx){
super(ctx)
this.Model = yapi.getInst(interfaceModel);
}
/**
* 添加项目分组
* @interface /interface/add
* @method POST
* @category interface
* @foldnumber 10
* @param {Number} project_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描述
* @param {String} [req_params_type] 请求参数方式,有["form", "json", "text", "xml"]四种
* @param {Mixed} [req_params] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_params[].name] 请求参数名
* @param {String} [req_params[].value] 请求参数值可填写生成规则mock。如@email随机生成一条email
* @param {String} [req_params[].type] 请求参数类型,有["text", "file"]两种
* @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
*/
async add(ctx){
let params = ctx.request.body;
params.method = params.method || 'GET';
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
if(!params.project_id){
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
let result = await interfaceModel.save(data);
log('interface err...', 'error');
ctx.body = resReturn(result)
},
async list(ctx) {
let data = interfaceModel.find();
ctx.body = 1;
if(!params.path){
return ctx.body = yapi.commons.resReturn(null, 400, '接口请求路径不能为空');
}
let checkRepeat = await this.Model.checkRepeat(params.path, params.method);
if(checkRepeat > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']');
}
try{
let data = {
project_id: params.project_id,
path: params.path,
desc: params.desc,
method: params.method,
req_headers: params.req_headers,
req_params_type: params.req_params_type,
res_body: params.res_body,
res_body_type: params.res_body_type,
uid: this.getUid(),
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
}
if(data.req_params_type === 'form') data.req_params_form = params.req_params;
else data.req_params_other = params.req_params;
let result = await this.Model.save(data);
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
async get(ctx){
let params = ctx.request.query;
if(!params.id){
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
}
try{
let result = await this.Model.get(params.id);
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
async list(ctx){
let project_id = ctx.request.query.project_id;
if(!project_id){
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
try{
let result = await this.Model.list(project_id);
ctx.body = yapi.commons.resReturn(result)
}catch(err){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
async up(ctx){
let params = ctx.request.body;
params.method = params.method || 'GET';
let id = ctx.request.body.id;
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
}
if(params.path){
let checkRepeat = await this.Model.checkRepeat(params.path, params.method);
if(checkRepeat > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']');
}
}
let data = {
up_time: yapi.commons.time()
}
if(params.path) data.path = params.path;
if(params.desc) data.desc = params.desc;
if(params.method) data.method = params.method;
if(params.req_headers) data.req_headers = params.req_headers;
if(params.req_params_type === 'form') data.req_params_form = params.req_params;
else data.req_params_other = params.req_params;
if(params.res_body_type) data.res_body_type = params.res_body_type;
if(params.res_body) data.res_body = params.res_body;
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)
}
}
async del(ctx){
try{
let id = ctx.request.body.id;
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
}
if(await this.jungeMemberAuth(id) !== true){
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
let result = await this.Model.del(id);
ctx.body = yapi.commons.resReturn(result)
}catch(err){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
}
module.exports = interfaceController;
</pre>
</div>
</div>

View File

@ -36,26 +36,20 @@ class projectController extends baseController {
this.Model = yapi.getInst(projectModel);
}
async jungeProjectAuth(id){
if(this.getRole() === 'admin') return true;
if(!id) return false;
let result = await this.Model.get(params.id);
if(result.uid === this.getUid()){
return true;
}
return false;
}
async jungeMemberAuth(id, member_uid){
if(this.getRole() === 'admin') return true;
if(!id || !member_uid) return false;
let result = await this.Model.checkMemberRepeat(params.id, member_uid);
if(result > 0){
return true;
}
return false;
}
/**
* 添加项目分组
* @interface /project/add
* @method POST
* @category project
* @foldnumber 10
* @param {String} name 项目名称,不能为空
* @param {String} basepath 项目基本路径,不能为空
* @param {String} prd_host 项目线上域名不能为空。可通过配置的域名访问到mock数据
* @param {Number} group_id 项目分组id不能为空
* @param {String} [desc] 项目描述
* @returns {Object}
* @example ./api/project/add.json
*/
async add(ctx) {
let params = ctx.request.body;
@ -104,7 +98,17 @@ class projectController extends baseController {
}
}
/**
* 添加项目
* @interface /project/add_member
* @method POST
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @param {member_uid} uid 项目成员uid,不能为空
* @returns {Object}
* @example ./api/project/add_member.json
*/
async addMember(ctx){
let params = ctx.request.body;
if(!params.member_uid){
@ -126,6 +130,17 @@ class projectController extends baseController {
}
}
/**
* 添加项目
* @interface /project/del_member
* @method POST
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @param {member_uid} uid 项目成员uid,不能为空
* @returns {Object}
* @example ./api/project/del_member.json
*/
async delMember(ctx){
let params = ctx.request.body;
@ -147,6 +162,16 @@ class projectController extends baseController {
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
/**
* 添加项目
* @interface /project/get
* @method GET
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @returns {Object}
* @example ./api/project/get.json
*/
async get(ctx){
let params = ctx.request.query;
@ -161,6 +186,17 @@ class projectController extends baseController {
}
}
/**
* 获取项目列表
* @interface /project/list
* @method GET
* @category project
* @foldnumber 10
* @param {Number} group_id 项目group_id不能为空
* @returns {Object}
* @example ./api/project/list.json
*/
async list(ctx) {
let group_id = ctx.request.query.group_id;
if(!group_id){
@ -174,10 +210,24 @@ class projectController extends baseController {
}
}
/**
* 删除项目
* @interface /project/del
* @method POST
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @returns {Object}
* @example ./api/project/del.json
*/
async del(ctx){
try{
let id = ctx.request.body.id;
if(this.jungeProjectAuth(id) !== true){
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
if(await this.jungeProjectAuth(id) !== true){
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
let result = await this.Model.del(id);
@ -187,50 +237,61 @@ class projectController extends baseController {
}
}
/**
* 编辑项目
* @interface /project/up
* @method GET
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @param {String} name 项目名称,不能为空
* @param {String} basepath 项目基本路径,不能为空
* @param {String} prd_host 项目线上域名不能为空。可通过配置的域名访问到mock数据
* @param {String} [desc] 项目描述
* @param {String} [env] JSON字符串,例如{"local": "http://www.api.com"}
* @returns {Object}
* @example ./api/project/up.json
*/
async up(ctx){
try{
let id = ctx.request.body.id;
let params = ctx.request.body;
if(this.jungeMemberAuth(id, this.getUid()) !== true){
if(await this.jungeMemberAuth(id, this.getUid()) !== true){
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
if(!params.name){
return ctx.body = yapi.commons.resReturn(null, 400, '项目名不能为空');
if(params.name){
let checkRepeat = await this.Model.checkNameRepeat(params.name);
if(checkRepeat > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的项目名');
}
}
let checkRepeat = await this.Model.checkNameRepeat(params.name);
if(checkRepeat > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的项目名');
if(params.basepath && params.prd_host){
let checkRepeatDomain = await this.Model.checkDomainRepeat(params.prd_host, params.basepath);
if(checkRepeatDomain > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在domain和basepath');
}
}
if(!params.basepath){
return ctx.body = yapi.commons.resReturn(null, 400, '项目basepath不能为空');
}
if(!params.prd_host){
return ctx.body = yapi.commons.resReturn(null, 400, '项目domain不能为空');
}
let checkRepeatDomain = await this.Model.checkDomainRepeat(params.prd_host, params.basepath);
if(checkRepeatDomain > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在domain和basepath');
}
let data= {
name: params.name,
desc: params.desc,
prd_host: params.prd_host,
basepath: params.basepath,
uid: this.getUid(),
up_time: yapi.commons.time(),
env: params.env
up_time: yapi.commons.time()
}
if(params.name) data.name = params.name;
if(params.desc) data.desc = params.desc;
if(params.prd_host && params.basepath){
data.prd_host = params.prd_host;
data.basepath = params.basepath;
}
if(params.env) data.env = params.env;
let result = await this.Model.up(id, data);
ctx.body = yapi.commons.resReturn(result)
}catch(err){
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}

View File

@ -0,0 +1,8 @@
{
"errcode": 0,
"errmsg": "success",
"data": {
"n": 1,
"ok": 1
}
}

View File

@ -0,0 +1,34 @@
{
"errcode": 0,
"errmsg": "success",
"data": [
{
"_id": 1,
"group_name": "大数据2",
"group_desc": "大数据2",
"add_time": 1499244581,
"up_time": 1499244581
},
{
"_id": 2,
"group_name": "大数据3",
"group_desc": "大数据3",
"add_time": 1499244588,
"up_time": 1499244588
},
{
"_id": 3,
"group_name": "大数据4",
"group_desc": "大数据4",
"add_time": 1499244652,
"up_time": 1499244652
},
{
"_id": 4,
"group_name": "大数据5",
"group_desc": "大数据5",
"add_time": 1499328065,
"up_time": 1499328065
}
]
}

View File

@ -0,0 +1,9 @@
{
"errcode": 0,
"errmsg": "success",
"data": {
"n": 1,
"nModified": 1,
"ok": 1
}
}

View File

@ -0,0 +1,69 @@
// /interface/add
//header Content-Type:application/json
{
"desc": "api",
"method": "post",
"path": "/testapi",
"project_id": 8,
"req_headers": [
{
"key": "h",
"value": "t"
}
],
"req_params_type": "form",
"req_params": [
{
"name": "uid",
"value": 100,
"type": "text"
},
{
"name": "gid",
"value": 1001,
"type": "text"
}
],
"res_body_type": "json",
"res_body": "{\"tt\": 222}"
}
//
{
"errcode": 0,
"errmsg": "success",
"data": {
"__v": 0,
"_id": 422,
"project_id": 8,
"path": "/testapi",
"desc": "api",
"method": "post",
"req_params_type": "form",
"res_body": "{\"tt\": 222}",
"res_body_type": "json",
"uid": 0,
"add_time": 1499658569,
"up_time": 1499658569,
"req_params_form": [
{
"name": "uid",
"value": "100",
"_id": "5962f94940a7a5767088fcc0"
},
{
"name": "gid",
"value": "1001",
"_id": "5962f94940a7a5767088fcbf"
}
],
"req_headers": [
{
"value": "t",
"_id": "5962f94940a7a5767088fcc1"
}
]
}
}

View File

@ -0,0 +1,19 @@
{
"errcode": 0,
"errmsg": "success",
"data": {
"__v": 0,
"_id": 8,
"name": "project_a2",
"desc": "tttttt",
"prd_host": "project.a2.cc",
"basepath": "/a1",
"uid": 0,
"group_id": 1,
"add_time": 1499331387,
"up_time": 1499331387,
"members": [
0
]
}
}

View File

@ -0,0 +1,8 @@
{
"errcode": 0,
"errmsg": "success",
"data": {
"n": 1,
"ok": 1
}
}

View File

@ -0,0 +1,8 @@
{
"errcode": 0,
"errmsg": "success",
"data": {
"n": 1,
"ok": 1
}
}

View File

@ -0,0 +1,8 @@
{
"errcode": 0,
"errmsg": "success",
"data": {
"n": 1,
"ok": 1
}
}

View File

@ -0,0 +1,19 @@
{
"errcode": 0,
"errmsg": "success",
"data": {
"_id": 7,
"name": "project_a1",
"desc": "tttttt",
"prd_host": "project.a1.cc",
"basepath": "/a1",
"uid": 0,
"group_id": 1,
"add_time": 1499331378,
"up_time": 1499331378,
"__v": 0,
"members": [
0
]
}
}

View File

@ -0,0 +1,36 @@
{
"errcode": 0,
"errmsg": "success",
"data": [
{
"_id": 7,
"name": "project_a1",
"desc": "tttttt",
"prd_host": "project.a1.cc",
"basepath": "/a1",
"uid": 0,
"group_id": 1,
"add_time": 1499331378,
"up_time": 1499331378,
"__v": 0,
"members": [
0
]
},
{
"_id": 8,
"name": "project_a2",
"desc": "tttttt",
"prd_host": "project.a2.cc",
"basepath": "/a1",
"uid": 0,
"group_id": 1,
"add_time": 1499331387,
"up_time": 1499331387,
"__v": 0,
"members": [
0
]
}
]
}

View File

@ -0,0 +1,8 @@
{
"errcode": 0,
"errmsg": "success",
"data": {
"n": 1,
"ok": 1
}
}

View File

@ -1,4 +1,5 @@
import yapi from '../yapi.js'
import projectModel from '../models/project.js'
class baseController{
constructor(ctx){
console.log('baseControler init...')
@ -15,6 +16,28 @@ class baseController{
getRole(){
return 'admin'
}
async jungeProjectAuth(id){
let model = yapi.getInst(projectModel);
if(this.getRole() === 'admin') return true;
if(!id) return false;
let result = await model.get(id);
if(result.uid === this.getUid()){
return true;
}
return false;
}
async jungeMemberAuth(id, member_uid){
let model = yapi.getInst(projectModel);
if(this.getRole() === 'admin') return true;
if(!id || !member_uid) return false;
let result = await model.checkMemberRepeat(id, member_uid);
if(result > 0){
return true;
}
return false;
}
}
module.exports = baseController

View File

@ -50,13 +50,13 @@ class groupController extends baseController{
}
/**
* 添加项目分组
* 获取项目分组列表
* @interface /group/list
* @method get
* @category group
* @foldnumber 10
* @returns {Object}
* @example
* @example ./api/group/list.json
*/
async list(ctx) {
@ -69,6 +69,17 @@ class groupController extends baseController{
}
}
/**
* 删除项目分组
* @interface /group/del
* @method post
* @param {String} id 项目分组id
* @category group
* @foldnumber 10
* @returns {Object}
* @example ./api/group/del.json
*/
async del(ctx){
try{
var groupInst = yapi.getInst(groupModel);
@ -80,6 +91,19 @@ class groupController extends baseController{
}
}
/**
* 更新项目分组
* @interface /group/up
* @method post
* @param {String} id 项目分组id
* @param {String} group_name 项目分组名称
* @param {String} group_desc 项目分组描述
* @category group
* @foldnumber 10
* @returns {Object}
* @example ./api/group/up.json
*/
async up(ctx){
try{
var groupInst = yapi.getInst(groupModel);

View File

@ -1,20 +1,160 @@
import { resReturn,log } from '../utils/commons';
import interfaceModel from '../models/interface.js'
import baseController from './base.js'
import yapi from '../yapi.js'
module.exports = {
async add(ctx) {
let data = {
title: 'yapi',
content: 'content',
uid: 'abc'
class interfaceController extends baseController{
constructor(ctx){
super(ctx)
this.Model = yapi.getInst(interfaceModel);
}
/**
* 添加项目分组
* @interface /interface/add
* @method POST
* @category interface
* @foldnumber 10
* @param {Number} project_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描述
* @param {String} [req_params_type] 请求参数方式["form", "json", "text", "xml"]四种
* @param {Mixed} [req_params] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_params[].name] 请求参数名
* @param {String} [req_params[].value] 请求参数值可填写生成规则mock@email随机生成一条email
* @param {String} [req_params[].type] 请求参数类型["text", "file"]两种
* @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
*/
async add(ctx){
let params = ctx.request.body;
params.method = params.method || 'GET';
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
if(!params.project_id){
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
let result = await interfaceModel.save(data);
log('interface err...', 'error');
ctx.body = resReturn(result)
},
async list(ctx) {
let data = interfaceModel.find();
ctx.body = 1;
if(!params.path){
return ctx.body = yapi.commons.resReturn(null, 400, '接口请求路径不能为空');
}
let checkRepeat = await this.Model.checkRepeat(params.path, params.method);
if(checkRepeat > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']');
}
try{
let data = {
project_id: params.project_id,
path: params.path,
desc: params.desc,
method: params.method,
req_headers: params.req_headers,
req_params_type: params.req_params_type,
res_body: params.res_body,
res_body_type: params.res_body_type,
uid: this.getUid(),
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
}
if(data.req_params_type === 'form') data.req_params_form = params.req_params;
else data.req_params_other = params.req_params;
let result = await this.Model.save(data);
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
async get(ctx){
let params = ctx.request.query;
if(!params.id){
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
}
try{
let result = await this.Model.get(params.id);
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
async list(ctx){
let project_id = ctx.request.query.project_id;
if(!project_id){
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
try{
let result = await this.Model.list(project_id);
ctx.body = yapi.commons.resReturn(result)
}catch(err){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
async up(ctx){
let params = ctx.request.body;
params.method = params.method || 'GET';
let id = ctx.request.body.id;
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
}
if(params.path){
let checkRepeat = await this.Model.checkRepeat(params.path, params.method);
if(checkRepeat > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']');
}
}
let data = {
up_time: yapi.commons.time()
}
if(params.path) data.path = params.path;
if(params.desc) data.desc = params.desc;
if(params.method) data.method = params.method;
if(params.req_headers) data.req_headers = params.req_headers;
if(params.req_params_type === 'form') data.req_params_form = params.req_params;
else data.req_params_other = params.req_params;
if(params.res_body_type) data.res_body_type = params.res_body_type;
if(params.res_body) data.res_body = params.res_body;
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)
}
}
async del(ctx){
try{
let id = ctx.request.body.id;
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
}
if(await this.jungeMemberAuth(id) !== true){
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
let result = await this.Model.del(id);
ctx.body = yapi.commons.resReturn(result)
}catch(err){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
}
module.exports = interfaceController;

View File

@ -9,26 +9,20 @@ class projectController extends baseController {
this.Model = yapi.getInst(projectModel);
}
async jungeProjectAuth(id){
if(this.getRole() === 'admin') return true;
if(!id) return false;
let result = await this.Model.get(params.id);
if(result.uid === this.getUid()){
return true;
}
return false;
}
async jungeMemberAuth(id, member_uid){
if(this.getRole() === 'admin') return true;
if(!id || !member_uid) return false;
let result = await this.Model.checkMemberRepeat(params.id, member_uid);
if(result > 0){
return true;
}
return false;
}
/**
* 添加项目分组
* @interface /project/add
* @method POST
* @category project
* @foldnumber 10
* @param {String} name 项目名称不能为空
* @param {String} basepath 项目基本路径不能为空
* @param {String} prd_host 项目线上域名不能为空可通过配置的域名访问到mock数据
* @param {Number} group_id 项目分组id不能为空
* @param {String} [desc] 项目描述
* @returns {Object}
* @example ./api/project/add.json
*/
async add(ctx) {
let params = ctx.request.body;
@ -77,7 +71,17 @@ class projectController extends baseController {
}
}
/**
* 添加项目
* @interface /project/add_member
* @method POST
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @param {member_uid} uid 项目成员uid,不能为空
* @returns {Object}
* @example ./api/project/add_member.json
*/
async addMember(ctx){
let params = ctx.request.body;
if(!params.member_uid){
@ -99,6 +103,17 @@ class projectController extends baseController {
}
}
/**
* 添加项目
* @interface /project/del_member
* @method POST
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @param {member_uid} uid 项目成员uid,不能为空
* @returns {Object}
* @example ./api/project/del_member.json
*/
async delMember(ctx){
let params = ctx.request.body;
@ -120,6 +135,16 @@ class projectController extends baseController {
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
/**
* 添加项目
* @interface /project/get
* @method GET
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @returns {Object}
* @example ./api/project/get.json
*/
async get(ctx){
let params = ctx.request.query;
@ -134,6 +159,17 @@ class projectController extends baseController {
}
}
/**
* 获取项目列表
* @interface /project/list
* @method GET
* @category project
* @foldnumber 10
* @param {Number} group_id 项目group_id不能为空
* @returns {Object}
* @example ./api/project/list.json
*/
async list(ctx) {
let group_id = ctx.request.query.group_id;
if(!group_id){
@ -147,10 +183,24 @@ class projectController extends baseController {
}
}
/**
* 删除项目
* @interface /project/del
* @method POST
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @returns {Object}
* @example ./api/project/del.json
*/
async del(ctx){
try{
let id = ctx.request.body.id;
if(this.jungeProjectAuth(id) !== true){
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
if(await this.jungeProjectAuth(id) !== true){
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
let result = await this.Model.del(id);
@ -160,50 +210,61 @@ class projectController extends baseController {
}
}
/**
* 编辑项目
* @interface /project/up
* @method GET
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @param {String} name 项目名称不能为空
* @param {String} basepath 项目基本路径不能为空
* @param {String} prd_host 项目线上域名不能为空可通过配置的域名访问到mock数据
* @param {String} [desc] 项目描述
* @param {String} [env] JSON字符串,例如{"local": "http://www.api.com"}
* @returns {Object}
* @example ./api/project/up.json
*/
async up(ctx){
try{
let id = ctx.request.body.id;
let params = ctx.request.body;
if(this.jungeMemberAuth(id, this.getUid()) !== true){
if(await this.jungeMemberAuth(id, this.getUid()) !== true){
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
if(!params.name){
return ctx.body = yapi.commons.resReturn(null, 400, '项目名不能为空');
if(params.name){
let checkRepeat = await this.Model.checkNameRepeat(params.name);
if(checkRepeat > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的项目名');
}
}
let checkRepeat = await this.Model.checkNameRepeat(params.name);
if(checkRepeat > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的项目名');
if(params.basepath && params.prd_host){
let checkRepeatDomain = await this.Model.checkDomainRepeat(params.prd_host, params.basepath);
if(checkRepeatDomain > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在domain和basepath');
}
}
if(!params.basepath){
return ctx.body = yapi.commons.resReturn(null, 400, '项目basepath不能为空');
}
if(!params.prd_host){
return ctx.body = yapi.commons.resReturn(null, 400, '项目domain不能为空');
}
let checkRepeatDomain = await this.Model.checkDomainRepeat(params.prd_host, params.basepath);
if(checkRepeatDomain > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在domain和basepath');
}
let data= {
name: params.name,
desc: params.desc,
prd_host: params.prd_host,
basepath: params.basepath,
uid: this.getUid(),
up_time: yapi.commons.time(),
env: params.env
up_time: yapi.commons.time()
}
if(params.name) data.name = params.name;
if(params.desc) data.desc = params.desc;
if(params.prd_host && params.basepath){
data.prd_host = params.prd_host;
data.basepath = params.basepath;
}
if(params.env) data.env = params.env;
let result = await this.Model.up(id, data);
ctx.body = yapi.commons.resReturn(result)
}catch(err){
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}

View File

@ -12,9 +12,28 @@ class baseModel{
constructor(){
this.schema = new mongoose.Schema(this.getSchema())
this.name = this.getName()
this.schema.plugin(autoIncrement.plugin, this.name)
if(this.isNeedAutoIncrement() === true){
this.schema.plugin(autoIncrement.plugin, {
model: this.name,
field: this.getPrimaryKey(),
startAt: 101,
incrementBy: yapi.commons.rand(1, 100)
})
}
this.model = yapi.db(this.name, this.schema);
}
isNeedAutoIncrement(){
return true;
}
/**
* 可通过覆盖此方法生成其他自增字段
*/
getPrimaryKey(){
return '_id'
}
/**
* 获取collection的schema结构

View File

@ -1,31 +1,77 @@
import mongoose from 'mongoose'
const Schema = mongoose.Schema;
const interfaceSchema = new Schema({
title: String,
content: String,
uid: String
})
import yapi from '../yapi.js'
import baseModel from './base.js'
var interfaceModel = mongoose.model('interface', interfaceSchema);
class interfaceModel extends baseModel{
getName(){
return 'interface'
}
getSchema(){
return {
uid: {type: Number, required: true},
path: {type: String, required: true},
method: {type: String, required: true},
project_id: {type: Number, required: true},
desc: String,
add_time: Number,
up_time: Number,
req_headers: [{
name: String, value: String, desc: String, required: Boolean
}],
req_params_type: {
type: String,
enum: ["form", "json", "text", "xml"]
},
req_params_form: [{
name: String, value: String,value_type: {type: String, enum: ["text", "file"]}, desc: String, required: Boolean
}],
req_params_other: String,
res_body_type: {
type: String,
enum: ["json", "text", "xml"]
},
res_body: String
}
}
save(data) {
let m = new this.model(data);
return m.save();
}
function save(data){
let m = new interfaceModel(data);
return m.save();
get(id){
return this.model.findOne({
_id: id
}).exec()
}
checkRepeat(path, method){
return this.model.count({
path: path,
method: method
})
}
list (group_id){
return this.model.find({
group_id: group_id
}).exec()
}
del(id){
return this.model.deleteOne({
_id: id
})
}
up(id, data){
data.up_time = yapi.commons.time();
return this.model.update({
_id: id,
}, data, { runValidators: true })
}
}
function findById(id){
return interfaceModel.findOne({
_id: id
}, 'title content')
}
function find(){
return interfaceModel.find({title: 2222}).exec()
}
module.exports = {
save: save,
findById: findById,
find: find
}
module.exports = interfaceModel;

View File

@ -66,8 +66,9 @@ class projectModel extends baseModel{
}
addMember(id, uid){
console.log(id, uid)
return this.model.update({
_id, id
_id: id
}, {
$push: {members: uid}
})

View File

@ -37,6 +37,15 @@ createAction('project', 'list', 'get', 'list')
createAction('project', 'get', 'get', 'get')
createAction('project', 'up', 'post', 'up')
createAction('project', 'del', 'post', 'del')
createAction('project', 'add_member', 'post', 'addMember')
createAction('project', 'del_member', 'post', 'delMember')
//interface
createAction('interface', 'add', 'post', 'add')
createAction('interface', 'list', 'get', 'list')
createAction('interface', 'get', 'get', 'get')
createAction('interface', 'up', 'post', 'up')
createAction('interface', 'del', 'post', 'del')
/**
*

View File

@ -67,4 +67,16 @@ exports.fieldSelect = (data, field)=>{
data[f] && (arr[f] = data[f]);
} )
return arr;
}
exports.rand =(min, max)=>{
return Math.floor(Math.random() * (max - min) + min);
}
exports.json_parse = (json)=>{
try{
return JSON.parse(json);
}catch(e){
return json
}
}

View File

@ -1,5 +1,13 @@
'use strict';
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
@ -12,6 +20,10 @@ var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _project = require('../models/project.js');
var _project2 = _interopRequireDefault(_project);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var baseController = function () {
@ -36,6 +48,120 @@ var baseController = function () {
value: function getRole() {
return 'admin';
}
}, {
key: 'jungeProjectAuth',
value: function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(id) {
var model, result;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
model = _yapi2.default.getInst(_project2.default);
if (!(this.getRole() === 'admin')) {
_context.next = 3;
break;
}
return _context.abrupt('return', true);
case 3:
if (id) {
_context.next = 5;
break;
}
return _context.abrupt('return', false);
case 5:
_context.next = 7;
return model.get(id);
case 7:
result = _context.sent;
if (!(result.uid === this.getUid())) {
_context.next = 10;
break;
}
return _context.abrupt('return', true);
case 10:
return _context.abrupt('return', false);
case 11:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function jungeProjectAuth(_x) {
return _ref.apply(this, arguments);
}
return jungeProjectAuth;
}()
}, {
key: 'jungeMemberAuth',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(id, member_uid) {
var model, result;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
model = _yapi2.default.getInst(_project2.default);
if (!(this.getRole() === 'admin')) {
_context2.next = 3;
break;
}
return _context2.abrupt('return', true);
case 3:
if (!(!id || !member_uid)) {
_context2.next = 5;
break;
}
return _context2.abrupt('return', false);
case 5:
_context2.next = 7;
return model.checkMemberRepeat(id, member_uid);
case 7:
result = _context2.sent;
if (!(result > 0)) {
_context2.next = 10;
break;
}
return _context2.abrupt('return', true);
case 10:
return _context2.abrupt('return', false);
case 11:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function jungeMemberAuth(_x2, _x3) {
return _ref2.apply(this, arguments);
}
return jungeMemberAuth;
}()
}]);
return baseController;
}();

View File

@ -147,13 +147,13 @@ var groupController = function (_baseController) {
}()
/**
* 添加项目分组
* 获取项目分组列表
* @interface /group/list
* @method get
* @category group
* @foldnumber 10
* @returns {Object}
* @example
* @example ./api/group/list.json
*/
}, {
@ -197,6 +197,18 @@ var groupController = function (_baseController) {
return list;
}()
/**
* 删除项目分组
* @interface /group/del
* @method post
* @param {String} id 项目分组id
* @category group
* @foldnumber 10
* @returns {Object}
* @example ./api/group/del.json
*/
}, {
key: 'del',
value: function () {
@ -239,6 +251,20 @@ var groupController = function (_baseController) {
return del;
}()
/**
* 更新项目分组
* @interface /group/up
* @method post
* @param {String} id 项目分组id
* @param {String} group_name 项目分组名称
* @param {String} group_desc 项目分组描述
* @category group
* @foldnumber 10
* @returns {Object}
* @example ./api/group/up.json
*/
}, {
key: 'up',
value: function () {

View File

@ -8,65 +8,425 @@ var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _commons = require('../utils/commons');
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 _interface = require('../models/interface.js');
var _interface2 = _interopRequireDefault(_interface);
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 }; }
module.exports = {
add: function add(ctx) {
var _this = this;
var interfaceController = function (_baseController) {
(0, _inherits3.default)(interfaceController, _baseController);
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee() {
var data, result;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
data = {
title: 'yapi',
content: 'content',
uid: 'abc'
};
_context.next = 3;
return _interface2.default.save(data);
function interfaceController(ctx) {
(0, _classCallCheck3.default)(this, interfaceController);
case 3:
result = _context.sent;
var _this = (0, _possibleConstructorReturn3.default)(this, (interfaceController.__proto__ || (0, _getPrototypeOf2.default)(interfaceController)).call(this, ctx));
(0, _commons.log)('interface err...', 'error');
ctx.body = (0, _commons.resReturn)(result);
case 6:
case 'end':
return _context.stop();
}
}
}, _callee, _this);
}))();
},
list: function list(ctx) {
var _this2 = this;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2() {
var data;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
data = _interface2.default.find();
ctx.body = 1;
case 2:
case 'end':
return _context2.stop();
}
}
}, _callee2, _this2);
}))();
_this.Model = _yapi2.default.getInst(_interface2.default);
return _this;
}
};
/**
* 添加项目分组
* @interface /interface/add
* @method POST
* @category interface
* @foldnumber 10
* @param {Number} project_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描述
* @param {String} [req_params_type] 请求参数方式["form", "json", "text", "xml"]四种
* @param {Mixed} [req_params] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_params[].name] 请求参数名
* @param {String} [req_params[].value] 请求参数值可填写生成规则mock@email随机生成一条email
* @param {String} [req_params[].type] 请求参数类型["text", "file"]两种
* @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 () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
var params, checkRepeat, data, result;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
params = ctx.request.body;
params.method = params.method || 'GET';
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
if (params.project_id) {
_context.next = 5;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空'));
case 5:
if (params.path) {
_context.next = 7;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口请求路径不能为空'));
case 7:
_context.next = 9;
return this.Model.checkRepeat(params.path, params.method);
case 9:
checkRepeat = _context.sent;
if (!(checkRepeat > 0)) {
_context.next = 12;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']'));
case 12:
_context.prev = 12;
data = {
project_id: params.project_id,
path: params.path,
desc: params.desc,
method: params.method,
req_headers: params.req_headers,
req_params_type: params.req_params_type,
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()
};
if (data.req_params_type === 'form') data.req_params_form = params.req_params;else data.req_params_other = params.req_params;
_context.next = 17;
return this.Model.save(data);
case 17:
result = _context.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context.next = 24;
break;
case 21:
_context.prev = 21;
_context.t0 = _context['catch'](12);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t0.message);
case 24:
case 'end':
return _context.stop();
}
}
}, _callee, this, [[12, 21]]);
}));
function add(_x) {
return _ref.apply(this, arguments);
}
return add;
}()
}, {
key: 'get',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) {
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();
}
}
}, _callee2, this, [[3, 10]]);
}));
function get(_x2) {
return _ref2.apply(this, arguments);
}
return get;
}()
}, {
key: 'list',
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) {
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);
ctx.body = _yapi2.default.commons.resReturn(null, 402, e.message);
case 13:
case 'end':
return _context3.stop();
}
}
}, _callee3, this, [[3, 10]]);
}));
function list(_x3) {
return _ref3.apply(this, arguments);
}
return list;
}()
}, {
key: 'up',
value: function () {
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(ctx) {
var params, id, checkRepeat, data, result;
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
params = ctx.request.body;
params.method = params.method || 'GET';
id = ctx.request.body.id;
if (id) {
_context4.next = 5;
break;
}
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口id不能为空'));
case 5:
if (!params.path) {
_context4.next = 11;
break;
}
_context4.next = 8;
return this.Model.checkRepeat(params.path, params.method);
case 8:
checkRepeat = _context4.sent;
if (!(checkRepeat > 0)) {
_context4.next = 11;
break;
}
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']'));
case 11:
data = {
up_time: _yapi2.default.commons.time()
};
if (params.path) data.path = params.path;
if (params.desc) data.desc = params.desc;
if (params.method) data.method = params.method;
if (params.req_headers) data.req_headers = params.req_headers;
if (params.req_params_type === 'form') data.req_params_form = params.req_params;else data.req_params_other = params.req_params;
if (params.res_body_type) data.res_body_type = params.res_body_type;
if (params.res_body) data.res_body = params.res_body;
_context4.prev = 19;
_context4.next = 22;
return this.Model.up(id, data);
case 22:
result = _context4.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context4.next = 29;
break;
case 26:
_context4.prev = 26;
_context4.t0 = _context4['catch'](19);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context4.t0.message);
case 29:
case 'end':
return _context4.stop();
}
}
}, _callee4, this, [[19, 26]]);
}));
function up(_x4) {
return _ref4.apply(this, arguments);
}
return up;
}()
}, {
key: 'del',
value: function () {
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) {
var id, result;
return _regenerator2.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.prev = 0;
id = ctx.request.body.id;
if (id) {
_context5.next = 4;
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口id不能为空'));
case 4:
_context5.next = 6;
return this.jungeMemberAuth(id);
case 6:
_context5.t0 = _context5.sent;
if (!(_context5.t0 !== true)) {
_context5.next = 9;
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限'));
case 9:
_context5.next = 11;
return this.Model.del(id);
case 11:
result = _context5.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context5.next = 18;
break;
case 15:
_context5.prev = 15;
_context5.t1 = _context5['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, e.message);
case 18:
case 'end':
return _context5.stop();
}
}
}, _callee5, this, [[0, 15]]);
}));
function del(_x5) {
return _ref5.apply(this, arguments);
}
return del;
}()
}]);
return interfaceController;
}(_base2.default);
module.exports = interfaceController;

File diff suppressed because it is too large Load Diff

View File

@ -32,15 +32,39 @@ var baseModel = function () {
this.schema = new _mongoose2.default.Schema(this.getSchema());
this.name = this.getName();
this.schema.plugin(_mongooseAutoIncrement2.default.plugin, this.name);
if (this.isNeedAutoIncrement() === true) {
this.schema.plugin(_mongooseAutoIncrement2.default.plugin, {
model: this.name,
field: this.getPrimaryKey(),
startAt: 101,
incrementBy: _yapi2.default.commons.rand(1, 100)
});
}
this.model = _yapi2.default.db(this.name, this.schema);
}
/**
* 获取collection的schema结构
*/
(0, _createClass3.default)(baseModel, [{
key: 'isNeedAutoIncrement',
value: function isNeedAutoIncrement() {
return true;
}
/**
* 可通过覆盖此方法生成其他自增字段
*/
}, {
key: 'getPrimaryKey',
value: function getPrimaryKey() {
return '_id';
}
/**
* 获取collection的schema结构
*/
}, {
key: 'getSchema',
value: function getSchema() {
_yapi2.default.commons.log('Model Class need getSchema function', 'error');

View File

@ -1,37 +1,122 @@
'use strict';
var _mongoose = require('mongoose');
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _mongoose2 = _interopRequireDefault(_mongoose);
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 _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _base = require('./base.js');
var _base2 = _interopRequireDefault(_base);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Schema = _mongoose2.default.Schema;
var interfaceSchema = new Schema({
title: String,
content: String,
uid: String
});
var interfaceModel = function (_baseModel) {
(0, _inherits3.default)(interfaceModel, _baseModel);
var interfaceModel = _mongoose2.default.model('interface', interfaceSchema);
function interfaceModel() {
(0, _classCallCheck3.default)(this, interfaceModel);
return (0, _possibleConstructorReturn3.default)(this, (interfaceModel.__proto__ || (0, _getPrototypeOf2.default)(interfaceModel)).apply(this, arguments));
}
function save(data) {
var m = new interfaceModel(data);
return m.save();
}
(0, _createClass3.default)(interfaceModel, [{
key: 'getName',
value: function getName() {
return 'interface';
}
}, {
key: 'getSchema',
value: function getSchema() {
return {
uid: { type: Number, required: true },
path: { type: String, required: true },
method: { type: String, required: true },
project_id: { type: Number, required: true },
desc: String,
add_time: Number,
up_time: Number,
req_headers: [{
name: String, value: String, desc: String, required: Boolean
}],
req_params_type: {
type: String,
enum: ["form", "json", "text", "xml"]
},
req_params_form: [{
name: String, value: String, value_type: { type: String, enum: ["text", "file"] }, desc: String, required: Boolean
}],
req_params_other: String,
res_body_type: {
type: String,
enum: ["json", "text", "xml"]
},
res_body: String
};
}
}, {
key: 'save',
value: function save(data) {
var m = new this.model(data);
return m.save();
}
}, {
key: 'get',
value: function get(id) {
return this.model.findOne({
_id: id
}).exec();
}
}, {
key: 'checkRepeat',
value: function checkRepeat(path, method) {
return this.model.count({
path: path,
method: method
});
}
}, {
key: 'list',
value: function list(group_id) {
return this.model.find({
group_id: group_id
}).exec();
}
}, {
key: 'del',
value: function del(id) {
return this.model.deleteOne({
_id: id
});
}
}, {
key: 'up',
value: function up(id, data) {
data.up_time = _yapi2.default.commons.time();
return this.model.update({
_id: id
}, data, { runValidators: true });
}
}]);
return interfaceModel;
}(_base2.default);
function findById(id) {
return interfaceModel.findOne({
_id: id
}, 'title content');
}
function find() {
return interfaceModel.find({ title: 2222 }).exec();
}
module.exports = {
save: save,
findById: findById,
find: find
};
module.exports = interfaceModel;

View File

@ -112,8 +112,9 @@ var projectModel = function (_baseModel) {
}, {
key: 'addMember',
value: function addMember(id, uid) {
console.log(id, uid);
return this.model.update({
_id: _id, id: id
_id: id
}, {
$push: { members: uid }
});

View File

@ -63,6 +63,15 @@ createAction('project', 'list', 'get', 'list');
createAction('project', 'get', 'get', 'get');
createAction('project', 'up', 'post', 'up');
createAction('project', 'del', 'post', 'del');
createAction('project', 'add_member', 'post', 'addMember');
createAction('project', 'del_member', 'post', 'delMember');
//interface
createAction('interface', 'add', 'post', 'add');
createAction('interface', 'list', 'get', 'list');
createAction('interface', 'get', 'get', 'get');
createAction('interface', 'up', 'post', 'up');
createAction('interface', 'del', 'post', 'del');
/**
*

View File

@ -86,4 +86,16 @@ exports.fieldSelect = function (data, field) {
data[f] && (arr[f] = data[f]);
});
return arr;
};
exports.rand = function (min, max) {
return Math.floor(Math.random() * (max - min) + min);
};
exports.json_parse = function (json) {
try {
return JSON.parse(json);
} catch (e) {
return json;
}
};

View File

@ -33,7 +33,7 @@
"options": {
"type": "interface", // component lib component
"source": true, // false
"categories":["group"]
"categories":["group","user","project", "interface"]
}
}]
}