2017-09-03 08:43:13 +08:00
|
|
|
|
const interfaceColModel = require('../models/interfaceCol.js');
|
|
|
|
|
const interfaceCaseModel = require('../models/interfaceCase.js');
|
|
|
|
|
const interfaceModel = require('../models/interface.js');
|
|
|
|
|
const projectModel = require('../models/project.js');
|
|
|
|
|
const baseController = require('./base.js');
|
|
|
|
|
const yapi = require('../yapi.js');
|
2017-08-08 19:18:34 +08:00
|
|
|
|
|
|
|
|
|
class interfaceColController extends baseController{
|
|
|
|
|
constructor(ctx) {
|
|
|
|
|
super(ctx);
|
2017-08-09 16:16:35 +08:00
|
|
|
|
this.colModel = yapi.getInst(interfaceColModel);
|
|
|
|
|
this.caseModel = yapi.getInst(interfaceCaseModel);
|
2017-08-28 15:19:07 +08:00
|
|
|
|
this.interfaceModel = yapi.getInst(interfaceModel);
|
2017-08-28 20:39:32 +08:00
|
|
|
|
this.projectModel = yapi.getInst(projectModel);
|
2017-08-08 19:18:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 16:16:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取所有接口集
|
|
|
|
|
* @interface /col/list
|
|
|
|
|
* @method GET
|
|
|
|
|
* @category col
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {String} project_id email名称,不能为空
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example
|
2017-08-09 16:16:35 +08:00
|
|
|
|
*/
|
2017-08-08 19:47:36 +08:00
|
|
|
|
async list(ctx){
|
2017-08-29 11:25:50 +08:00
|
|
|
|
try {
|
2017-08-08 19:47:36 +08:00
|
|
|
|
let id = ctx.query.project_id;
|
2017-08-15 12:08:59 +08:00
|
|
|
|
let result = await this.colModel.list(id);
|
|
|
|
|
|
|
|
|
|
for(let i=0; i< result.length;i++){
|
|
|
|
|
result[i] = result[i].toObject();
|
|
|
|
|
result[i].caseList = await this.caseModel.list(result[i]._id)
|
|
|
|
|
}
|
2017-08-08 19:47:36 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
|
|
|
|
}
|
2017-08-08 19:18:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 16:16:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* 增加接口集
|
|
|
|
|
* @interface /col/add_col
|
|
|
|
|
* @method POST
|
|
|
|
|
* @category col
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {Number} project_id
|
|
|
|
|
* @param {String} name
|
|
|
|
|
* @param {String} desc
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example
|
2017-08-09 16:16:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async addCol(ctx){
|
|
|
|
|
try{
|
|
|
|
|
let params = ctx.request.body;
|
|
|
|
|
params = yapi.commons.handleParams(params, {
|
|
|
|
|
name: 'string',
|
|
|
|
|
project_id: 'number',
|
|
|
|
|
desc: 'string'
|
|
|
|
|
});
|
2017-08-15 12:08:59 +08:00
|
|
|
|
|
2017-08-09 16:16:35 +08:00
|
|
|
|
if (!params.project_id) {
|
2017-08-15 12:08:59 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
|
2017-08-09 16:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
if(!params.name){
|
2017-08-15 12:08:59 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '名称不能为空');
|
2017-08-09 16:16:35 +08:00
|
|
|
|
}
|
2017-08-15 12:08:59 +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-08-09 16:16:35 +08:00
|
|
|
|
let result = await this.colModel.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()
|
2017-08-24 16:40:33 +08:00
|
|
|
|
});
|
|
|
|
|
let username = this.getUsername();
|
|
|
|
|
yapi.commons.saveLog({
|
|
|
|
|
content: `用户 "${username}" 添加了接口集 "${params.name}"`,
|
|
|
|
|
type: 'project',
|
|
|
|
|
uid: this.getUid(),
|
|
|
|
|
username: username,
|
|
|
|
|
typeid: params.project_id
|
|
|
|
|
});
|
2017-08-09 16:16:35 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
|
|
|
|
|
}catch(e){
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取一个接口集下的所有的接口用例
|
|
|
|
|
* @interface /col/case_list
|
|
|
|
|
* @method GET
|
|
|
|
|
* @category col
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {String} col_id 接口集id
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example
|
2017-08-09 16:16:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async getCaseList(ctx){
|
|
|
|
|
try {
|
|
|
|
|
let id = ctx.query.col_id;
|
|
|
|
|
let inst = yapi.getInst(interfaceCaseModel);
|
2017-08-21 15:52:22 +08:00
|
|
|
|
let result = await inst.list(id, 'all');
|
2017-08-28 17:13:05 +08:00
|
|
|
|
for(let index=0; index< result.length; index++){
|
2017-08-29 11:25:50 +08:00
|
|
|
|
|
2017-08-28 17:13:05 +08:00
|
|
|
|
result[index] = result[index].toObject();
|
|
|
|
|
let interfaceData = await this.interfaceModel.getBaseinfo(result[index].interface_id);
|
2017-09-08 11:12:33 +08:00
|
|
|
|
if(!interfaceData){
|
|
|
|
|
await this.caseModel.del(result[index]._id);
|
|
|
|
|
result[index] = undefined;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-08-28 20:39:32 +08:00
|
|
|
|
let projectData = await this.projectModel.getBaseInfo(interfaceData.project_id);
|
|
|
|
|
result[index].path = projectData.basepath + interfaceData.path;
|
2017-08-28 17:13:05 +08:00
|
|
|
|
result[index].method = interfaceData.method;
|
|
|
|
|
}
|
2017-08-09 16:16:35 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 增加一个接口用例
|
|
|
|
|
* @interface /col/add_case
|
|
|
|
|
* @method POST
|
|
|
|
|
* @category col
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {String} casename
|
|
|
|
|
* @param {Number} col_id
|
|
|
|
|
* @param {Number} project_id
|
|
|
|
|
* @param {String} domain
|
|
|
|
|
* @param {String} path
|
|
|
|
|
* @param {String} method
|
|
|
|
|
* @param {Object} req_query
|
|
|
|
|
* @param {Object} req_headers
|
|
|
|
|
* @param {String} req_body_type
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @param {Array} req_body_form
|
2017-08-09 16:16:35 +08:00
|
|
|
|
* @param {String} req_body_other
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example
|
2017-08-09 16:16:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async addCase(ctx){
|
|
|
|
|
try{
|
|
|
|
|
let params = ctx.request.body;
|
|
|
|
|
params = yapi.commons.handleParams(params, {
|
|
|
|
|
casename: 'string',
|
|
|
|
|
project_id: 'number',
|
|
|
|
|
col_id: 'number',
|
2017-08-28 15:19:07 +08:00
|
|
|
|
interface_id: 'number',
|
2017-08-29 10:01:27 +08:00
|
|
|
|
case_env: 'string'
|
2017-08-09 16:16:35 +08:00
|
|
|
|
});
|
2017-08-15 12:08:59 +08:00
|
|
|
|
|
2017-08-24 14:17:19 +08:00
|
|
|
|
|
2017-08-09 16:16:35 +08:00
|
|
|
|
if (!params.project_id) {
|
2017-08-15 12:08:59 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
|
2017-08-09 16:16:35 +08:00
|
|
|
|
}
|
2017-08-24 14:17:19 +08:00
|
|
|
|
|
2017-08-28 15:19:07 +08:00
|
|
|
|
if(!params.interface_id){
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let auth = await this.checkAuth(params.project_id, 'project', 'edit');
|
2017-08-24 14:17:19 +08:00
|
|
|
|
if (!auth) {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 16:16:35 +08:00
|
|
|
|
if (!params.col_id) {
|
2017-08-15 12:08:59 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '接口集id不能为空');
|
2017-08-09 16:16:35 +08:00
|
|
|
|
}
|
2017-08-15 12:08:59 +08:00
|
|
|
|
|
|
|
|
|
|
2017-08-09 16:16:35 +08:00
|
|
|
|
if(!params.casename){
|
2017-08-15 12:08:59 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '用例名称不能为空');
|
2017-08-09 16:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params.uid = this.getUid();
|
|
|
|
|
params.index = 0;
|
|
|
|
|
params.add_time = yapi.commons.time();
|
|
|
|
|
params.up_time = yapi.commons.time();
|
|
|
|
|
let result = await this.caseModel.save(params);
|
2017-08-24 16:40:33 +08:00
|
|
|
|
let username = this.getUsername();
|
2017-08-24 19:27:48 +08:00
|
|
|
|
|
|
|
|
|
this.colModel.get(params.col_id).then((col)=>{
|
|
|
|
|
yapi.commons.saveLog({
|
|
|
|
|
content: `用户 "${username}" 在接口集 "${col.name}" 下添加了接口用例 "${params.casename}"`,
|
|
|
|
|
type: 'project',
|
|
|
|
|
uid: this.getUid(),
|
|
|
|
|
username: username,
|
|
|
|
|
typeid: params.project_id
|
|
|
|
|
});
|
2017-08-24 16:40:33 +08:00
|
|
|
|
});
|
2017-08-29 11:25:50 +08:00
|
|
|
|
|
|
|
|
|
|
2017-08-09 16:16:35 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
2017-08-24 10:33:17 +08:00
|
|
|
|
|
|
|
|
|
}catch(e){
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-24 16:40:33 +08:00
|
|
|
|
* 更新一个接口用例
|
2017-08-24 10:33:17 +08:00
|
|
|
|
* @interface /col/up_case
|
|
|
|
|
* @method POST
|
|
|
|
|
* @category col
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {number} id
|
|
|
|
|
* @param {String} casename
|
|
|
|
|
* @param {String} domain
|
|
|
|
|
* @param {String} path
|
|
|
|
|
* @param {String} method
|
|
|
|
|
* @param {Object} req_query
|
|
|
|
|
* @param {Object} req_headers
|
|
|
|
|
* @param {String} req_body_type
|
|
|
|
|
* @param {Array} req_body_form
|
|
|
|
|
* @param {String} req_body_other
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async upCase(ctx){
|
|
|
|
|
try{
|
|
|
|
|
let params = ctx.request.body;
|
|
|
|
|
params = yapi.commons.handleParams(params, {
|
|
|
|
|
id: 'number',
|
2017-08-28 15:19:07 +08:00
|
|
|
|
casename: 'string'
|
2017-08-24 10:33:17 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!params.id) {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '用例id不能为空');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!params.casename){
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '用例名称不能为空');
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 21:07:49 +08:00
|
|
|
|
let caseData = await this.caseModel.get(params.id);
|
|
|
|
|
let auth = await this.checkAuth(caseData.project_id, 'project', 'edit');
|
2017-08-24 14:17:19 +08:00
|
|
|
|
if (!auth) {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 10:33:17 +08:00
|
|
|
|
params.uid = this.getUid();
|
|
|
|
|
|
2017-08-28 15:19:07 +08:00
|
|
|
|
delete params.interface_id;
|
|
|
|
|
delete params.project_id;
|
|
|
|
|
delete params.col_id;
|
|
|
|
|
|
2017-08-24 11:11:38 +08:00
|
|
|
|
let result = await this.caseModel.up(params.id, params);
|
2017-08-24 16:40:33 +08:00
|
|
|
|
let username = this.getUsername();
|
2017-08-24 19:27:48 +08:00
|
|
|
|
this.colModel.get(caseData.col_id).then((col)=>{
|
|
|
|
|
yapi.commons.saveLog({
|
|
|
|
|
content: `用户 "${username}" 在接口集 "${col.name}" 更新了接口用例 "${params.casename}"`,
|
|
|
|
|
type: 'project',
|
|
|
|
|
uid: this.getUid(),
|
|
|
|
|
username: username,
|
|
|
|
|
typeid: caseData.project_id
|
|
|
|
|
});
|
2017-08-24 16:40:33 +08:00
|
|
|
|
});
|
2017-08-29 11:25:50 +08:00
|
|
|
|
|
|
|
|
|
|
2017-08-24 10:33:17 +08:00
|
|
|
|
|
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
2017-08-09 16:16:35 +08:00
|
|
|
|
|
|
|
|
|
}catch(e){
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取一个接口用例详情
|
|
|
|
|
* @interface /col/case
|
|
|
|
|
* @method GET
|
|
|
|
|
* @category col
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {String} caseid
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example
|
2017-08-09 16:16:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async getCase(ctx){
|
|
|
|
|
try{
|
|
|
|
|
let id = ctx.query.caseid;
|
|
|
|
|
let result = await this.caseModel.get(id);
|
2017-08-28 15:19:07 +08:00
|
|
|
|
if(!result){
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '不存在的case');
|
|
|
|
|
}
|
|
|
|
|
result = result.toObject();
|
|
|
|
|
let data = await this.interfaceModel.get(result.interface_id);
|
2017-09-08 11:12:33 +08:00
|
|
|
|
if(!data){
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '找不到对应的接口,请联系管理员')
|
|
|
|
|
}
|
2017-08-30 14:43:44 +08:00
|
|
|
|
data = data.toObject();
|
2017-09-08 11:12:33 +08:00
|
|
|
|
let projectData = await this.projectModel.getBaseInfo(data.project_id);
|
2017-08-28 20:39:32 +08:00
|
|
|
|
result.path = projectData.basepath + data.path;
|
2017-08-28 15:19:07 +08:00
|
|
|
|
result.method = data.method;
|
|
|
|
|
result.req_body_type = data.req_body_type;
|
|
|
|
|
result.req_headers = data.req_headers;
|
2017-08-31 15:18:37 +08:00
|
|
|
|
result.res_body = data.res_body;
|
|
|
|
|
result.res_body_type = data.res_body_type;
|
2017-08-30 14:43:44 +08:00
|
|
|
|
|
2017-08-28 15:19:07 +08:00
|
|
|
|
result.req_body_form = this.handleParamsValue(data.req_body_form, result.req_body_form)
|
|
|
|
|
result.req_query = this.handleParamsValue(data.req_query, result.req_query)
|
|
|
|
|
result.req_params = this.handleParamsValue(data.req_params, result.req_params)
|
2017-08-31 15:18:37 +08:00
|
|
|
|
|
2017-08-09 16:16:35 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
}catch(e){
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 400, e.message)
|
|
|
|
|
}
|
2017-08-08 19:18:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:19:07 +08:00
|
|
|
|
handleParamsValue(params, val){
|
|
|
|
|
let value = {};
|
|
|
|
|
if(params.length === 0 || val.length === 0){
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
val.forEach((item, index)=>{
|
|
|
|
|
value[item.name] = item;
|
|
|
|
|
})
|
|
|
|
|
params.forEach((item, index)=>{
|
2017-09-08 10:02:34 +08:00
|
|
|
|
if(!value[item.name] || typeof value[item.name] !== 'object') return null;
|
2017-08-28 15:19:07 +08:00
|
|
|
|
params[index].value = value[item.name].value;
|
|
|
|
|
})
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 16:16:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* 更新一个接口集name或描述
|
|
|
|
|
* @interface /col/up_col
|
2017-08-09 20:11:39 +08:00
|
|
|
|
* @method POST
|
2017-08-09 16:16:35 +08:00
|
|
|
|
* @category col
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {String} name
|
|
|
|
|
* @param {String} desc
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example
|
2017-08-09 16:16:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async upCol(ctx){
|
|
|
|
|
try{
|
|
|
|
|
let params = ctx.request.body;
|
2017-08-24 14:17:19 +08:00
|
|
|
|
let id = params.col_id;
|
|
|
|
|
let colData = await this.colModel.get(id);
|
|
|
|
|
let auth = await this.checkAuth(colData.project_id, 'project', 'edit')
|
|
|
|
|
if (!auth) {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-22 16:50:15 +08:00
|
|
|
|
let result = await this.colModel.up(params.col_id, {
|
|
|
|
|
name: params.name,
|
|
|
|
|
desc: params.desc,
|
2017-08-09 16:16:35 +08:00
|
|
|
|
up_time: yapi.commons.time()
|
2017-08-24 16:40:33 +08:00
|
|
|
|
});
|
|
|
|
|
let username = this.getUsername();
|
|
|
|
|
yapi.commons.saveLog({
|
|
|
|
|
content: `用户 "${username}" 更新了接口集 "${params.name}" 的信息`,
|
|
|
|
|
type: 'project',
|
|
|
|
|
uid: this.getUid(),
|
|
|
|
|
username: username,
|
|
|
|
|
typeid: colData.project_id
|
|
|
|
|
});
|
2017-08-09 16:16:35 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result)
|
|
|
|
|
}catch(e){
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 400, e.message)
|
|
|
|
|
}
|
2017-08-08 19:18:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 20:11:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* 更新多个接口case index
|
|
|
|
|
* @interface /col/up_col_index
|
|
|
|
|
* @method POST
|
|
|
|
|
* @category col
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {Array} [id, index]
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example
|
2017-08-09 20:11:39 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async upCaseIndex(ctx){
|
|
|
|
|
try{
|
|
|
|
|
let params = ctx.request.body;
|
|
|
|
|
if(!params || !Array.isArray(params)){
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 400, "请求参数必须是数组")
|
|
|
|
|
}
|
2017-08-24 19:27:48 +08:00
|
|
|
|
// let caseName = "";
|
2017-08-09 20:11:39 +08:00
|
|
|
|
params.forEach((item) => {
|
|
|
|
|
if(item.id && item.index){
|
|
|
|
|
this.caseModel.upCaseIndex(item.id, item.index).then((res) => {}, (err) => {
|
|
|
|
|
yapi.commons.log(err.message, 'error')
|
|
|
|
|
})
|
|
|
|
|
}
|
2017-08-15 12:08:59 +08:00
|
|
|
|
|
2017-08-24 19:27:48 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// let username = this.getUsername();
|
|
|
|
|
// yapi.commons.saveLog({
|
|
|
|
|
// content: `用户 "${username}" 更新了接口集 "${params.col_name}"`,
|
|
|
|
|
// type: 'project',
|
|
|
|
|
// uid: this.getUid(),
|
|
|
|
|
// username: username,
|
|
|
|
|
// typeid: params.project_id
|
|
|
|
|
// });
|
2017-08-15 12:08:59 +08:00
|
|
|
|
|
2017-08-29 11:25:50 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn('成功!')
|
2017-08-09 20:11:39 +08:00
|
|
|
|
}catch(e){
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 400, e.message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 16:16:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* 删除一个接口集
|
2017-08-09 20:11:39 +08:00
|
|
|
|
* @interface /col/del_col
|
2017-08-09 16:16:35 +08:00
|
|
|
|
* @method GET
|
|
|
|
|
* @category col
|
|
|
|
|
* @foldnumber 10
|
2017-08-15 12:08:59 +08:00
|
|
|
|
* @param {String}
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example
|
2017-08-09 16:16:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async delCol(ctx){
|
2017-08-09 20:11:39 +08:00
|
|
|
|
try{
|
2017-08-22 18:05:20 +08:00
|
|
|
|
let id = ctx.query.col_id;
|
2017-08-09 20:11:39 +08:00
|
|
|
|
let colData = await this.colModel.get(id);
|
|
|
|
|
if(!colData){
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 400, "不存在的id")
|
|
|
|
|
}
|
2017-08-09 16:16:35 +08:00
|
|
|
|
|
2017-08-09 20:11:39 +08:00
|
|
|
|
if(colData.uid !== this.getUid()){
|
|
|
|
|
let auth = await this.checkAuth(colData.project_id, 'project', 'danger')
|
|
|
|
|
if(!auth){
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-22 18:05:20 +08:00
|
|
|
|
let result = await this.colModel.del(id);
|
2017-08-24 19:27:48 +08:00
|
|
|
|
await this.caseModel.delByCol(id);
|
|
|
|
|
let username = this.getUsername();
|
|
|
|
|
yapi.commons.saveLog({
|
|
|
|
|
content: `用户 "${username}" 删除了接口集 "${colData.name}" 及其下面的接口`,
|
|
|
|
|
type: 'project',
|
|
|
|
|
uid: this.getUid(),
|
|
|
|
|
username: username,
|
|
|
|
|
typeid: colData.project_id
|
|
|
|
|
});
|
2017-08-09 20:11:39 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
}catch(e){
|
|
|
|
|
yapi.commons.resReturn(null, 400, e.message)
|
|
|
|
|
}
|
2017-08-09 16:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-15 12:08:59 +08:00
|
|
|
|
*
|
|
|
|
|
* @param {*} ctx
|
2017-08-09 16:16:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async delCase(ctx){
|
2017-08-09 20:11:39 +08:00
|
|
|
|
try{
|
2017-08-22 18:05:20 +08:00
|
|
|
|
let caseid = ctx.query.caseid;
|
2017-08-09 20:11:39 +08:00
|
|
|
|
let caseData = await this.caseModel.get(caseid);
|
|
|
|
|
if(!caseData){
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 400, "不存在的caseid")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(caseData.uid !== this.getUid()){
|
|
|
|
|
let auth = await this.checkAuth(caseData.project_id, 'project', 'danger')
|
|
|
|
|
if(!auth){
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-09 16:16:35 +08:00
|
|
|
|
|
2017-08-09 20:11:39 +08:00
|
|
|
|
let result = await this.caseModel.del(caseid);
|
2017-08-24 19:27:48 +08:00
|
|
|
|
|
|
|
|
|
let username = this.getUsername();
|
|
|
|
|
this.colModel.get(caseData.col_id).then((col)=>{
|
|
|
|
|
yapi.commons.saveLog({
|
|
|
|
|
content: `用户 "${username}" 删除了接口集 "${col.name}" 下的接口 "${caseData.casename}"`,
|
|
|
|
|
type: 'project',
|
|
|
|
|
uid: this.getUid(),
|
|
|
|
|
username: username,
|
|
|
|
|
typeid: caseData.project_id
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-08-29 11:25:50 +08:00
|
|
|
|
|
2017-08-24 19:27:48 +08:00
|
|
|
|
|
2017-08-09 20:11:39 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}catch(e){
|
|
|
|
|
yapi.commons.resReturn(null, 400, e.message)
|
|
|
|
|
}
|
2017-08-09 16:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2017-08-08 19:18:34 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
module.exports = interfaceColController
|