mock平台

controller.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. const baseController = require('controllers/base.js');
  2. const advModel = require('./advMockModel.js');
  3. const yapi = require('yapi.js');
  4. const caseModel = require('./caseModel.js');
  5. const userModel = require('models/user.js');
  6. const config = require('./index.js');
  7. class advMockController extends baseController {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.Model = yapi.getInst(advModel);
  11. this.caseModel = yapi.getInst(caseModel);
  12. this.userModel = yapi.getInst(userModel);
  13. }
  14. async getMock(ctx) {
  15. let id = ctx.query.interface_id;
  16. let mockData = await this.Model.get(id);
  17. if (!mockData) {
  18. return (ctx.body = yapi.commons.resReturn(null, 408, 'mock脚本不存在'));
  19. }
  20. return (ctx.body = yapi.commons.resReturn(mockData));
  21. }
  22. async upMock(ctx) {
  23. let params = ctx.request.body;
  24. try {
  25. let auth = await this.checkAuth(params.project_id, 'project', 'edit');
  26. if (!auth) {
  27. return (ctx.body = yapi.commons.resReturn(null, 40033, '没有权限'));
  28. }
  29. if (!params.interface_id) {
  30. return (ctx.body = yapi.commons.resReturn(null, 408, '缺少interface_id'));
  31. }
  32. if (!params.project_id) {
  33. return (ctx.body = yapi.commons.resReturn(null, 408, '缺少project_id'));
  34. }
  35. let data = {
  36. interface_id: params.interface_id,
  37. mock_script: params.mock_script || '',
  38. project_id: params.project_id,
  39. uid: this.getUid(),
  40. enable: params.enable === true ? true : false
  41. };
  42. let result;
  43. let mockData = await this.Model.get(data.interface_id);
  44. if (mockData) {
  45. result = await this.Model.up(data);
  46. } else {
  47. result = await this.Model.save(data);
  48. }
  49. return (ctx.body = yapi.commons.resReturn(result));
  50. } catch (e) {
  51. return (ctx.body = yapi.commons.resReturn(null, 400, e.message));
  52. }
  53. }
  54. async list(ctx) {
  55. try {
  56. let id = ctx.query.interface_id;
  57. if (!id) {
  58. return (ctx.body = yapi.commons.resReturn(null, 400, '缺少 interface_id'));
  59. }
  60. let result = await this.caseModel.list(id);
  61. for (let i = 0, len = result.length; i < len; i++) {
  62. let userinfo = await this.userModel.findById(result[i].uid);
  63. result[i] = result[i].toObject();
  64. // if (userinfo) {
  65. result[i].username = userinfo.username;
  66. // }
  67. }
  68. ctx.body = yapi.commons.resReturn(result);
  69. } catch (err) {
  70. ctx.body = yapi.commons.resReturn(null, 400, err.message);
  71. }
  72. }
  73. async getCase(ctx) {
  74. let id = ctx.query.id;
  75. if (!id) {
  76. return (ctx.body = yapi.commons.resReturn(null, 400, '缺少 id'));
  77. }
  78. let result = await this.caseModel.get({
  79. _id: id
  80. });
  81. ctx.body = yapi.commons.resReturn(result);
  82. }
  83. async saveCase(ctx) {
  84. let params = ctx.request.body;
  85. if (!params.interface_id) {
  86. return (ctx.body = yapi.commons.resReturn(null, 408, '缺少interface_id'));
  87. }
  88. if (!params.project_id) {
  89. return (ctx.body = yapi.commons.resReturn(null, 408, '缺少project_id'));
  90. }
  91. if (!params.res_body) {
  92. return (ctx.body = yapi.commons.resReturn(null, 408, '请输入 Response Body'));
  93. }
  94. let data = {
  95. interface_id: params.interface_id,
  96. project_id: params.project_id,
  97. ip_enable: params.ip_enable,
  98. name: params.name,
  99. params: params.params || [],
  100. uid: this.getUid(),
  101. code: params.code || 200,
  102. delay: params.delay || 0,
  103. headers: params.headers || [],
  104. up_time: yapi.commons.time(),
  105. res_body: params.res_body,
  106. ip: params.ip
  107. };
  108. data.code = isNaN(data.code) ? 200 : +data.code;
  109. data.delay = isNaN(data.delay) ? 0 : +data.delay;
  110. if (config.httpCodes.indexOf(data.code) === -1) {
  111. return (ctx.body = yapi.commons.resReturn(null, 408, '非法的 httpCode'));
  112. }
  113. let findRepeat, findRepeatParams;
  114. findRepeatParams = {
  115. project_id: data.project_id,
  116. interface_id: data.interface_id,
  117. ip_enable: data.ip_enable
  118. };
  119. if (data.params && typeof data.params === 'object' && Object.keys(data.params).length > 0) {
  120. for (let i in data.params) {
  121. findRepeatParams['params.' + i] = data.params[i];
  122. }
  123. }
  124. if (data.ip_enable) {
  125. findRepeatParams.ip = data.ip;
  126. }
  127. findRepeat = await this.caseModel.get(findRepeatParams);
  128. if (findRepeat && findRepeat._id !== params.id) {
  129. return (ctx.body = yapi.commons.resReturn(null, 400, '已存在的期望'));
  130. }
  131. let result;
  132. if (params.id && !isNaN(params.id)) {
  133. data.id = +params.id;
  134. result = await this.caseModel.up(data);
  135. } else {
  136. result = await this.caseModel.save(data);
  137. }
  138. return (ctx.body = yapi.commons.resReturn(result));
  139. }
  140. async delCase(ctx) {
  141. let id = ctx.request.body.id;
  142. if (!id) {
  143. return (ctx.body = yapi.commons.resReturn(null, 408, '缺少 id'));
  144. }
  145. let result = await this.caseModel.del(id);
  146. return (ctx.body = yapi.commons.resReturn(result));
  147. }
  148. async hideCase(ctx) {
  149. let id = ctx.request.body.id;
  150. let enable = ctx.request.body.enable;
  151. if (!id) {
  152. return (ctx.body = yapi.commons.resReturn(null, 408, '缺少 id'));
  153. }
  154. let data = {
  155. id,
  156. case_enable: enable
  157. };
  158. let result = await this.caseModel.up(data);
  159. return (ctx.body = yapi.commons.resReturn(result));
  160. }
  161. }
  162. module.exports = advMockController;