standard_lib_controller.js 2.8 KB

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