menu.js 2.9 KB

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