menu.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. const assert = require('assert');
  3. const Service = require('egg').Service;
  4. class MenuService extends Service {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.model = this.ctx.model.Menu;
  8. this.column = this.ctx.model.Column;
  9. this.pages = this.ctx.model.Pages;
  10. }
  11. async create({ name, code, date, type, url, column, pages, parentCode, isshow, template }) {
  12. assert(name, '名称不存在');
  13. assert(code, '编码不存在');
  14. assert(type, '类型不存在');
  15. try {
  16. const res = await this.model.findOne({ code });
  17. if (res) return { errcode: -1001, errmsg: '编码已存在', data: '' };
  18. const item = await this.model.create({ name, code, date, type, url, column, pages, parentCode, isshow, template });
  19. return { errcode: 0, errmsg: '', data: item };
  20. } catch (error) {
  21. throw error;
  22. }
  23. }
  24. async update({ id, name, date, type, url, column, pages, parentCode, isshow, template }) {
  25. assert(id, 'id不存在');
  26. try {
  27. const res = await this.model.findOne({ _id: id });
  28. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  29. await this.model.updateOne({ _id: id }, { name, date, type, url, column, pages, parentCode, isshow, template });
  30. return { errcode: 0, errmsg: '', data: 'update' };
  31. } catch (error) {
  32. throw error;
  33. }
  34. }
  35. async delete({ id }) {
  36. assert(id, 'id不存在');
  37. try {
  38. const res = await this.model.findOne({ _id: id });
  39. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  40. const column = await this.column.findOne({ bind: res.code });
  41. const pages = await this.pages.findOne({ bind: res.code });
  42. if (column || pages) return { errcode: -1001, errmsg: '存在绑定关系,不能删除', data: '' };
  43. await this.model.remove({ _id: id });
  44. return { errcode: 0, errmsg: '', data: 'delete' };
  45. } catch (error) {
  46. throw error;
  47. }
  48. }
  49. async query({ skip, limit, name, code, parentCode, isshow }) {
  50. const filter = {};
  51. if (name || code || parentCode) filter.$or = [];
  52. if (name) filter.$or.push({ name: { $regex: name } });
  53. if (code) filter.$or.push({ code: { $regex: code } });
  54. if (parentCode) filter.$or.push({ parentCode: { $regex: parentCode } });
  55. if (isshow) filter.isshow = isshow;
  56. try {
  57. let res;
  58. const total = await this.model.find({ ...filter });
  59. if (skip && limit) {
  60. res = await this.model.find({ ...filter })
  61. .skip(Number(skip) * Number(limit))
  62. .limit(Number(limit))
  63. .sort({ date: 1 });
  64. } else {
  65. res = await this.model.find({ ...filter }).sort({ date: 1 });
  66. }
  67. return { errcode: 0, errmsg: '', data: res, total: total.length };
  68. } catch (error) {
  69. throw error;
  70. }
  71. }
  72. async menuList({ code }) {
  73. const list = await this.model.find();
  74. const lists = [];
  75. const children = ({ code }) => {
  76. const item = list.filter(j => code === j.parentCode).map(e => children(e));
  77. lists.push(...item);
  78. return code;
  79. };
  80. children({ code });
  81. return lists;
  82. }
  83. }
  84. module.exports = MenuService;