2017-07-27 15:06:42 +08:00
|
|
|
|
import interfaceModel from '../models/interface.js';
|
|
|
|
|
import baseController from './base.js';
|
|
|
|
|
import yapi from '../yapi.js';
|
2017-07-10 11:56:53 +08:00
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
class interfaceController extends baseController {
|
|
|
|
|
constructor(ctx) {
|
|
|
|
|
super(ctx);
|
2017-07-10 11:56:53 +08:00
|
|
|
|
this.Model = yapi.getInst(interfaceModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加项目分组
|
|
|
|
|
* @interface /interface/add
|
|
|
|
|
* @method POST
|
|
|
|
|
* @category interface
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {Number} project_id 项目id,不能为空
|
2017-07-20 16:14:20 +08:00
|
|
|
|
* @param {String} title 接口标题,不能为空
|
2017-07-10 11:56:53 +08:00
|
|
|
|
* @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描述
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @param {String} [req_body_type] 请求参数方式,有["form", "json", "text", "xml"]四种
|
2017-08-15 17:38:22 +08:00
|
|
|
|
* @param {Array} [req_params] name, desc两个参数
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @param {Mixed} [req_body_form] 请求参数,如果请求方式是form,参数是Array数组,其他格式请求参数是字符串
|
|
|
|
|
* @param {String} [req_body_form[].name] 请求参数名
|
|
|
|
|
* @param {String} [req_body_form[].value] 请求参数值,可填写生成规则(mock)。如@email,随机生成一条email
|
|
|
|
|
* @param {String} [req_body_form[].type] 请求参数类型,有["text", "file"]两种
|
|
|
|
|
* @param {String} [req_body_other] 非form类型的请求参数可保存到此字段
|
2017-07-10 11:56:53 +08:00
|
|
|
|
* @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
|
|
|
|
|
*/
|
2017-07-27 15:06:42 +08:00
|
|
|
|
async add(ctx) {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
let params = ctx.request.body;
|
2017-07-27 15:06:42 +08:00
|
|
|
|
|
2017-07-26 14:22:59 +08:00
|
|
|
|
params = yapi.commons.handleParams(params, {
|
2017-07-26 17:56:51 +08:00
|
|
|
|
project_id: 'number',
|
2017-07-26 14:22:59 +08:00
|
|
|
|
title: 'string',
|
|
|
|
|
path: 'string',
|
|
|
|
|
method: 'string',
|
|
|
|
|
desc: 'string'
|
2017-07-27 15:06:42 +08:00
|
|
|
|
});
|
2017-07-10 11:56:53 +08:00
|
|
|
|
params.method = params.method || 'GET';
|
2017-07-27 15:06:42 +08:00
|
|
|
|
params.method = params.method.toUpperCase();
|
2017-07-10 11:56:53 +08:00
|
|
|
|
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
|
2017-07-27 15:06:42 +08:00
|
|
|
|
|
|
|
|
|
if (!params.project_id) {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
if (!params.path) {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '接口请求路径不能为空');
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
if (!yapi.commons.verifyPath(params.path)) {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/');
|
2017-07-24 11:24:08 +08:00
|
|
|
|
}
|
2017-07-27 15:06:42 +08:00
|
|
|
|
|
2017-07-26 17:56:51 +08:00
|
|
|
|
let checkRepeat = await this.Model.checkRepeat(params.project_id, params.path, params.method);
|
2017-07-24 11:24:08 +08:00
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
if (checkRepeat > 0) {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']');
|
|
|
|
|
}
|
2017-07-10 11:56:53 +08:00
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
try {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
let data = {
|
|
|
|
|
project_id: params.project_id,
|
2017-07-20 18:37:00 +08:00
|
|
|
|
title: params.title,
|
2017-07-10 11:56:53 +08:00
|
|
|
|
path: params.path,
|
|
|
|
|
desc: params.desc,
|
|
|
|
|
method: params.method,
|
|
|
|
|
req_headers: params.req_headers,
|
2017-08-15 12:08:59 +08:00
|
|
|
|
req_body_type: params.req_body_type,
|
2017-07-27 15:06:42 +08:00
|
|
|
|
res_body: params.res_body,
|
2017-07-10 11:56:53 +08:00
|
|
|
|
res_body_type: params.res_body_type,
|
|
|
|
|
uid: this.getUid(),
|
|
|
|
|
add_time: yapi.commons.time(),
|
|
|
|
|
up_time: yapi.commons.time()
|
2017-07-27 15:06:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
2017-08-15 17:38:22 +08:00
|
|
|
|
if (params.req_query) {
|
2017-08-15 12:08:59 +08:00
|
|
|
|
data.req_query = params.req_query;
|
2017-07-27 15:06:42 +08:00
|
|
|
|
}
|
2017-08-15 12:08:59 +08:00
|
|
|
|
|
|
|
|
|
if (params.req_body_form) {
|
|
|
|
|
data.req_body_form = params.req_body_form;
|
|
|
|
|
}
|
2017-08-15 17:38:22 +08:00
|
|
|
|
if (params.req_params) {
|
|
|
|
|
data.req_params = params.req_params;
|
|
|
|
|
}
|
2017-08-15 12:08:59 +08:00
|
|
|
|
if (params.req_body_other) {
|
|
|
|
|
data.req_body_other = params.req_body_other;
|
2017-07-10 11:56:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result = await this.Model.save(data);
|
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
2017-07-27 15:06:42 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
2017-07-10 11:56:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 18:22:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* 添加项目分组
|
|
|
|
|
* @interface /interface/get
|
|
|
|
|
* @method GET
|
|
|
|
|
* @category interface
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {Number} id 接口id,不能为空
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example ./api/interface/get.json
|
|
|
|
|
*/
|
2017-07-27 15:06:42 +08:00
|
|
|
|
async get(ctx) {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
let params = ctx.request.query;
|
2017-07-27 15:06:42 +08:00
|
|
|
|
|
|
|
|
|
if (!params.id) {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
|
|
|
|
|
}
|
2017-07-27 15:06:42 +08:00
|
|
|
|
|
|
|
|
|
try {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
let result = await this.Model.get(params.id);
|
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
2017-07-27 15:06:42 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
2017-07-10 11:56:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 18:22:20 +08:00
|
|
|
|
/**
|
2017-07-20 19:47:10 +08:00
|
|
|
|
* 接口列表
|
2017-07-11 18:22:20 +08:00
|
|
|
|
* @interface /interface/list
|
|
|
|
|
* @method GET
|
|
|
|
|
* @category interface
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {Number} project_id 项目id,不能为空
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example ./api/interface/list.json
|
|
|
|
|
*/
|
2017-07-27 15:06:42 +08:00
|
|
|
|
async list(ctx) {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
let project_id = ctx.request.query.project_id;
|
2017-07-27 15:06:42 +08:00
|
|
|
|
|
|
|
|
|
if (!project_id) {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
|
|
|
|
|
}
|
2017-07-27 15:06:42 +08:00
|
|
|
|
|
|
|
|
|
try {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
let result = await this.Model.list(project_id);
|
2017-07-27 15:06:42 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, err.message);
|
2017-07-10 11:56:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 18:22:20 +08:00
|
|
|
|
/**
|
2017-07-20 18:37:00 +08:00
|
|
|
|
* 编辑接口
|
2017-07-11 18:22:20 +08:00
|
|
|
|
* @interface /interface/up
|
|
|
|
|
* @method POST
|
|
|
|
|
* @category interface
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {Number} 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描述
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @param {String} [req_body_type] 请求参数方式,有["form", "json", "text", "xml"]四种
|
|
|
|
|
* @param {Mixed} [req_body_form] 请求参数,如果请求方式是form,参数是Array数组,其他格式请求参数是字符串
|
|
|
|
|
* @param {String} [req_body_form[].name] 请求参数名
|
|
|
|
|
* @param {String} [req_body_form[].value] 请求参数值,可填写生成规则(mock)。如@email,随机生成一条email
|
|
|
|
|
* @param {String} [req_body_form[].type] 请求参数类型,有["text", "file"]两种
|
|
|
|
|
* @param {String} [req_body_other] 非form类型的请求参数可保存到此字段
|
2017-07-11 18:22:20 +08:00
|
|
|
|
* @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/up.json
|
|
|
|
|
*/
|
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
async up(ctx) {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
let params = ctx.request.body;
|
2017-07-27 15:06:42 +08:00
|
|
|
|
|
2017-07-26 14:22:59 +08:00
|
|
|
|
params = yapi.commons.handleParams(params, {
|
|
|
|
|
title: 'string',
|
|
|
|
|
path: 'string',
|
|
|
|
|
method: 'string',
|
|
|
|
|
desc: 'string'
|
2017-07-27 15:06:42 +08:00
|
|
|
|
});
|
2017-07-10 11:56:53 +08:00
|
|
|
|
params.method = params.method || 'GET';
|
2017-07-27 15:06:42 +08:00
|
|
|
|
params.method = params.method.toUpperCase();
|
|
|
|
|
|
2017-07-10 11:56:53 +08:00
|
|
|
|
let id = ctx.request.body.id;
|
2017-08-15 17:38:22 +08:00
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
if (!id) {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
|
|
|
|
|
}
|
2017-07-20 19:47:10 +08:00
|
|
|
|
let interfaceData = await this.Model.get(id);
|
2017-07-24 11:24:08 +08:00
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
if (params.path && !yapi.commons.verifyPath(params.path)) {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/');
|
2017-07-24 11:24:08 +08:00
|
|
|
|
}
|
2017-07-27 15:06:42 +08:00
|
|
|
|
|
|
|
|
|
if (params.path && params.path !== interfaceData.path && params.method !== interfaceData.method) {
|
|
|
|
|
let checkRepeat = await this.Model.checkRepeat(interfaceData.project_id, params.path, params.method);
|
|
|
|
|
if (checkRepeat > 0) {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']');
|
2017-07-10 11:56:53 +08:00
|
|
|
|
}
|
2017-07-27 15:06:42 +08:00
|
|
|
|
}
|
2017-07-03 16:16:05 +08:00
|
|
|
|
|
|
|
|
|
let data = {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
up_time: yapi.commons.time()
|
2017-07-27 15:06:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (params.path) {
|
|
|
|
|
data.path = params.path;
|
|
|
|
|
}
|
|
|
|
|
if (params.title) {
|
|
|
|
|
data.title = params.title;
|
|
|
|
|
}
|
|
|
|
|
if (params.desc) {
|
|
|
|
|
data.desc = params.desc;
|
|
|
|
|
}
|
|
|
|
|
if (params.method) {
|
|
|
|
|
data.method = params.method;
|
2017-07-10 11:56:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
if (params.req_headers) {
|
|
|
|
|
data.req_headers = params.req_headers;
|
|
|
|
|
}
|
2017-08-15 17:38:22 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
if (params.req_body_form) {
|
|
|
|
|
data.req_body_form = params.req_body_form;
|
|
|
|
|
}
|
2017-08-15 17:38:22 +08:00
|
|
|
|
if (params.req_params) {
|
|
|
|
|
data.req_params = params.req_params;
|
|
|
|
|
}
|
|
|
|
|
if (params.req_query) {
|
2017-08-15 12:08:59 +08:00
|
|
|
|
data.req_query = params.req_query;
|
2017-07-27 15:06:42 +08:00
|
|
|
|
}
|
2017-08-15 12:08:59 +08:00
|
|
|
|
if (params.req_body_other) {
|
|
|
|
|
data.req_body_other = params.req_body_other;
|
2017-07-27 15:06:42 +08:00
|
|
|
|
}
|
2017-07-10 11:56:53 +08:00
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
if (params.res_body_type) {
|
|
|
|
|
data.res_body_type = params.res_body_type;
|
|
|
|
|
}
|
|
|
|
|
if (params.res_body) {
|
|
|
|
|
data.res_body = params.res_body;
|
|
|
|
|
}
|
2017-07-10 11:56:53 +08:00
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
try {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
let result = await this.Model.up(id, data);
|
2017-07-27 15:06:42 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
2017-07-10 11:56:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 18:22:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* 删除接口
|
|
|
|
|
* @interface /interface/del
|
|
|
|
|
* @method GET
|
|
|
|
|
* @category interface
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {Number} id 接口id,不能为空
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example ./api/interface/del.json
|
|
|
|
|
*/
|
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
async del(ctx) {
|
|
|
|
|
try {
|
|
|
|
|
let id = ctx.request.body.id;
|
|
|
|
|
|
|
|
|
|
if (!id) {
|
2017-07-10 11:56:53 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
|
|
|
|
|
}
|
2017-07-11 18:22:20 +08:00
|
|
|
|
|
2017-07-24 10:59:17 +08:00
|
|
|
|
let data = await this.Model.get(ctx.request.body.id);
|
2017-07-11 18:22:20 +08:00
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
if (data.uid != this.getUid()) {
|
|
|
|
|
if (await this.jungeProjectAuth(data.project_id) !== true) {
|
2017-07-11 18:22:20 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
|
|
|
|
|
}
|
2017-07-10 11:56:53 +08:00
|
|
|
|
}
|
2017-07-11 18:22:20 +08:00
|
|
|
|
|
2017-07-27 15:06:42 +08:00
|
|
|
|
|
2017-07-10 11:56:53 +08:00
|
|
|
|
let result = await this.Model.del(id);
|
2017-07-27 15:06:42 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, err.message);
|
2017-07-10 11:56:53 +08:00
|
|
|
|
}
|
2017-07-03 16:16:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-10 11:56:53 +08:00
|
|
|
|
|
|
|
|
|
module.exports = interfaceController;
|