mock平台

log.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const yapi = require('../yapi.js');
  2. const baseModel = require('./base.js');
  3. var mongoose = require('mongoose');
  4. var Schema = mongoose.Schema;
  5. class logModel extends baseModel {
  6. getName() {
  7. return 'log';
  8. }
  9. getSchema() {
  10. return {
  11. uid: { type: Number, required: true },
  12. typeid: { type: Number, required: true },
  13. type: {
  14. type: String,
  15. enum: ['user', 'group', 'interface', 'project', 'other', 'interface_col'],
  16. required: true
  17. },
  18. content: { type: String, required: true },
  19. username: { type: String, required: true },
  20. add_time: Number,
  21. data: Schema.Types.Mixed //用于原始数据存储
  22. };
  23. }
  24. /**
  25. * @param {String} content log内容
  26. * @param {Enum} type log类型, ['user', 'group', 'interface', 'project', 'other']
  27. * @param {Number} uid 用户id
  28. * @param {String} username 用户名
  29. * @param {Number} typeid 类型id
  30. * @param {Number} add_time 时间
  31. */
  32. save(data) {
  33. let saveData = {
  34. content: data.content,
  35. type: data.type,
  36. uid: data.uid,
  37. username: data.username,
  38. typeid: data.typeid,
  39. add_time: yapi.commons.time(),
  40. data: data.data
  41. };
  42. let log = new this.model(saveData);
  43. return log.save();
  44. }
  45. del(id) {
  46. return this.model.remove({
  47. _id: id
  48. });
  49. }
  50. list(typeid, type) {
  51. return this.model
  52. .find({
  53. typeid: typeid,
  54. type: type
  55. })
  56. .exec();
  57. }
  58. listWithPaging(typeid, type, page, limit, selectValue) {
  59. page = parseInt(page);
  60. limit = parseInt(limit);
  61. const params = {
  62. type: type,
  63. typeid: typeid
  64. };
  65. if (selectValue === 'wiki') {
  66. params['data.type'] = selectValue;
  67. }
  68. if (selectValue && !isNaN(selectValue)) {
  69. params['data.interface_id'] = +selectValue;
  70. }
  71. return this.model
  72. .find(params)
  73. .sort({ add_time: -1 })
  74. .skip((page - 1) * limit)
  75. .limit(limit)
  76. .exec();
  77. }
  78. listWithPagingByGroup(typeid, pidList, page, limit) {
  79. page = parseInt(page);
  80. limit = parseInt(limit);
  81. return this.model
  82. .find({
  83. $or: [
  84. {
  85. type: 'project',
  86. typeid: { $in: pidList }
  87. },
  88. {
  89. type: 'group',
  90. typeid: typeid
  91. }
  92. ]
  93. })
  94. .sort({ add_time: -1 })
  95. .skip((page - 1) * limit)
  96. .limit(limit)
  97. .exec();
  98. }
  99. listCountByGroup(typeid, pidList) {
  100. return this.model.countDocuments({
  101. $or: [
  102. {
  103. type: 'project',
  104. typeid: { $in: pidList }
  105. },
  106. {
  107. type: 'group',
  108. typeid: typeid
  109. }
  110. ]
  111. });
  112. }
  113. listCount(typeid, type, selectValue) {
  114. const params = {
  115. type: type,
  116. typeid: typeid
  117. };
  118. if (selectValue === 'wiki') {
  119. params['data.type'] = selectValue;
  120. }
  121. if (selectValue && !isNaN(selectValue)) {
  122. params['data.interface_id'] = +selectValue;
  123. }
  124. return this.model.countDocuments(params);
  125. }
  126. listWithCatid(typeid, type, interfaceId) {
  127. const params = {
  128. type: type,
  129. typeid: typeid
  130. };
  131. if (interfaceId && !isNaN(interfaceId)) {
  132. params['data.interface_id'] = +interfaceId;
  133. }
  134. return this.model
  135. .find(params)
  136. .sort({ add_time: -1 })
  137. .limit(1)
  138. .select('uid content type username typeid add_time')
  139. .exec();
  140. }
  141. }
  142. module.exports = logModel;