123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 'use strict';
- const assert = require('assert');
- const moment = require('moment');
- const Service = require('egg').Service;
- class ContentService extends Service {
- constructor(ctx) {
- super(ctx);
- this.model = this.ctx.model.Content;
- this.menuModel = this.ctx.model.Menu;
- }
- async create({ title, describe, source, content, bind, istop, date, thumbnail, recommend, annex, keywords, svip, isShow, status, curtTitle, author, twoTitle }) {
- assert(title, '标题不存在');
- // assert(describe, '描述不存在');
- assert(content, '内容不存在');
- assert(bind, '绑定栏目不存在');
- try {
- const createAt = moment().format('x');
- const updateAt = moment().format('x');
- const item = await this.model.create({ title, twoTitle, describe, author, source, content, bind, istop, date, thumbnail, recommend, annex, createAt, updateAt, keywords, svip, isShow, status, curtTitle });
- return { errcode: 0, errmsg: '', data: item };
- } catch (error) {
- throw error;
- }
- }
- async update({ id, title, describe, author, source, content, bind, istop, date, thumbnail, recommend, annex, keywords, svip, isShow, status, curtTitle, twoTitle }) {
- assert(id, 'id不存在');
- try {
- const res = await this.model.findOne({ _id: id });
- if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
- const updateAt = moment().format('x');
- await this.model.updateOne({ _id: id }, { title, twoTitle, describe, author, source, content, bind, istop, date, thumbnail, recommend, annex, updateAt, keywords, svip, isShow, status, curtTitle });
- return { errcode: 0, errmsg: '', data: 'update' };
- } catch (error) {
- throw error;
- }
- }
- async delete({ id }) {
- assert(id, 'id不存在');
- try {
- const res = await this.model.findOne({ _id: id });
- if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
- await this.model.remove({ _id: id });
- return { errcode: 0, errmsg: '', data: 'delete' };
- } catch (error) {
- throw error;
- }
- }
- async query({ skip, limit, title, date, bind, keywords, svip, isShow, status, istop, parentCode, recommend, twoTitle }) {
- if (parentCode) {
- const menus = await this.service.menu.menuList({ code: parentCode });
- const filter = { bind: { $in: menus } };
- const total = await this.model.find({ ...filter }, { content: false });
- const res = await this.model.find({ ...filter }, { content: false })
- .sort({ istop: -1, date: -1 })
- .skip(Number(skip) * Number(limit))
- .limit(Number(limit));
- return { errcode: 0, errmsg: '', data: res, total: total.length };
- }
- const filter = {};
- const arr = { title, date, keywords, svip, isShow, status, istop, twoTitle };
- for (const e in arr) {
- let datas;
- if (e === 'keywords') {
- datas = `{ "${e}": { "$elemMatch": { "$eq": "${arr[e]}" } } }`;
- } else {
- datas = `{ "${e}": { "$regex": "${arr[e]}" } }`;
- }
- if (arr[e]) {
- filter.$or = [];
- filter.$or.push(JSON.parse(datas));
- }
- }
- if (recommend) filter.recommend = recommend;
- if (bind) filter.bind = bind;
- try {
- let res;
- const total = await this.model.find({ ...filter }, { content: false });
- if (skip && limit) {
- res = await this.model.find({ ...filter }, { content: false })
- .sort({ istop: -1, date: -1 })
- .skip(Number(skip) * Number(limit))
- .limit(Number(limit));
- } else {
- res = await this.model.find({ ...filter }, { content: false }).sort({ istop: -1, date: -1 });
- }
- return { errcode: 0, errmsg: '', data: res, total: total.length };
- } catch (error) {
- throw error;
- }
- }
- async fetch({ id }) {
- try {
- const res = await this.model.findOne({ _id: id });
- if (!res) return { errcode: -1001, errmsg: '数据不存在', data: '' };
- await this.model.updateOne({ _id: id }, { visit: res.visit + 1 });
- return { errcode: 0, errmsg: '', data: res };
- } catch (error) {
- throw error;
- }
- }
- async search({ search, skip, limit }) {
- try {
- const list = await this.model.find();
- const data = list.filter(e => (e.content.includes(search) || e.title.includes(search)));
- const res = data.slice(Number(skip) * Number(limit), (Number(skip) + 1) * Number(limit));
- return { errcode: 0, errmsg: '', data: res, total: data.length };
- } catch (error) {
- throw error;
- }
- }
- }
- module.exports = ContentService;
|