standard_lib_controller.js 2.5 KB

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