imgnews.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. const assert = require('assert');
  3. const moment = require('moment');
  4. const Service = require('egg').Service;
  5. class ImgnewsService extends Service {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.model = this.ctx.model.Imgnews;
  9. this.column = this.ctx.model.Column;
  10. this.pages = this.ctx.model.Pages;
  11. }
  12. async create({ type, bind, date, img, url, content, describe, title, isshow, twoTitle }) {
  13. assert(bind, '绑定类型不存在');
  14. // assert(img, '图片不存在');
  15. assert(title, '标题不存在');
  16. try {
  17. const createAt = moment().format('x');
  18. const updateAt = moment().format('x');
  19. const item = await this.model.create({ title, type, bind, date, img, url, content, describe, createAt, updateAt, isshow, twoTitle });
  20. return { errcode: 0, errmsg: '', data: item };
  21. } catch (error) {
  22. throw error;
  23. }
  24. }
  25. async update({ id, type, bind, date, img, url, content, describe, title, isshow, 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, type, bind, date, img, url, content, describe, updateAt, isshow, twoTitle });
  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, isshow, twoTitle }) {
  49. const filter = {};
  50. if (title || date) filter.$or = [];
  51. if (title) filter.$or.push({ code: { $regex: title } });
  52. if (twoTitle) filter.$or.push({ code: { $regex: twoTitle } });
  53. if (date) filter.$or.push({ date: { $regex: date } });
  54. if (bind) filter.bind = bind;
  55. if (isshow) filter.isshow = isshow;
  56. try {
  57. let res;
  58. const total = await this.model.find({ ...filter }, { content: false });
  59. if (skip && limit) {
  60. res = await this.model.find({ ...filter }, { content: false })
  61. .skip(Number(skip) * Number(limit))
  62. .limit(Number(limit))
  63. .sort({ date: -1 });
  64. } else {
  65. res = await this.model.find({ ...filter }, { content: false }).sort({ date: -1 });
  66. }
  67. return { errcode: 0, errmsg: '', data: res, total: total.length };
  68. } catch (error) {
  69. throw error;
  70. }
  71. }
  72. async fetch({ id }) {
  73. try {
  74. const res = await this.model.findOne({ _id: id });
  75. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  76. return { errcode: 0, errmsg: '', data: res };
  77. } catch (error) {
  78. throw error;
  79. }
  80. }
  81. }
  82. module.exports = ImgnewsService;