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