123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 'use strict';
- const assert = require('assert');
- const moment = require('moment');
- const Service = require('egg').Service;
- class ImgnewsService extends Service {
- constructor(ctx) {
- super(ctx);
- this.model = this.ctx.model.Imgnews;
- this.column = this.ctx.model.Column;
- this.pages = this.ctx.model.Pages;
- }
- async create({ type, bind, date, img, url, content, describe, title, isshow, twoTitle }) {
- assert(bind, '绑定类型不存在');
- // assert(img, '图片不存在');
- assert(title, '标题不存在');
- try {
- const createAt = moment().format('x');
- const updateAt = moment().format('x');
- const item = await this.model.create({ title, type, bind, date, img, url, content, describe, createAt, updateAt, isshow, twoTitle });
- return { errcode: 0, errmsg: '', data: item };
- } catch (error) {
- throw error;
- }
- }
- async update({ id, type, bind, date, img, url, content, describe, title, isshow, 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, type, bind, date, img, url, content, describe, updateAt, isshow, twoTitle });
- 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, isshow, twoTitle }) {
- const filter = {};
- if (title || date) filter.$or = [];
- if (title) filter.$or.push({ code: { $regex: title } });
- if (twoTitle) filter.$or.push({ code: { $regex: twoTitle } });
- if (date) filter.$or.push({ date: { $regex: date } });
- if (bind) filter.bind = bind;
- if (isshow) filter.isshow = isshow;
- 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: '' };
- return { errcode: 0, errmsg: '', data: res };
- } catch (error) {
- throw error;
- }
- }
- }
- module.exports = ImgnewsService;
|