pages.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. const assert = require('assert');
  3. const moment = require('moment');
  4. const Service = require('egg').Service;
  5. class PagesService extends Service {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.model = this.ctx.model.Pages;
  9. this.menu = this.ctx.model.Menu;
  10. this.imgnews = this.ctx.model.Imgnews;
  11. }
  12. async create({ title, describe, content, code }) {
  13. assert(title, '标题不存在');
  14. assert(describe, '描述不存在');
  15. assert(content, '内容不存在');
  16. try {
  17. const res = await this.model.findOne({ code });
  18. if (res) return { errcode: -1001, errmsg: '编码已存在', data: '' };
  19. const createAt = moment().format('x');
  20. const updateAt = moment().format('x');
  21. const item = await this.model.create({ title, describe, content, code, createAt, updateAt });
  22. return { errcode: 0, errmsg: '', data: item };
  23. } catch (error) {
  24. throw error;
  25. }
  26. }
  27. async update({ id, title, describe, content }) {
  28. assert(id, 'id不存在');
  29. try {
  30. const res = await this.model.findOne({ _id: id });
  31. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  32. const updateAt = moment().format('x');
  33. await this.model.updateOne({ _id: id }, { title, describe, content, updateAt });
  34. return { errcode: 0, errmsg: '', data: 'update' };
  35. } catch (error) {
  36. throw error;
  37. }
  38. }
  39. async delete({ id }) {
  40. assert(id, 'id不存在');
  41. try {
  42. const res = await this.model.findOne({ _id: id });
  43. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  44. const menu = await this.menu.findOne({ column: res.code });
  45. const imgnews = await this.imgnews.findOne({ column: res.code });
  46. if (menu || imgnews) {
  47. let errmsg = '';
  48. if (menu) errmsg += '(菜单)';
  49. if (imgnews) errmsg += '(图片新闻)';
  50. return { errcode: -1001, errmsg: `存在${errmsg}绑定关系,不能删除`, data: '' };
  51. }
  52. await this.model.remove({ _id: id });
  53. return { errcode: 0, errmsg: '', data: 'delete' };
  54. } catch (error) {
  55. throw error;
  56. }
  57. }
  58. async query({ skip, limit, title, code }) {
  59. const filter = {};
  60. if (title || code) filter.$or = [];
  61. if (title) filter.$or.push({ code: { $regex: title } });
  62. if (code) filter.$or.push({ code: { $regex: code } });
  63. try {
  64. let res;
  65. const total = await this.model.find({ ...filter }, { content: false });
  66. if (skip && limit) {
  67. res = await this.model.find({ ...filter }, { content: false }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  68. } else {
  69. res = await this.model.find({ ...filter }, { content: false });
  70. }
  71. return { errcode: 0, errmsg: '', data: res, total: total.length };
  72. } catch (error) {
  73. throw error;
  74. }
  75. }
  76. async fetch({ id }) {
  77. try {
  78. const res = await this.model.findOne({ _id: id });
  79. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  80. await this.model.updateOne({ _id: id }, { visit: res.visit + 1 });
  81. return { errcode: 0, errmsg: '', data: res };
  82. } catch (error) {
  83. throw error;
  84. }
  85. }
  86. }
  87. module.exports = PagesService;