yapi/server/controllers/interface.js

863 lines
34 KiB
JavaScript
Raw Normal View History

2017-07-27 15:06:42 +08:00
import interfaceModel from '../models/interface.js';
2017-08-18 20:35:31 +08:00
import interfaceCatModel from '../models/interfaceCat.js';
2017-08-28 18:01:28 +08:00
import interfaceCaseModel from '../models/interfaceCase.js'
import followModel from '../models/follow.js'
2017-08-29 13:56:49 +08:00
import _ from 'underscore';
2017-09-01 11:04:29 +08:00
import url from 'url';
2017-07-27 15:06:42 +08:00
import baseController from './base.js';
import yapi from '../yapi.js';
2017-08-17 15:19:47 +08:00
import userModel from '../models/user.js';
import projectModel from '../models/project.js';
2017-08-16 10:56:42 +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);
this.catModel = yapi.getInst(interfaceCatModel);
this.projectModel = yapi.getInst(projectModel);
2017-08-28 18:01:28 +08:00
this.caseModel = yapi.getInst(interfaceCaseModel);
this.followModel = yapi.getInst(followModel);
this.userModel = yapi.getInst(userModel);
2017-07-10 11:56:53 +08:00
}
/**
* 添加项目分组
* @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"]四种
* @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-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',
2017-08-18 20:35:31 +08:00
desc: 'string',
catid: 'number'
2017-07-27 15:06:42 +08:00
});
2017-08-24 14:17:19 +08:00
let auth = await this.checkAuth(params.project_id, 'project', 'edit')
if (!auth) {
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
}
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-08-29 13:56:49 +08:00
params.req_params = params.req_params || [];
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-08-18 20:35:31 +08:00
catid: params.catid,
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
};
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-29 13:56:49 +08:00
if (params.path.indexOf(":") > 0) {
let paths = params.path.split("/"), name, i;
2017-08-29 13:56:49 +08:00
for (i = 1; i < paths.length; i++) {
if (paths[i][0] === ':') {
name = paths[i].substr(1);
if (!_.find(params.req_params, { name: name })) {
params.req_params.push({
name: name,
desc: ''
})
}
}
}
}
if (params.req_params.length > 0) {
2017-08-16 10:56:42 +08:00
data.type = 'var'
data.req_params = params.req_params;
2017-08-16 10:56:42 +08:00
} else {
data.type = 'static'
}
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);
2017-08-29 13:56:49 +08:00
this.catModel.get(params.catid).then((cate) => {
2017-08-24 19:27:48 +08:00
let username = this.getUsername();
let title = `用户 "${username}" 为分类 "${cate.name}" 添加了接口 "${data.title}"`
2017-08-24 19:27:48 +08:00
yapi.commons.saveLog({
content: title,
2017-08-24 19:27:48 +08:00
type: 'project',
uid: this.getUid(),
username: username,
typeid: params.project_id
});
//let project = await this.projectModel.getBaseInfo(params.project_id);
// let interfaceUrl = `http://${ctx.request.host}/project/${params.project_id}/interface/api/${result._id}`
// this.sendNotice(params.project_id, {
// title: `${username} 新增了接口 ${data.title}`,
// content: `<div><h3>${username}新增了接口(${data.title})</h3>
// <p>项目名:${project.name}</p>
// <p>修改用户: "${username}"</p>
// <p>接口名: <a href="${interfaceUrl}">${data.title}</a></p>
// <p>接口路径: [${data.method}]${data.path}</p></div>`
// })
});
2017-08-29 13:56:49 +08:00
2017-07-10 11:56:53 +08:00
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-08-18 20:35:31 +08:00
async listByCat(ctx) {
let catid = ctx.request.query.catid;
if (!catid) {
return ctx.body = yapi.commons.resReturn(null, 400, 'catid不能为空');
}
try {
let result = await this.Model.listByCatid(catid)
ctx.body = yapi.commons.resReturn(result);
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 402, err.message);
}
}
async listByMenu(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.catModel.list(project_id), newResult = [];
2017-08-24 14:17:19 +08:00
for (let i = 0, item, list; i < result.length; i++) {
2017-08-18 20:35:31 +08:00
item = result[i].toObject()
2017-08-24 12:16:43 +08:00
list = await this.Model.listByCatid(item._id, '_id title method path')
2017-08-24 14:17:19 +08:00
for (let j = 0; j < list.length; j++) {
2017-08-18 20:35:31 +08:00
list[j] = list[j].toObject()
2017-08-24 14:17:19 +08:00
}
2017-08-18 20:35:31 +08:00
item.list = list;
2017-08-24 14:17:19 +08:00
newResult[i] = item
2017-08-18 20:35:31 +08:00
}
ctx.body = yapi.commons.resReturn(newResult);
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 402, err.message);
}
}
2017-07-11 18:22:20 +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-26 14:22:59 +08:00
params = yapi.commons.handleParams(params, {
title: 'string',
path: 'string',
method: 'string',
2017-08-18 20:35:31 +08:00
desc: 'string',
catid: 'number'
2017-07-27 15:06:42 +08:00
});
2017-08-24 14:17:19 +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;
params.message = params.message || '';
params.message = params.message.replace(/\n/g, "<br>")
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-08-24 14:17:19 +08:00
2017-07-20 19:47:10 +08:00
let interfaceData = await this.Model.get(id);
2017-08-24 14:17:19 +08:00
let auth = await this.checkAuth(interfaceData.project_id, 'project', 'edit')
if (!auth) {
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
}
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
2017-08-29 13:56:49 +08:00
if (params.path && (params.path !== interfaceData.path || params.method !== interfaceData.method)) {
2017-07-27 15:06:42 +08:00
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 (!_.isUndefined(params.path)) {
2017-07-27 15:06:42 +08:00
data.path = params.path;
}
if (!_.isUndefined(params.title)) {
2017-07-27 15:06:42 +08:00
data.title = params.title;
}
if (!_.isUndefined(params.desc)) {
2017-07-27 15:06:42 +08:00
data.desc = params.desc;
}
if (!_.isUndefined(params.method)) {
2017-07-27 15:06:42 +08:00
data.method = params.method;
2017-07-10 11:56:53 +08:00
}
if (!_.isUndefined(params.catid)) {
2017-08-18 20:35:31 +08:00
data.catid = params.catid;
}
if (!_.isUndefined(params.req_headers)) {
2017-07-27 15:06:42 +08:00
data.req_headers = params.req_headers;
}
if (!_.isUndefined(params.req_body_form)) {
2017-08-15 12:08:59 +08:00
data.req_body_form = params.req_body_form;
}
2017-08-16 10:56:42 +08:00
if (params.req_params && Array.isArray(params.req_params) && params.req_params.length > 0) {
data.type = 'var'
data.req_params = params.req_params;
2017-08-16 10:56:42 +08:00
} else {
data.type = 'static'
2017-08-31 18:04:45 +08:00
data.req_params = [];
}
2017-08-16 10:56:42 +08:00
if (!_.isUndefined(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
}
if (!_.isUndefined(params.req_body_other)) {
2017-08-15 12:08:59 +08:00
data.req_body_other = params.req_body_other;
2017-07-27 15:06:42 +08:00
}
2017-07-10 11:56:53 +08:00
if (!_.isUndefined(params.req_body_type)) {
2017-08-18 12:37:32 +08:00
data.req_body_type = params.req_body_type;
}
if (!_.isUndefined(params.res_body_type)) {
2017-07-27 15:06:42 +08:00
data.res_body_type = params.res_body_type;
}
if (!_.isUndefined(params.res_body)) {
2017-07-27 15:06:42 +08:00
data.res_body = params.res_body;
}
2017-07-10 11:56:53 +08:00
if (!_.isUndefined(params.status)) {
2017-08-20 22:13:46 +08:00
data.status = params.status;
}
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);
let username = this.getUsername();
2017-08-24 14:17:19 +08:00
if (params.catid) {
2017-08-29 13:56:49 +08:00
this.catModel.get(+params.catid).then((cate) => {
2017-08-24 19:27:48 +08:00
yapi.commons.saveLog({
content: `用户 "${username}" 更新了分类 "${cate.name}" 下的接口 "${data.title}"`,
type: 'project',
uid: this.getUid(),
username: username,
typeid: cate.project_id
});
});
2017-08-24 14:17:19 +08:00
} else {
2017-08-24 19:27:48 +08:00
let cateid = interfaceData.catid;
2017-08-29 13:56:49 +08:00
this.catModel.get(cateid).then((cate) => {
2017-08-24 19:27:48 +08:00
yapi.commons.saveLog({
content: `用户 "${username}" 更新了分类 "${cate.name}" 下的接口 "${data.title}"`,
type: 'project',
uid: this.getUid(),
username: username,
typeid: cate.project_id
});
});
}
if (params.switch_notice === true) {
let project = await this.projectModel.getBaseInfo(interfaceData.project_id);
let interfaceUrl = `http://${ctx.request.host}/project/${interfaceData.project_id}/interface/api/${id}`
this.sendNotice(interfaceData.project_id, {
title: `${username} 更新了接口`,
content: `<div><h3>${username}更新了接口(${data.title})</h3>
<p>项目名${project.name} </p>
<p>修改用户: ${username}</p>
<p>接口名: <a href="${interfaceUrl}">${data.title}</a></p>
<p>接口路径: [${data.method}]${data.path}</p>
<p>详细改动日志: ${params.message}</p></div>`
})
}
2017-08-29 13:56:49 +08:00
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()) {
2017-08-24 14:17:19 +08:00
let auth = await this.checkAuth(data.project_id, 'project', 'danger')
if (!auth) {
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
2017-07-11 18:22:20 +08:00
}
2017-07-10 11:56:53 +08:00
}
2017-07-11 18:22:20 +08:00
let inter = await this.Model.get(id);
2017-07-10 11:56:53 +08:00
let result = await this.Model.del(id);
2017-08-28 18:01:28 +08:00
await this.caseModel.delByInterfaceId(id);
let username = this.getUsername();
2017-08-29 13:56:49 +08:00
this.catModel.get(inter.catid).then((cate) => {
2017-08-24 19:27:48 +08:00
yapi.commons.saveLog({
content: `用户 "${username}" 删除了分类 "${cate.name}" 下的接口 "${inter.title}"`,
type: 'project',
uid: this.getUid(),
username: username,
typeid: cate.project_id
});
})
2017-08-29 13:56:49 +08:00
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
}
async solveConflict(ctx) {
2017-08-17 15:19:47 +08:00
try {
let id = parseInt(ctx.query.id, 10), result, userInst, userinfo, data;
if (!id) return ctx.websocket.send("id 参数有误");
2017-08-18 20:35:31 +08:00
result = await this.Model.get(id), userinfo;
if (result.edit_uid !== 0 && result.edit_uid !== this.getUid()) {
2017-08-17 15:19:47 +08:00
userInst = yapi.getInst(userModel);
userinfo = await userInst.findById(result.edit_uid);
data = {
errno: result.edit_uid,
2017-08-18 20:35:31 +08:00
data: { uid: result.edit_uid, username: userinfo.username }
2017-08-17 15:19:47 +08:00
}
2017-08-18 20:35:31 +08:00
} else {
this.Model.upEditUid(id, this.getUid()).then()
2017-08-17 15:19:47 +08:00
data = {
errno: 0,
data: result
}
}
2017-08-18 20:35:31 +08:00
ctx.websocket.send(JSON.stringify(data));
ctx.websocket.on('close', () => {
2017-08-17 15:19:47 +08:00
this.Model.upEditUid(id, 0).then()
})
} catch (err) {
yapi.commons.log(err, 'error')
}
}
2017-08-18 20:35:31 +08:00
async addCat(ctx) {
try {
let params = ctx.request.body;
params = yapi.commons.handleParams(params, {
name: 'string',
project_id: 'number',
desc: 'string'
});
if (!params.project_id) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
2017-08-24 14:17:19 +08:00
let auth = await this.checkAuth(params.project_id, 'project', 'edit')
if (!auth) {
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
}
2017-08-18 20:35:31 +08:00
if (!params.name) {
return ctx.body = yapi.commons.resReturn(null, 400, '名称不能为空');
}
2017-08-24 14:17:19 +08:00
2017-08-18 20:35:31 +08:00
let result = await this.catModel.save({
name: params.name,
project_id: params.project_id,
desc: params.desc,
uid: this.getUid(),
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
})
let username = this.getUsername();
yapi.commons.saveLog({
content: `用户 "${username}" 添加了分类 "${params.name}"`,
type: 'project',
uid: this.getUid(),
username: username,
typeid: params.project_id
});
2017-08-18 20:35:31 +08:00
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
async upCat(ctx) {
try {
let params = ctx.request.body;
let result = await this.catModel.up(params.catid, {
2017-08-19 19:06:09 +08:00
name: params.name,
desc: params.desc,
2017-08-18 20:35:31 +08:00
up_time: yapi.commons.time()
2017-08-29 13:56:49 +08:00
});
let username = this.getUsername();
let cate = await this.catModel.get(params.catid);
2017-08-24 14:17:19 +08:00
let auth = await this.checkAuth(cate.project_id, 'project', 'edit')
if (!auth) {
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
}
yapi.commons.saveLog({
content: `用户 "${username}" 更新了分类 "${cate.name}"`,
type: 'project',
uid: this.getUid(),
username: username,
typeid: cate.project_id
});
2017-08-18 20:35:31 +08:00
ctx.body = yapi.commons.resReturn(result)
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 400, e.message)
}
}
async delCat(ctx) {
try {
let id = ctx.request.body.catid;
let catData = await this.catModel.get(id);
if (!catData) {
ctx.body = yapi.commons.resReturn(null, 400, "不存在的分类")
}
if (catData.uid !== this.getUid()) {
let auth = await this.checkAuth(catData.project_id, 'project', 'danger')
if (!auth) {
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
}
}
let username = this.getUsername();
yapi.commons.saveLog({
2017-08-24 19:27:48 +08:00
content: `用户 "${username}" 删除了分类 "${catData.name}" 及该分类下的接口`,
type: 'project',
uid: this.getUid(),
username: username,
2017-08-24 19:27:48 +08:00
typeid: catData.project_id
});
2017-08-24 19:27:48 +08:00
let result = await this.catModel.del(id);
let r = await this.Model.delByCatid(id);
2017-08-19 19:06:09 +08:00
return ctx.body = yapi.commons.resReturn(r);
2017-08-18 20:35:31 +08:00
} catch (e) {
yapi.commons.resReturn(null, 400, e.message)
}
}
2017-08-31 17:26:09 +08:00
/**
* 接口数据上传
* @interface /interface/interUpload
* @method POST
* @category interface
* @foldnumber 10
* @param {Number} project_id 项目id不能为空
* @param {String} title 接口标题不能为空
* @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_body_type] 请求参数方式["form", "json", "text", "xml"]四种
* @param {Array} [req_params] name, desc两个参数
* @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类型的请求参数可保存到此字段
* @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 interUpload(ctx) {
let interData = ctx.request.body.interData;
let project_id = ctx.request.body.project_id;
let request = interData.requests;
let data1 = [];
let catid = ctx.request.body.catid;
let auth = await this.checkAuth(project_id, 'project', 'danger');
if (!auth) {
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
}
if (!project_id) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
if (!catid) {
return ctx.body = yapi.commons.resReturn(null, 400, '分类id不能为空');
}
const len = request.length;
let successNum = len;
if(request &&len){
for(let i = 0;i<len;i++){
try {
2017-09-01 11:04:29 +08:00
let path = url.parse(request[i].url.replace(/{{(\w+)}}/,'')).path;
2017-08-31 17:26:09 +08:00
let reg = /^(\w+):\/\/([^\/:]*)(?::(\d+))?\/([^\/\?]*)(\/.*)/;
2017-09-01 11:04:29 +08:00
// let path = request[i].url;
// let result = path.match(reg);
// if(result){
// path = result[4]+ result[5];
// path = path.split('?')[0].replace(/{{(\w+)}}/,'').replace(/\/$/,'');
// if(path.indexOf("/") > 0){
// path = '/'+ path;
// }
// }else{
// // path.replace(/\{\{\ [0-9a-zA-Z-_]* }\}/g,'');
// path = path.replace(/{{(\w+)}}/,'');
// if(path.indexOf("/") > 0){
// path = '/'+ path;
// }
// }
2017-08-31 17:26:09 +08:00
let title = request[i].name;
if(reg.test(request[i].name)){
title = path;
}
let inter = {
project_id: project_id,
title: title,
path: path,
method: request[i].method,
req_headers: '',
// req_body_type: request[i].dataMode,
req_params: '',
req_body_form: '',
req_body_other: '',
res_body_type: 'json',
res_body: '',
desc: request[i].description
};
2017-09-01 11:04:29 +08:00
console.log(inter.path);
2017-08-31 17:26:09 +08:00
// req_body_type req_body_form
if(request[i].dataMode){
// console.log(i);
2017-09-01 11:04:29 +08:00
if(request[i].dataMode === 'params' || request[i].dataMode==='urlencoded'){
2017-08-31 17:26:09 +08:00
inter.req_body_type = 'form';
inter.req_body_form = [];
const data = request[i].data;
for(let item in data){
inter.req_body_form.push({
name: data[item].key,
value: data[item].value,
type: data[item].type
});
}
}else if(request[i].dataMode === 'raw'){
inter.req_body_form = [];
2017-09-01 11:04:29 +08:00
// console.log(request[i].headers.inedxOf('application/json')>-1);
if(request[i].headers&&request[i].headers.indexOf('application/json')>-1){
inter.req_body_type = 'json';
}else{
inter.req_body_type = 'raw';
}
2017-08-31 17:26:09 +08:00
inter.req_body_other = request[i].rawModeData;
}else if(request[i].dataMode === 'binary'){
2017-09-01 11:04:29 +08:00
inter.req_body_type = 'file';
inter.req_body_other = request[i].rawModeData;
2017-08-31 17:26:09 +08:00
}
}
// req_params
if(request[i].queryParams){
inter.req_params = [];
const queryParams = request[i].queryParams;
for(let item in queryParams){
inter.req_params.push({
name: queryParams[item].key,
desc: queryParams[item].description,
required: queryParams[item].enable
});
}
}
// req_headers
if(request[i].headerData){
inter.req_headers = [];
const headerData = request[i].headerData;
for(let item in headerData){
inter.req_headers.push({
name: headerData[item].key,
value: headerData[item].value,
required: headerData[item].enable,
desc: headerData[item].description
});
}
}
if (!inter.project_id) {
// return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
continue;
}
if (!inter.path) {
// return ctx.body = yapi.commons.resReturn(null, 400, '接口请求路径不能为空');
continue;
}
if (!yapi.commons.verifyPath(inter.path)) {
successNum--;
// return ctx.body = yapi.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/');
continue;
}
let checkRepeat = await this.Model.checkRepeat(inter.project_id, inter.path, inter.method);
if (checkRepeat > 0) {
successNum--;
// return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + inter.path + '[' + inter.method + ']');
continue;
}
let data = {
...inter,
catid: catid,
uid: this.getUid(),
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
};
if ( data.req_params.length > 0) {
data.type = 'var';
data.req_params = data.req_params;
} else {
data.type = 'static';
}
2017-09-01 11:04:29 +08:00
// data1.push(data);
// console.log(data);
2017-08-31 17:26:09 +08:00
let res = await this.Model.save(data);
// return ctx.body = yapi.commons.resReturn(res);
}catch(e){
// ctx.body = yapi.commons.resReturn(e.message);
successNum--;
}
}
}
try{
if(successNum){
this.catModel.get(catid).then((cate) => {
let username = this.getUsername();
yapi.commons.saveLog({
content: `用户 "${username}" 为分类 "${cate.name}" 成功导入了 ${successNum} 个接口`,
type: 'project',
uid: this.getUid(),
username: username,
typeid: project_id
});
});
}
}catch(e){
}
return ctx.body = yapi.commons.resReturn(successNum);
}
2017-09-01 14:59:53 +08:00
2017-08-31 17:26:09 +08:00
async getCatMenu(ctx) {
let project_id = ctx.request.query.project_id;
if(!project_id){
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
try{
let res = await this.catModel.list(project_id);
return ctx.body = yapi.commons.resReturn(res);
}catch(e){
yapi.commons.resReturn(null, 400, e.message);
}
}
sendNotice(projectId, data) {
this.followModel.listByProjectId(projectId).then(list => {
let users = [];
list.forEach(item => {
users.push(item.uid)
})
this.userModel.findByUids(users).then(list => {
list.forEach(item => {
yapi.commons.sendMail({
to: item.email,
contents: data.content,
subject: data.title
});
})
})
});
}
2017-08-18 20:35:31 +08:00
2017-07-03 16:16:05 +08:00
}
2017-07-10 11:56:53 +08:00
module.exports = interfaceController;