standard_lib_controller.js 2.5 KB

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