'use strict'; const assert = require('assert'); const moment = require('moment'); const Service = require('egg').Service; class PagesService extends Service { constructor(ctx) { super(ctx); this.model = this.ctx.model.Pages; this.menu = this.ctx.model.Menu; this.imgnews = this.ctx.model.Imgnews; } async create({ title, describe, content, code, bind }) { assert(title, '标题不存在'); assert(describe, '描述不存在'); assert(content, '内容不存在'); try { const res = await this.model.findOne({ code }); if (res) return { errcode: -1001, errmsg: '编码已存在', data: '' }; const createAt = moment().format('x'); const updateAt = moment().format('x'); const item = await this.model.create({ title, describe, content, code, createAt, updateAt, bind }); return { errcode: 0, errmsg: '', data: item }; } catch (error) { throw error; } } async update({ id, title, describe, content, bind }) { 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, updateAt, bind }); 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: '' }; const menu = await this.menu.findOne({ column: res.code }); const imgnews = await this.imgnews.findOne({ column: res.code }); if (menu || imgnews) { let errmsg = ''; if (menu) errmsg += '(菜单)'; if (imgnews) errmsg += '(图片新闻)'; return { errcode: -1001, errmsg: `存在${errmsg}绑定关系,不能删除`, data: '' }; } await this.model.remove({ _id: id }); return { errcode: 0, errmsg: '', data: 'delete' }; } catch (error) { throw error; } } async query({ skip, limit, title, code, bind }) { const filter = {}; if (title || code || bind) filter.$or = []; if (title) filter.$or.push({ title: { $regex: title } }); if (code) filter.$or.push({ code: { $regex: code } }); if (bind) filter.$or.push({ bind: { $regex: bind } }); 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)); } else { res = await this.model.find({ ...filter }, { content: false }); } return { errcode: 0, errmsg: '', data: res, total: total.length }; } catch (error) { throw error; } } async fetch({ id, bind }) { try { // const filter = {}; // if (id || bind) filter.$or = []; // if (id) filter.$or.push({ _id: { $regex: id } }); // if (bind) filter.$or.push({ bind: { $regex: bind } }); let res; if (id) res = await this.model.findById(id); if (bind) res = await this.model.findOne({ bind }); 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 = PagesService;