mock平台

log.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const logModel = require('../models/log.js');
  2. const yapi = require('../yapi.js');
  3. const baseController = require('./base.js');
  4. const groupModel = require('../models/group');
  5. const projectModel = require('../models/project');
  6. const interfaceModel = require('../models/interface');
  7. class logController extends baseController {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.Model = yapi.getInst(logModel);
  11. this.groupModel = yapi.getInst(groupModel);
  12. this.projectModel = yapi.getInst(projectModel);
  13. this.interfaceModel = yapi.getInst(interfaceModel);
  14. this.schemaMap = {
  15. listByUpdate: {
  16. '*type': 'string',
  17. '*typeid': 'number',
  18. apis: [
  19. {
  20. method: 'string',
  21. path: 'string'
  22. }
  23. ]
  24. }
  25. };
  26. }
  27. /**
  28. * 获取动态列表
  29. * @interface /log/list
  30. * @method GET
  31. * @category log
  32. * @foldnumber 10
  33. * @param {Number} typeid 动态类型id, 不能为空
  34. * @param {Number} [page] 分页页码
  35. * @param {Number} [limit] 分页大小
  36. * @returns {Object}
  37. * @example /log/list
  38. */
  39. async list(ctx) {
  40. let typeid = ctx.request.query.typeid,
  41. page = ctx.request.query.page || 1,
  42. limit = ctx.request.query.limit || 10,
  43. type = ctx.request.query.type,
  44. selectValue = ctx.request.query.selectValue;
  45. if (!typeid) {
  46. return (ctx.body = yapi.commons.resReturn(null, 400, 'typeid不能为空'));
  47. }
  48. if (!type) {
  49. return (ctx.body = yapi.commons.resReturn(null, 400, 'type不能为空'));
  50. }
  51. try {
  52. if (type === 'group') {
  53. let projectList = await this.projectModel.list(typeid);
  54. let projectIds = [],
  55. projectDatas = {};
  56. for (let i in projectList) {
  57. projectDatas[projectList[i]._id] = projectList[i];
  58. projectIds[i] = projectList[i]._id;
  59. }
  60. let projectLogList = await this.Model.listWithPagingByGroup(
  61. typeid,
  62. projectIds,
  63. page,
  64. limit
  65. );
  66. projectLogList.forEach((item, index) => {
  67. item = item.toObject();
  68. if (item.type === 'project') {
  69. item.content =
  70. `在 <a href="/project/${item.typeid}">${projectDatas[item.typeid].name}</a> 项目: ` +
  71. item.content;
  72. }
  73. projectLogList[index] = item;
  74. });
  75. let total = await this.Model.listCountByGroup(typeid, projectIds);
  76. ctx.body = yapi.commons.resReturn({
  77. list: projectLogList,
  78. total: Math.ceil(total / limit)
  79. });
  80. } else if (type === "project") {
  81. let result = await this.Model.listWithPaging(typeid, type, page, limit, selectValue);
  82. let count = await this.Model.listCount(typeid, type, selectValue);
  83. ctx.body = yapi.commons.resReturn({
  84. total: Math.ceil(count / limit),
  85. list: result
  86. });
  87. }
  88. } catch (err) {
  89. ctx.body = yapi.commons.resReturn(null, 402, err.message);
  90. }
  91. }
  92. /**
  93. * 获取特定cat_id下最新修改的动态信息
  94. * @interface /log/list_by_update
  95. * @method post
  96. * @category log
  97. * @foldnumber 10
  98. * @param {Number} typeid 动态类型id, 不能为空
  99. * @returns {Object}
  100. * @example /log/list
  101. */
  102. async listByUpdate(ctx) {
  103. let params = ctx.params;
  104. try {
  105. let { typeid, type, apis } = params;
  106. let list = [];
  107. let projectDatas = await this.projectModel.getBaseInfo(typeid, 'basepath');
  108. let basePath = projectDatas.toObject().basepath;
  109. for (let i = 0; i < apis.length; i++) {
  110. let api = apis[i];
  111. if (basePath) {
  112. api.path = api.path.indexOf(basePath) === 0 ? api.path.substr(basePath.length) : api.path;
  113. }
  114. let interfaceIdList = await this.interfaceModel.getByPath(
  115. typeid,
  116. api.path,
  117. api.method,
  118. '_id'
  119. );
  120. for (let j = 0; j < interfaceIdList.length; j++) {
  121. let interfaceId = interfaceIdList[j];
  122. let id = interfaceId.id;
  123. let result = await this.Model.listWithCatid(typeid, type, id);
  124. list = list.concat(result);
  125. }
  126. }
  127. // let result = await this.Model.listWithCatid(typeid, type, catId);
  128. ctx.body = yapi.commons.resReturn(list);
  129. } catch (err) {
  130. ctx.body = yapi.commons.resReturn(null, 402, err.message);
  131. }
  132. }
  133. }
  134. module.exports = logController;