menu.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { BusinessError, ErrorCode } = require('naf-core').Error;
  5. const { isNullOrUndefined, trimData } = require('naf-core').Util;
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. class MenuService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.model = this.ctx.model.Menu;
  11. }
  12. async create({ site }, { parent_id, level, parent_name, position,path,name, picurl, description, isShow, issuer }) {
  13. // 检查数据
  14. assert(_.isString(site), 'site不能为空');
  15. assert(!level || _.isString(level), 'level不能为空');
  16. assert(!path || _.isString(path), 'path不能为空');
  17. assert(!position || _.isNumber(position), 'position不能为空');
  18. assert(!parent_id || _.isString(parent_id), 'parent_id不能为空');
  19. assert(!parent_name || _.isString(parent_name), 'parent_name不能为空');
  20. assert(!name || _.isString(name), 'name不能为空');
  21. assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
  22. assert(!description || _.isString(description), 'description必须为字符串');
  23. assert(!isShow || _.isString(isShow), 'isShow必须为字符串');
  24. assert(!issuer || _.isString(issuer), 'issuer必须为字符串');
  25. // TODO: 检查用户信息
  26. const userid = this.ctx.userid;
  27. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  28. // TODO:保存数据
  29. const data = {
  30. site, parent_id, parent_name, level, position,path,name, picurl, description, isShow, issuer,
  31. meta: { createdBy: userid },
  32. };
  33. const res = await this.model.create(data);
  34. return res;
  35. }
  36. async update({ id }, payload) {
  37. // 检查数据
  38. const { parent_id, level, parent_name, position,path,name, picurl, description, isShow, issuer } = payload;
  39. assert(id, 'id不能为空');
  40. assert(!parent_id || _.isString(parent_id), 'parent_id不能为空');
  41. assert(!level || _.isString(level), 'level不能为空');
  42. assert(!position || _.isNumber(position), 'position不能为空');
  43. assert(!path || _.isString(path), 'path不能为空');
  44. assert(!parent_name || _.isString(parent_name), 'parent_name不能为空');
  45. assert(!name || _.isString(name), 'name不能为空');
  46. assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
  47. assert(!description || _.isString(description), 'description必须为字符串');
  48. assert(!isShow || _.isString(isShow), 'isShow必须为字符串');
  49. assert(!issuer || _.isString(issuer), 'issuer必须为字符串');
  50. // TODO: 检查用户信息
  51. const userid = this.ctx.userid;
  52. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  53. // TODO:检查数据是否存在
  54. const doc = await this.model.findById(id).exec();
  55. if (isNullOrUndefined(doc)) {
  56. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  57. }
  58. // TODO:保存数据
  59. const data = trimData(payload);
  60. await this.model.findByIdAndUpdate(doc.id, { ...data, 'meta.updatedBy': userid }, { new: true }).exec();
  61. const res = this.model.findById(id, '+name').exec();
  62. return res;
  63. }
  64. async status({ id, state }) {
  65. // TODO: 检查数据状态
  66. const doc = await this.model.findById(id).exec();
  67. if (!doc) {
  68. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  69. }
  70. doc.meta.state = state;
  71. return await doc.save();
  72. }
  73. async delete({ id }) {
  74. return await this.status({ id, state: 1 });
  75. }
  76. async restore({ id }) {
  77. return await this.status({ id, state: 0 });
  78. }
  79. }
  80. module.exports = MenuService;