imgnews.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, column, pages, title }) {
  13. assert(type, '类型不存在');
  14. assert(bind, '绑定类型不存在');
  15. assert(img, '图片不存在');
  16. assert(title, '图片不存在');
  17. try {
  18. const createAt = moment().format('x');
  19. const updateAt = moment().format('x');
  20. const item = await this.model.create({ title, type, bind, date, img, url, column, pages, createAt, updateAt });
  21. return { errcode: 0, errmsg: '', data: item };
  22. } catch (error) {
  23. throw error;
  24. }
  25. }
  26. async update({ id, type, bind, date, img, url, column, pages, title }) {
  27. assert(id, 'id不存在');
  28. try {
  29. const res = await this.model.findOne({ _id: id });
  30. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  31. const updateAt = moment().format('x');
  32. await this.model.updateOne({ _id: id }, { title, type, bind, date, img, url, column, pages, updateAt });
  33. return { errcode: 0, errmsg: '', data: 'update' };
  34. } catch (error) {
  35. throw error;
  36. }
  37. }
  38. async delete({ id }) {
  39. assert(id, 'id不存在');
  40. try {
  41. const res = await this.model.findOne({ _id: id });
  42. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  43. await this.model.remove({ _id: id });
  44. return { errcode: 0, errmsg: '', data: 'delete' };
  45. } catch (error) {
  46. throw error;
  47. }
  48. }
  49. async query({ skip, limit, title, date, bind }) {
  50. const filter = {};
  51. if (title || date || bind) filter.$or = [];
  52. if (title) filter.$or.push({ code: { $regex: title } });
  53. if (date) filter.$or.push({ date: { $regex: date } });
  54. if (bind) filter.$or.push({ bind: { $regex: bind } });
  55. try {
  56. let res;
  57. const total = await this.model.find({ ...filter }, { content: false });
  58. if (skip && limit) {
  59. res = await this.model.find({ ...filter }, { content: false })
  60. .skip(Number(skip) * Number(limit))
  61. .limit(Number(limit))
  62. .sort({ date: 1 });
  63. } else {
  64. res = await this.model.find({ ...filter }, { content: false }).sort({ date: 1 });
  65. }
  66. return { errcode: 0, errmsg: '', data: res, total: total.length };
  67. } catch (error) {
  68. throw error;
  69. }
  70. }
  71. async fetch({ id }) {
  72. try {
  73. const res = await this.model.findOne({ _id: id });
  74. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  75. return { errcode: 0, errmsg: '', data: res };
  76. } catch (error) {
  77. throw error;
  78. }
  79. }
  80. }
  81. module.exports = ImgnewsService;