content.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. const assert = require('assert');
  3. const moment = require('moment');
  4. const Service = require('egg').Service;
  5. class ContentService extends Service {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.model = this.ctx.model.Content;
  9. this.menuModel = this.ctx.model.Menu;
  10. }
  11. async create({ title, describe, source, content, bind, istop, date, thumbnail, recommend, annex, keywords, svip, isShow, status, curtTitle, author, twoTitle }) {
  12. assert(title, '标题不存在');
  13. // assert(describe, '描述不存在');
  14. assert(content, '内容不存在');
  15. assert(bind, '绑定栏目不存在');
  16. try {
  17. const createAt = moment().format('x');
  18. const updateAt = moment().format('x');
  19. const item = await this.model.create({ title, twoTitle, describe, author, source, content, bind, istop, date, thumbnail, recommend, annex, createAt, updateAt, keywords, svip, isShow, status, curtTitle });
  20. return { errcode: 0, errmsg: '', data: item };
  21. } catch (error) {
  22. throw error;
  23. }
  24. }
  25. async update({ id, title, describe, author, source, content, bind, istop, date, thumbnail, recommend, annex, keywords, svip, isShow, status, curtTitle, twoTitle }) {
  26. assert(id, 'id不存在');
  27. try {
  28. const res = await this.model.findOne({ _id: id });
  29. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  30. const updateAt = moment().format('x');
  31. await this.model.updateOne({ _id: id }, { title, twoTitle, describe, author, source, content, bind, istop, date, thumbnail, recommend, annex, updateAt, keywords, svip, isShow, status, curtTitle });
  32. return { errcode: 0, errmsg: '', data: 'update' };
  33. } catch (error) {
  34. throw error;
  35. }
  36. }
  37. async delete({ id }) {
  38. assert(id, 'id不存在');
  39. try {
  40. const res = await this.model.findOne({ _id: id });
  41. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  42. await this.model.remove({ _id: id });
  43. return { errcode: 0, errmsg: '', data: 'delete' };
  44. } catch (error) {
  45. throw error;
  46. }
  47. }
  48. async query({ skip, limit, title, date, bind, keywords, svip, isShow, status, istop, parentCode, recommend, twoTitle }) {
  49. if (parentCode) {
  50. const menus = await this.service.menu.menuList({ code: parentCode });
  51. const filter = { bind: { $in: menus } };
  52. const total = await this.model.find({ ...filter }, { content: false });
  53. const res = await this.model.find({ ...filter }, { content: false })
  54. .sort({ istop: -1, date: -1 })
  55. .skip(Number(skip) * Number(limit))
  56. .limit(Number(limit));
  57. return { errcode: 0, errmsg: '', data: res, total: total.length };
  58. }
  59. const filter = {};
  60. const arr = { title, date, keywords, svip, isShow, status, istop, twoTitle };
  61. for (const e in arr) {
  62. let datas;
  63. if (e === 'keywords') {
  64. datas = `{ "${e}": { "$elemMatch": { "$eq": "${arr[e]}" } } }`;
  65. } else {
  66. datas = `{ "${e}": { "$regex": "${arr[e]}" } }`;
  67. }
  68. if (arr[e]) {
  69. filter.$or = [];
  70. filter.$or.push(JSON.parse(datas));
  71. }
  72. }
  73. if (recommend) filter.recommend = recommend;
  74. if (bind) filter.bind = bind;
  75. try {
  76. let res;
  77. const total = await this.model.find({ ...filter }, { content: false });
  78. if (skip && limit) {
  79. res = await this.model.find({ ...filter }, { content: false })
  80. .sort({ istop: -1, date: -1 })
  81. .skip(Number(skip) * Number(limit))
  82. .limit(Number(limit));
  83. } else {
  84. res = await this.model.find({ ...filter }, { content: false }).sort({ istop: -1, date: -1 });
  85. }
  86. return { errcode: 0, errmsg: '', data: res, total: total.length };
  87. } catch (error) {
  88. throw error;
  89. }
  90. }
  91. async fetch({ id }) {
  92. try {
  93. const res = await this.model.findOne({ _id: id });
  94. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  95. await this.model.updateOne({ _id: id }, { visit: res.visit + 1 });
  96. return { errcode: 0, errmsg: '', data: res };
  97. } catch (error) {
  98. throw error;
  99. }
  100. }
  101. async search({ search, skip, limit }) {
  102. try {
  103. const list = await this.model.find();
  104. const data = list.filter(e => (e.content.includes(search) || e.title.includes(search)));
  105. const res = data.slice(Number(skip) * Number(limit), (Number(skip) + 1) * Number(limit));
  106. return { errcode: 0, errmsg: '', data: res, total: data.length };
  107. } catch (error) {
  108. throw error;
  109. }
  110. }
  111. }
  112. module.exports = ContentService;