yapi/exts/yapi-plugin-advanced-mock/controller.js

157 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-09-17 13:36:51 +08:00
const baseController = require('controllers/base.js');
2017-10-18 15:59:23 +08:00
const advModel = require('./advMockModel.js');
2017-09-17 13:36:51 +08:00
const yapi = require('yapi.js');
2017-10-18 15:59:23 +08:00
const caseModel = require('./caseModel.js');
2017-10-24 15:32:26 +08:00
const userModel = require('../../server/models/user.js');
2017-10-19 14:07:23 +08:00
const config = require('./index.js');
2017-09-17 13:36:51 +08:00
class advMockController extends baseController{
constructor(ctx){
super(ctx);
this.Model = yapi.getInst(advModel);
2017-10-18 15:59:23 +08:00
this.caseModel = yapi.getInst(caseModel);
2017-10-24 15:32:26 +08:00
this.userModel = yapi.getInst(userModel);
2017-09-17 13:36:51 +08:00
}
async getMock(ctx){
let id = ctx.query.interface_id;
let mockData = await this.Model.get(id);
if(!mockData){
return ctx.body = yapi.commons.resReturn(null, 408, 'mock脚本不存在');
}
return ctx.body = yapi.commons.resReturn(mockData);
}
async upMock(ctx){
let params = ctx.request.body;
try{
if(!params.interface_id){
return ctx.body =yapi.commons.resReturn(null, 408, '缺少interface_id');
2017-09-17 13:36:51 +08:00
}
if(!params.project_id){
return ctx.body =yapi.commons.resReturn(null, 408, '缺少project_id');
2017-09-17 13:36:51 +08:00
}
2017-09-17 13:36:51 +08:00
let data = {
interface_id: params.interface_id,
mock_script: params.mock_script || '',
2017-09-17 13:36:51 +08:00
project_id: params.project_id,
uid: this.getUid(),
enable: params.enable === true ? true : false
}
let result;
let mockData = await this.Model.get(data.interface_id);
if(mockData){
result = await this.Model.up(data);
}else{
result = await this.Model.save(data);
}
return ctx.body = yapi.commons.resReturn(result);
}catch(e){
return ctx.body = yapi.commons.resReturn(null, 400, e.message);
}
}
2017-10-18 15:59:23 +08:00
async list(ctx){
let id = ctx.query.interface_id;
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '缺少 interface_id');
}
let result = await this.caseModel.list(id);
2017-10-24 15:32:26 +08:00
for(let i = 0, len = result.length; i < len; i++) {
let userinfo = await this.userModel.findById(result[i].uid);
result[i] = result[i].toObject();
result[i].username = userinfo.username;
}
2017-10-18 15:59:23 +08:00
ctx.body = yapi.commons.resReturn(result);
}
async getCase(ctx){
let id = ctx.query.id;
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '缺少 id');
}
let result = await this.caseModel.get({
_id: id
})
2017-10-24 15:32:26 +08:00
2017-10-18 15:59:23 +08:00
ctx.body = yapi.commons.resReturn(result);
}
async saveCase(ctx){
let params = ctx.request.body;
if(!params.interface_id){
return ctx.body =yapi.commons.resReturn(null, 408, '缺少interface_id');
}
if(!params.project_id){
return ctx.body =yapi.commons.resReturn(null, 408, '缺少project_id');
}
let data = {
interface_id: params.interface_id,
project_id: params.project_id,
ip_enable: params.ip_enable,
name: params.name,
2017-10-19 10:31:57 +08:00
params: params.params || [],
2017-10-18 15:59:23 +08:00
uid: this.getUid(),
code: params.code || 200,
2017-10-19 14:07:23 +08:00
delay: params.delay || 0,
2017-10-19 10:31:57 +08:00
headers: params.headers || [],
up_time: yapi.commons.time(),
res_body: params.res_body || '',
ip: params.ip
2017-10-18 15:59:23 +08:00
}
2017-10-19 14:07:23 +08:00
data.code = isNaN(data.code) ? 200 : +data.code;
data.delay = isNaN(data.delay) ? 0 : +data.delay;
if(config.httpCodes.indexOf(data.code) === -1){
return ctx.body =yapi.commons.resReturn(null, 408, '非法的 httpCode');
}
let findRepeat, findRepeatParams;
findRepeatParams = {
project_id: data.project_id,
interface_id: data.interface_id,
2017-10-23 09:19:38 +08:00
ip_enable: data.ip_enable
}
if(data.params && typeof data.params === 'object' && Object.keys(data.params).length >0){
for(let i in data.params){
findRepeatParams['params.' + i] = data.params[i];
}
}else{
findRepeatParams.params = null;
}
if(data.ip_enable){
findRepeatParams.ip = data.ip;
}
findRepeat = await this.caseModel.get(findRepeatParams);
2017-10-24 20:39:23 +08:00
if(findRepeat && findRepeat._id !== params.id){
return ctx.body = yapi.commons.resReturn(null,400, '已存在的期望');
}
2017-10-18 15:59:23 +08:00
let result;
2017-10-23 09:19:38 +08:00
if(params.id && !isNaN(params.id)){
2017-10-18 15:59:23 +08:00
data.id = +params.id;
result = await this.caseModel.up(data);
}else{
result = await this.caseModel.save(data);
}
return ctx.body = yapi.commons.resReturn(result);
}
async delCase(ctx){
let id = ctx.request.body.id;
if(!id){
return ctx.body =yapi.commons.resReturn(null, 408, '缺少 id');
}
2017-10-24 20:39:23 +08:00
let result = await this.caseModel.del(id);
return ctx.body = yapi.commons.resReturn(result);
2017-10-18 15:59:23 +08:00
}
2017-09-17 13:36:51 +08:00
}
module.exports = advMockController;