content.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  10. async create({ title, describe, content, bind, istop, date, thumbnail, recommend, annex, keywords, svip, isShow, status }) {
  11. assert(title, '标题不存在');
  12. assert(describe, '描述不存在');
  13. assert(content, '内容不存在');
  14. assert(bind, '绑定栏目不存在');
  15. try {
  16. const createAt = moment().format('x');
  17. const updateAt = moment().format('x');
  18. const item = await this.model.create({ title, describe, content, bind, istop, date, thumbnail, recommend, annex, createAt, updateAt, keywords, svip, isShow, status });
  19. return { errcode: 0, errmsg: '', data: item };
  20. } catch (error) {
  21. throw error;
  22. }
  23. }
  24. async update({ id, title, describe, content, bind, istop, date, thumbnail, recommend, annex, keywords, svip, isShow, status }) {
  25. assert(id, 'id不存在');
  26. try {
  27. const res = await this.model.findOne({ _id: id });
  28. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  29. const updateAt = moment().format('x');
  30. await this.model.updateOne({ _id: id }, { title, describe, content, bind, istop, date, thumbnail, recommend, annex, updateAt, keywords, svip, isShow, status });
  31. return { errcode: 0, errmsg: '', data: 'update' };
  32. } catch (error) {
  33. throw error;
  34. }
  35. }
  36. async delete({ id }) {
  37. assert(id, 'id不存在');
  38. try {
  39. const res = await this.model.findOne({ _id: id });
  40. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  41. await this.model.remove({ _id: id });
  42. return { errcode: 0, errmsg: '', data: 'delete' };
  43. } catch (error) {
  44. throw error;
  45. }
  46. }
  47. async query({ skip, limit, title, date, bind, keywords, svip, isShow, status }) {
  48. const filter = {};
  49. const arr = { title, date, bind, keywords, svip, isShow, status };
  50. for (const e in arr) {
  51. let datas;
  52. if (e === 'keywords') {
  53. datas = `{ "${e}": { "$elemMatch": { "$eq": "${arr[e]}" } } }`;
  54. } else {
  55. datas = `{ "${e}": { "$regex": "${arr[e]}" } }`;
  56. }
  57. if (arr[e]) {
  58. filter.$or = [];
  59. filter.$or.push(JSON.parse(datas));
  60. }
  61. }
  62. try {
  63. let res;
  64. const total = await this.model.find({ ...filter }, { content: false });
  65. if (skip && limit) {
  66. res = await this.model.find({ ...filter }, { content: false })
  67. .skip(Number(skip) * Number(limit))
  68. .limit(Number(limit))
  69. .sort({ date: 1 });
  70. } else {
  71. res = await this.model.find({ ...filter }, { content: false }).sort({ date: 1 });
  72. }
  73. return { errcode: 0, errmsg: '', data: res, total: total.length };
  74. } catch (error) {
  75. throw error;
  76. }
  77. }
  78. async fetch({ id }) {
  79. try {
  80. const res = await this.model.findOne({ _id: id });
  81. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  82. await this.model.updateOne({ _id: id }, { visit: res.visit + 1 });
  83. return { errcode: 0, errmsg: '', data: res };
  84. } catch (error) {
  85. throw error;
  86. }
  87. }
  88. }
  89. module.exports = ContentService;