column.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const assert = require('assert');
  4. const moment = require('moment');
  5. // const _ = require('lodash');
  6. class ColumnService extends Service {
  7. async create({ name, code, menuList }) {
  8. assert(name, '名称不存在');
  9. assert(code, '编码不存在');
  10. const { Column: model } = this.ctx.model;
  11. const createAt = moment().format('x');
  12. try {
  13. await model.create({ name, code, menuList, createAt });
  14. return { errmsg: '', errcode: 0 };
  15. } catch (error) {
  16. throw new Error({ errcode: -2001, errmsg: '添加失败' });
  17. }
  18. }
  19. async update({ name, code, _id, menuList }) {
  20. assert(_id, 'id不存在');
  21. const { Column: model } = this.ctx.model;
  22. try {
  23. await model.findById(_id).update({ name, code, menuList });
  24. return { errmsg: '', errcode: 0 };
  25. } catch (error) {
  26. throw new Error({ errcode: -2001, errmsg: '修改失败' });
  27. }
  28. }
  29. async del({ id }) {
  30. assert(id, 'id不存在');
  31. const { Column: model } = this.ctx.model;
  32. const { Content: Contentmodel } = this.ctx.model;
  33. try {
  34. // 当前数据
  35. const res = await model.findById(id);
  36. // 复杂查询文章数据
  37. const contentList = await Contentmodel.where('columnList').in(res.code);
  38. contentList.filter(async p => {
  39. const columnList = p.columnList.filter(e => e !== res.code);
  40. await Contentmodel.findByIdAndUpdate(p._id, { columnList });
  41. });
  42. await model.findByIdAndDelete(id);
  43. return { errmsg: '', errcode: 0 };
  44. } catch (error) {
  45. throw new Error({ errcode: -2001, errmsg: '删除失败' });
  46. }
  47. }
  48. async menuquery({ code }) {
  49. const { Column: model } = this.ctx.model;
  50. try {
  51. const res = await model.where('menuList').in(code);
  52. return { errmsg: '', errcode: 0, data: res };
  53. } catch (error) {
  54. throw new Error({ errcode: -2001, errmsg: '查询失败' });
  55. }
  56. }
  57. async usercolumnquery() {
  58. const { Column: model } = this.ctx.model;
  59. const { AdminUser: Usermodel } = this.ctx.model;
  60. try {
  61. const str = this.ctx.request.header.authorization;
  62. const token = str.substring(7);
  63. const decode = this.ctx.app.jwt.verify(token, this.app.config.jwt.secret);
  64. const columnList = [];
  65. const res = await Usermodel.find({ acct: decode.acct });
  66. if (res.length > 0) {
  67. const usercolumnList = res[0].columnList;
  68. if (usercolumnList.length > 0) {
  69. for (let i = 0; i < usercolumnList.length; i++) {
  70. const item = await model.find({ code: usercolumnList[i] });
  71. columnList.push(item[0]);
  72. }
  73. }
  74. }
  75. return { errmsg: '', errcode: 0, data: columnList };
  76. } catch (error) {
  77. throw new Error({ errcode: -2001, errmsg: '查询失败' });
  78. }
  79. }
  80. async query({ skip, limit, name, code }) {
  81. const { Column: model } = this.ctx.model;
  82. const filter = {};
  83. if (name) filter.name = name;
  84. if (code) filter.code = code;
  85. try {
  86. let res;
  87. const total = await model.find();
  88. if (skip && limit) {
  89. res = await model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  90. } else {
  91. res = await model.find({ ...filter });
  92. }
  93. return { errmsg: '', errcode: 0, data: res, total: total.length };
  94. } catch (error) {
  95. console.log(error, 111);
  96. throw new Error({ errcode: -2001, errmsg: '查询失败' });
  97. }
  98. }
  99. }
  100. module.exports = ColumnService;