menu.js 3.4 KB

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