menu.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 }, { parentId, name, picurl, sort, description, isShow, issuer }) {
  13. // 检查数据
  14. assert(_.isString(site), 'site不能为空');
  15. assert(_.isString(parentId), 'parentId不能为空');
  16. assert(_.isString(name), 'name不能为空');
  17. assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
  18. assert(!description || _.isString(description), 'description必须为字符串');
  19. assert(_.isUndefined(sort) || _.isNumber(sort), 'top必须为数字');
  20. assert(!isShow || _.isString(isShow), 'isShow必须为字符串');
  21. assert(!issuer || _.isString(issuer), 'issuer必须为字符串');
  22. // TODO: 检查用户信息
  23. const userid = this.ctx.userid;
  24. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  25. // TODO:保存数据
  26. const data = {
  27. parentId, name, picurl, sort, description, isShow, issuer,
  28. meta: { createdBy: userid },
  29. };
  30. const res = await this.model.create(data);
  31. return res;
  32. }
  33. async update({ id }, payload) {
  34. // 检查数据
  35. const { parentId, name, picurl, sort, description, isShow, issuer } = payload;
  36. assert(id, 'id不能为空');
  37. assert(_.isString(parentId), 'parentId不能为空');
  38. assert(_.isString(name), 'name不能为空');
  39. assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
  40. assert(!description || _.isString(description), 'description必须为字符串');
  41. assert(_.isUndefined(sort) || _.isNumber(sort), 'top必须为数字');
  42. assert(!isShow || _.isString(isShow), 'isShow必须为字符串');
  43. assert(!issuer || _.isString(issuer), 'issuer必须为字符串');
  44. // TODO: 检查用户信息
  45. const userid = this.ctx.userid;
  46. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  47. // TODO:检查数据是否存在
  48. const doc = await this.model.findById(id).exec();
  49. if (isNullOrUndefined(doc)) {
  50. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  51. }
  52. // TODO:保存数据
  53. const data = trimData(payload);
  54. await this.model.findByIdAndUpdate(doc.id, { ...data, 'meta.updatedBy': userid }, { new: true }).exec();
  55. const res = this.model.findById(id, '+name').exec();
  56. return res;
  57. }
  58. async status({ id, state }) {
  59. // TODO: 检查数据状态
  60. const doc = await this.model.findById(id).exec();
  61. if (!doc) {
  62. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  63. }
  64. doc.meta.state = state;
  65. return await doc.save();
  66. }
  67. async delete({ id }) {
  68. return await this.status({ id, state: 1 });
  69. }
  70. async restore({ id }) {
  71. return await this.status({ id, state: 0 });
  72. }
  73. }
  74. module.exports = MenuService;