standard_lib_controller.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. default:
  32. throw '查询的标准清单不存在';
  33. }
  34. } catch (error) {
  35. this.log(error);
  36. responseData.err = 1;
  37. responseData.msg = error;
  38. }
  39. ctx.body = responseData;
  40. }
  41. /**
  42. * 根据id获取子项
  43. *
  44. * @param {Object} ctx - egg全局变量
  45. * @return {void}
  46. */
  47. async getChildren(ctx) {
  48. const responseData = {
  49. err: 0,
  50. msg: '',
  51. data: [],
  52. };
  53. try {
  54. const data = JSON.parse(ctx.request.body.data);
  55. if (isNaN(data.pid) || data.pid <= 0 || isNaN(data.list_id) || data.list_id <= 0) {
  56. throw '参数错误';
  57. }
  58. const condition = { pid: data.pid, list_id: data.list_id };
  59. switch (data.stdType) {
  60. case 'gcl':
  61. responseData.data = await this.ctx.service.stdGcl.getAllDataByCondition({ where: condition });
  62. break;
  63. case 'xmj':
  64. responseData.data = await this.ctx.service.stdXmj.getAllDataByCondition({ where: condition });
  65. break;
  66. case 'glj':
  67. responseData.data = await this.ctx.service.gljLib.getAllDataByCondition({ where: condition });
  68. break;
  69. default:
  70. throw '查询的标准清单不存在';
  71. }
  72. } catch (error) {
  73. responseData.err = 1;
  74. responseData.msg = error;
  75. }
  76. ctx.body = responseData;
  77. }
  78. }
  79. return StandardLibController;
  80. };