yapi/server/controllers/log.js

77 lines
2.8 KiB
JavaScript
Raw Normal View History

const logModel = require('../models/log.js');
const yapi = require('../yapi.js');
const baseController = require('./base.js');
const groupModel = require('../models/group');
const projectModel = require('../models/project');
2017-07-20 20:30:24 +08:00
class logController extends baseController {
constructor(ctx) {
super(ctx);
this.Model = yapi.getInst(logModel);
this.groupModel = yapi.getInst(groupModel);
this.projectModel = yapi.getInst(projectModel);
2017-07-20 20:30:24 +08:00
}
/**
2017-08-10 15:47:33 +08:00
* 获取动态列表
* @interface /log/list
2017-07-20 20:30:24 +08:00
* @method GET
2017-08-10 15:47:33 +08:00
* @category log
2017-07-20 20:30:24 +08:00
* @foldnumber 10
2017-08-11 17:49:47 +08:00
* @param {Number} typeid 动态类型id 不能为空
2017-07-20 20:30:24 +08:00
* @param {Number} [page] 分页页码
* @param {Number} [limit] 分页大小
* @returns {Object}
2017-08-10 15:47:33 +08:00
* @example /log/list
2017-07-20 20:30:24 +08:00
*/
2017-08-09 16:08:37 +08:00
2017-07-20 20:30:24 +08:00
async list(ctx) {
2017-08-09 18:00:45 +08:00
let typeid = ctx.request.query.typeid,
2017-07-20 20:30:24 +08:00
page = ctx.request.query.page || 1,
2017-08-11 17:49:47 +08:00
limit = ctx.request.query.limit || 10,
type = ctx.request.query.type;
2017-08-09 18:00:45 +08:00
if (!typeid) {
2017-08-10 12:13:32 +08:00
return ctx.body = yapi.commons.resReturn(null, 400, 'typeid不能为空');
2017-07-20 20:30:24 +08:00
}
2017-08-11 17:49:47 +08:00
if(!type) {
return ctx.body = yapi.commons.resReturn(null, 400, 'type不能为空');
}
2017-07-20 20:30:24 +08:00
try {
if(type === "group"){
let projectList = await this.projectModel.list(typeid);
2017-10-10 11:34:33 +08:00
let projectIds = [], projectDatas = {};
for(let i in projectList){
2017-10-10 11:34:33 +08:00
projectDatas[projectList[i]._id] = projectList[i];
projectIds[i] = projectList[i]._id;
}
2017-10-10 11:34:33 +08:00
let projectLogList = await this.Model.listWithPagingByGroup(typeid,projectIds,page,limit);
projectLogList.forEach((item, index)=>{
item = item.toObject();
if(item.type === 'project'){
2017-10-16 16:52:43 +08:00
item.content = `在 <a href="/project/${item.typeid}">${projectDatas[item.typeid].name}</a> 项目: ` + item.content;
2017-10-10 11:34:33 +08:00
}
projectLogList[index] = item;
})
let total = await this.Model.listCountByGroup(typeid,projectIds);
ctx.body = yapi.commons.resReturn({
list: projectLogList,
total: Math.ceil(total / limit)
});
}else if(type === "project"){
let result = await this.Model.listWithPaging(typeid,type, page, limit);
let count = await this.Model.listCount(typeid,type);
ctx.body = yapi.commons.resReturn({
total: Math.ceil(count / limit),
list: result
});
}
2017-07-27 19:49:26 +08:00
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 402, err.message);
2017-07-20 20:30:24 +08:00
}
}
}
module.exports = logController;