fix: 用户从用户列表中删除后访问高级mock报错

This commit is contained in:
gaoxiaolin.gao 2018-06-26 17:06:26 +08:00
parent 6274cd40ac
commit f986eb4010
3 changed files with 177 additions and 156 deletions

View File

@ -9,6 +9,7 @@ import { formatTime } from 'client/common.js';
import constants from 'client/constants/variable.js'
import CaseDesModal from './CaseDesModal';
import { json5_parse } from '../../../client/common';
import _ from 'underscore';
@connect(
state => {
@ -125,6 +126,7 @@ export default class MockCol extends Component {
let ipObj = {};
let userFilters = [];
let userObj = {};
_.isArray(data) &&
data.forEach(item => {
ipObj[item.ip_enable ? item.ip : ''] = '';
userObj[item.username] = '';

View File

@ -5,38 +5,37 @@ const caseModel = require('./caseModel.js');
const userModel = require('models/user.js');
const config = require('./index.js');
class advMockController extends baseController{
constructor(ctx){
class advMockController extends baseController {
constructor(ctx) {
super(ctx);
this.Model = yapi.getInst(advModel);
this.caseModel = yapi.getInst(caseModel);
this.userModel = yapi.getInst(userModel);
}
async getMock(ctx){
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脚本不存在');
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);
return (ctx.body = yapi.commons.resReturn(mockData));
}
async upMock(ctx){
async upMock(ctx) {
let params = ctx.request.body;
try{
let auth = await this.checkAuth(params.project_id, 'project', 'edit')
try {
let auth = await this.checkAuth(params.project_id, 'project', 'edit');
if (!auth) {
return ctx.body = yapi.commons.resReturn(null, 40033, '没有权限');
return (ctx.body = yapi.commons.resReturn(null, 40033, '没有权限'));
}
if(!params.interface_id){
return ctx.body =yapi.commons.resReturn(null, 408, '缺少interface_id');
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');
if (!params.project_id) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少project_id'));
}
let data = {
@ -45,64 +44,69 @@ class advMockController extends baseController{
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){
let mockData = await this.Model.get(data.interface_id);
if (mockData) {
result = await this.Model.up(data);
}else{
} 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);
return (ctx.body = yapi.commons.resReturn(result));
} catch (e) {
return (ctx.body = yapi.commons.resReturn(null, 400, e.message));
}
}
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);
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;
}
async list(ctx) {
try {
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);
for (let i = 0, len = result.length; i < len; i++) {
let userinfo = await this.userModel.findById(result[i].uid);
result[i] = result[i].toObject();
// if (userinfo) {
result[i].username = userinfo.username;
// }
}
ctx.body = yapi.commons.resReturn(result);
ctx.body = yapi.commons.resReturn(result);
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 400, err.message)
}
}
async getCase(ctx){
async getCase(ctx) {
let id = ctx.query.id;
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '缺少 id');
if (!id) {
return (ctx.body = yapi.commons.resReturn(null, 400, '缺少 id'));
}
let result = await this.caseModel.get({
_id: id
})
});
ctx.body = yapi.commons.resReturn(result);
}
async saveCase(ctx){
async saveCase(ctx) {
let params = ctx.request.body;
if(!params.interface_id){
return ctx.body =yapi.commons.resReturn(null, 408, '缺少interface_id');
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');
if (!params.project_id) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少project_id'));
}
if(!params.res_body){
return ctx.body =yapi.commons.resReturn(null, 408, '请输入 Response Body');
if (!params.res_body) {
return (ctx.body = yapi.commons.resReturn(null, 408, '请输入 Response Body'));
}
let data = {
interface_id: params.interface_id,
interface_id: params.interface_id,
project_id: params.project_id,
ip_enable: params.ip_enable,
name: params.name,
@ -114,12 +118,12 @@ class advMockController extends baseController{
up_time: yapi.commons.time(),
res_body: params.res_body,
ip: params.ip
}
};
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');
if (config.httpCodes.indexOf(data.code) === -1) {
return (ctx.body = yapi.commons.resReturn(null, 408, '非法的 httpCode'));
}
let findRepeat, findRepeatParams;
@ -127,45 +131,43 @@ class advMockController extends baseController{
project_id: data.project_id,
interface_id: data.interface_id,
ip_enable: data.ip_enable
}
};
if(data.params && typeof data.params === 'object' && Object.keys(data.params).length >0){
for(let i in data.params){
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{
} else {
findRepeatParams.params = null;
}
if(data.ip_enable){
if (data.ip_enable) {
findRepeatParams.ip = data.ip;
}
findRepeat = await this.caseModel.get(findRepeatParams);
if(findRepeat && findRepeat._id !== params.id){
return ctx.body = yapi.commons.resReturn(null,400, '已存在的期望');
if (findRepeat && findRepeat._id !== params.id) {
return (ctx.body = yapi.commons.resReturn(null, 400, '已存在的期望'));
}
let result;
if(params.id && !isNaN(params.id)){
if (params.id && !isNaN(params.id)) {
data.id = +params.id;
result = await this.caseModel.up(data);
}else{
} else {
result = await this.caseModel.save(data);
}
return ctx.body = yapi.commons.resReturn(result);
return (ctx.body = yapi.commons.resReturn(result));
}
async delCase(ctx){
async delCase(ctx) {
let id = ctx.request.body.id;
if(!id){
return ctx.body =yapi.commons.resReturn(null, 408, '缺少 id');
if (!id) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少 id'));
}
let result = await this.caseModel.del(id);
return ctx.body = yapi.commons.resReturn(result);
return (ctx.body = yapi.commons.resReturn(result));
}
}
module.exports = advMockController;
module.exports = advMockController;

View File

@ -6,12 +6,12 @@ const statisMockModel = require('./statisMockModel.js');
const groupModel = require('models/group.js');
const projectModel = require('models/project.js');
const interfaceModel = require('models/interface.js');
const interfaceCaseModel = require('models/interfaceCase.js')
const interfaceCaseModel = require('models/interfaceCase.js');
const yapi = require('yapi.js');
const config = require('./index.js');
const commons = require('./util.js');
const os = require("os");
const os = require('os');
let cpu = require('cpu-load');
class statisMockController extends baseController {
@ -25,20 +25,29 @@ class statisMockController extends baseController {
}
/**
* 获取所有统计总数
* @interface statismock/count
* @method get
* @category statistics
* @foldnumber 10
* @returns {Object}
*/
* 获取所有统计总数
* @interface statismock/count
* @method get
* @category statistics
* @foldnumber 10
* @returns {Object}
*/
async getStatisCount(ctx) {
let groupCount = await this.groupModel.getGroupListCount();
let projectCount = await this.projectModel.getProjectListCount();
let interfaceCount = await this.interfaceModel.getInterfaceListCount();
let interfaceCaseCount = await this.interfaceCaseModel.getInterfaceCaseListCount();
try {
let groupCount = await this.groupModel.getGroupListCount();
let projectCount = await this.projectModel.getProjectListCount();
let interfaceCount = await this.interfaceModel.getInterfaceListCount();
let interfaceCaseCount = await this.interfaceCaseModel.getInterfaceCaseListCount();
return ctx.body = yapi.commons.resReturn({ groupCount, projectCount, interfaceCount, interfaceCaseCount });
return (ctx.body = yapi.commons.resReturn({
groupCount,
projectCount,
interfaceCount,
interfaceCaseCount
}));
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 400, err.message);
}
}
/**
@ -50,16 +59,20 @@ class statisMockController extends baseController {
* @returns {Object}
*/
async getMockDateList(ctx) {
let mockCount = await this.Model.getTotalCount();
let mockDateList = [];
try {
let mockCount = await this.Model.getTotalCount();
let mockDateList = [];
if (!this.getRole() === 'admin') {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
if (!this.getRole() === 'admin') {
return (ctx.body = yapi.commons.resReturn(null, 405, '没有权限'));
}
// 默认时间是30 天为一周期
let dateInterval = commons.getDateRange();
mockDateList = await this.Model.getDayCount(dateInterval);
return (ctx.body = yapi.commons.resReturn({ mockCount, mockDateList }));
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 400, err.message);
}
// 默认时间是30 天为一周期
let dateInterval = commons.getDateRange();
mockDateList = await this.Model.getDayCount(dateInterval);
return ctx.body = yapi.commons.resReturn({ mockCount, mockDateList });
}
/**
@ -71,89 +84,93 @@ class statisMockController extends baseController {
* @returns {Object}
*/
async getSystemStatus(ctx) {
let mail = '';
if (yapi.WEBCONFIG.mail && yapi.WEBCONFIG.mail.enable) {
mail = await this.checkEmail();
// return ctx.body = yapi.commons.resReturn(result);
} else {
mail = '未配置'
try {
let mail = '';
if (yapi.WEBCONFIG.mail && yapi.WEBCONFIG.mail.enable) {
mail = await this.checkEmail();
// return ctx.body = yapi.commons.resReturn(result);
} else {
mail = '未配置';
}
let load = (await this.cupLoad()) * 100;
let systemName = os.platform();
let totalmem = commons.transformBytesToGB(os.totalmem());
let freemem = commons.transformBytesToGB(os.freemem());
let uptime = commons.transformSecondsToDay(os.uptime());
let data = {
mail,
systemName,
totalmem,
freemem,
uptime,
load: load.toFixed(2)
};
return (ctx.body = yapi.commons.resReturn(data));
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 400, err.message);
}
let load = await this.cupLoad()*100;
let systemName = os.platform();
let totalmem = commons.transformBytesToGB(os.totalmem());
let freemem = commons.transformBytesToGB(os.freemem());
let uptime = commons.transformSecondsToDay(os.uptime());
let data = {
mail,
systemName,
totalmem,
freemem,
uptime,
load: load.toFixed(2)
}
return ctx.body = yapi.commons.resReturn(data);
}
checkEmail() {
return new Promise((resolve, reject) => {
let result = {}
yapi.mail.verify((error) => {
let result = {};
yapi.mail.verify(error => {
if (error) {
result = '不可用';
resolve(result)
resolve(result);
} else {
result = '可用';
resolve(result)
resolve(result);
}
})
})
});
});
}
async groupDataStatis(ctx){
let groupData = await this.groupModel.list();
let result = []
for(let i=0; i< groupData.length; i++){
let group = groupData[i];
let groupId = group._id;
const data = {
name: group.group_name,
interface: 0,
mock: 0,
project: 0
}
result.push(data)
async groupDataStatis(ctx) {
try {
let groupData = await this.groupModel.list();
let result = [];
for (let i = 0; i < groupData.length; i++) {
let group = groupData[i];
let groupId = group._id;
const data = {
name: group.group_name,
interface: 0,
mock: 0,
project: 0
};
result.push(data);
let projectCount = await this.projectModel.listCount(groupId);
let projectData = await this.projectModel.list(groupId);
let interfaceCount = 0;
for(let j=0; j< projectData.length; j++){
let project = projectData[j]
interfaceCount += await this.interfaceModel.listCount({
project_id: project._id
})
let projectCount = await this.projectModel.listCount(groupId);
let projectData = await this.projectModel.list(groupId);
let interfaceCount = 0;
for (let j = 0; j < projectData.length; j++) {
let project = projectData[j];
interfaceCount += await this.interfaceModel.listCount({
project_id: project._id
});
}
let mockCount = await this.Model.countByGroupId(groupId);
data.interface = interfaceCount;
data.project = projectCount;
data.mock = mockCount;
}
let mockCount = await this.Model.countByGroupId(groupId)
data.interface = interfaceCount;
data.project = projectCount;
data.mock = mockCount;
return (ctx.body = yapi.commons.resReturn(result));
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 400, err.message);
}
return ctx.body = yapi.commons.resReturn(result)
}
cupLoad() {
return new Promise((resolve,reject)=>{
cpu(1000, function (load) {
resolve(load)
})
})
return new Promise((resolve, reject) => {
cpu(1000, function(load) {
resolve(load);
});
});
}
}
module.exports = statisMockController;
module.exports = statisMockController;