mock平台

syncController.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const baseController = require('controllers/base.js');
  2. const yapi = require('yapi.js');
  3. const syncModel = require('../syncModel.js');
  4. const projectModel = require('models/project.js');
  5. const interfaceSyncUtils = require('../interfaceSyncUtils.js')
  6. class syncController extends baseController {
  7. constructor(ctx) {
  8. super(ctx);
  9. this.syncModel = yapi.getInst(syncModel);
  10. this.projectModel = yapi.getInst(projectModel);
  11. this.interfaceSyncUtils = yapi.getInst(interfaceSyncUtils);
  12. }
  13. /**
  14. * 保存定时任务
  15. * @param {*} ctx
  16. */
  17. async upSync(ctx) {
  18. let requestBody = ctx.request.body;
  19. let projectId = requestBody.project_id;
  20. if (!projectId) {
  21. return (ctx.body = yapi.commons.resReturn(null, 408, '缺少项目Id'));
  22. }
  23. if ((await this.checkAuth(projectId, 'project', 'edit')) !== true) {
  24. return (ctx.body = yapi.commons.resReturn(null, 405, '没有权限'));
  25. }
  26. let result;
  27. if (requestBody.id) {
  28. result = await this.syncModel.up(requestBody);
  29. } else {
  30. result = await this.syncModel.save(requestBody);
  31. }
  32. //操作定时任务
  33. if (requestBody.is_sync_open) {
  34. this.interfaceSyncUtils.addSyncJob(projectId, requestBody.sync_cron, requestBody.sync_json_url, requestBody.sync_mode, requestBody.uid);
  35. } else {
  36. this.interfaceSyncUtils.deleteSyncJob(projectId);
  37. }
  38. return (ctx.body = yapi.commons.resReturn(result));
  39. }
  40. /**
  41. * 查询定时任务
  42. * @param {*} ctx
  43. */
  44. async getSync(ctx) {
  45. let projectId = ctx.query.project_id;
  46. if (!projectId) {
  47. return (ctx.body = yapi.commons.resReturn(null, 408, '缺少项目Id'));
  48. }
  49. let result = await this.syncModel.getByProjectId(projectId);
  50. return (ctx.body = yapi.commons.resReturn(result));
  51. }
  52. }
  53. module.exports = syncController;