standard_lib_controller.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. /**
  3. * 标准库控制器基类
  4. *
  5. * @author Mai
  6. * @date 2018/3/13
  7. * @version
  8. */
  9. module.exports = app => {
  10. class StandardLibController extends app.BaseController {
  11. async getData(ctx) {
  12. const responseData = {
  13. err: 0,
  14. msg: '',
  15. data: [],
  16. };
  17. try {
  18. const data = JSON.parse(ctx.request.body.data);
  19. if (isNaN(data.list_id) || data.list_id <= 0) {
  20. throw '参数错误';
  21. }
  22. switch (data.stdType) {
  23. case 'gcl':
  24. responseData.data = await this.ctx.service.stdGcl.getData(data.list_id);
  25. break;
  26. case 'xmj':
  27. responseData.data = await this.ctx.service.stdXmj.getData(data.list_id);
  28. break;
  29. case 'glj':
  30. responseData.data = await this.ctx.service.gljLib.getData(data.list_id);
  31. break;
  32. case 'reference':
  33. responseData.data = await this.ctx.service.fileReference.getData(data.list_id);
  34. break;
  35. default:
  36. throw '查询的标准清单不存在';
  37. }
  38. } catch (error) {
  39. this.log(error);
  40. responseData.err = 1;
  41. responseData.msg = error;
  42. }
  43. ctx.body = responseData;
  44. }
  45. /**
  46. * 根据id获取子项
  47. *
  48. * @param {Object} ctx - egg全局变量
  49. * @return {void}
  50. */
  51. async getChildren(ctx) {
  52. const responseData = {
  53. err: 0,
  54. msg: '',
  55. data: [],
  56. };
  57. try {
  58. const data = JSON.parse(ctx.request.body.data);
  59. if (isNaN(data.pid) || data.pid <= 0 || isNaN(data.list_id) || data.list_id <= 0) {
  60. throw '参数错误';
  61. }
  62. const condition = { pid: data.pid, list_id: data.list_id };
  63. switch (data.stdType) {
  64. case 'gcl':
  65. responseData.data = await this.ctx.service.stdGcl.getAllDataByCondition({ where: condition });
  66. break;
  67. case 'xmj':
  68. responseData.data = await this.ctx.service.stdXmj.getAllDataByCondition({ where: condition });
  69. break;
  70. case 'glj':
  71. responseData.data = await this.ctx.service.gljLib.getAllDataByCondition({ where: condition });
  72. break;
  73. case 'reference':
  74. responseData.data = await this.ctx.service.fileReference.getAllDataByCondition({ where: condition });
  75. break;
  76. default:
  77. throw '查询的标准清单不存在';
  78. }
  79. } catch (error) {
  80. responseData.err = 1;
  81. responseData.msg = error;
  82. }
  83. ctx.body = responseData;
  84. }
  85. }
  86. return StandardLibController;
  87. };