12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- '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;
- }
- async create({ title, describe, content, bind, istop, date, thumbnail, recommend, annex, keywords, svip, isShow, status }) {
- 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, describe, content, bind, istop, date, thumbnail, recommend, annex, createAt, updateAt, keywords, svip, isShow, status });
- return { errcode: 0, errmsg: '', data: item };
- } catch (error) {
- throw error;
- }
- }
- async update({ id, title, describe, content, bind, istop, date, thumbnail, recommend, annex, keywords, svip, isShow, status }) {
- 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, describe, content, bind, istop, date, thumbnail, recommend, annex, updateAt, keywords, svip, isShow, status });
- 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 }) {
- const filter = {};
- const arr = { title, date, bind, keywords, svip, isShow, status };
- 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));
- }
- }
- try {
- let res;
- const total = await this.model.find({ ...filter }, { content: false });
- if (skip && limit) {
- res = await this.model.find({ ...filter }, { content: false })
- .skip(Number(skip) * Number(limit))
- .limit(Number(limit))
- .sort({ date: 1 });
- } else {
- res = await this.model.find({ ...filter }, { content: false }).sort({ 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;
- }
- }
- }
- module.exports = ContentService;
|