pages.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, bind }) {
  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, bind });
  22. return { errcode: 0, errmsg: '', data: item };
  23. } catch (error) {
  24. throw error;
  25. }
  26. }
  27. async update({ id, title, describe, content, bind }) {
  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, bind });
  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, bind }) {
  59. const filter = {};
  60. if (title || code || bind) filter.$or = [];
  61. if (title) filter.$or.push({ title: { $regex: title } });
  62. if (code) filter.$or.push({ code: { $regex: code } });
  63. if (bind) filter.$or.push({ bind: { $regex: bind } });
  64. try {
  65. let res;
  66. const total = await this.model.find({ ...filter }, { content: false });
  67. if (skip && limit) {
  68. res = await this.model.find({ ...filter }, { content: false }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  69. } else {
  70. res = await this.model.find({ ...filter }, { content: false });
  71. }
  72. return { errcode: 0, errmsg: '', data: res, total: total.length };
  73. } catch (error) {
  74. throw error;
  75. }
  76. }
  77. async fetch({ id, bind }) {
  78. try {
  79. // const filter = {};
  80. // if (id || bind) filter.$or = [];
  81. // if (id) filter.$or.push({ _id: { $regex: id } });
  82. // if (bind) filter.$or.push({ bind: { $regex: bind } });
  83. let res;
  84. if (id) res = await this.model.findById(id);
  85. if (bind) res = await this.model.findOne({ bind });
  86. if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  87. await this.model.updateOne({ _id: id }, { visit: res.visit + 1 });
  88. return { errcode: 0, errmsg: '', data: res };
  89. } catch (error) {
  90. throw error;
  91. }
  92. }
  93. }
  94. module.exports = PagesService;