|
@@ -4,7 +4,7 @@ const Service = require('egg').Service;
|
|
|
const assert = require('assert');
|
|
|
const moment = require('moment');
|
|
|
class ContentService extends Service {
|
|
|
- async create({ title, thumbnail, annex, content, istop, menus, date, annexname, term }) {
|
|
|
+ async create({ title, thumbnail, annex, content, istop, menus, date, annexname, term, sort }) {
|
|
|
assert(title, '标题不存在');
|
|
|
assert(thumbnail, '缩略图不存在');
|
|
|
assert(content, '内容不存在');
|
|
@@ -15,21 +15,21 @@ class ContentService extends Service {
|
|
|
const slug = text.substring(0, 200);
|
|
|
try {
|
|
|
const year = date.slice(0, 4) + '年';
|
|
|
- await model.create({ title, slug, thumbnail, annex, content, istop, menus, createAt, year, date, annexname, term, hits: 0 });
|
|
|
+ await model.create({ title, slug, thumbnail, annex, content, istop, menus, createAt, year, date, annexname, term, hits: 0, sort });
|
|
|
return { errmsg: '', errcode: 0 };
|
|
|
} catch (error) {
|
|
|
throw new Error('添加失败');
|
|
|
}
|
|
|
}
|
|
|
- async update({ title, thumbnail, annex, content, istop, menus, date, annexname, term, _id }) {
|
|
|
+ async update({ title, thumbnail, annex, content, istop, menus, date, annexname, term, _id, sort }) {
|
|
|
assert(_id, 'id不存在');
|
|
|
const { Content: model } = this.ctx.model;
|
|
|
let text = content.replace(new RegExp('</?[^>]+>', 'gm'), '');
|
|
|
text = text.replace('\\s*|\t|\r|\n', '');
|
|
|
const slug = text.substring(0, 200);
|
|
|
- const year = date.slice(0, 3) + '年';
|
|
|
+ const year = date.slice(0, 4) + '年';
|
|
|
try {
|
|
|
- await model.findById(_id).update({ title, slug, thumbnail, annex, content, istop, menus, year, date, annexname, term });
|
|
|
+ await model.findById(_id).update({ title, slug, thumbnail, annex, content, istop, menus, year, date, annexname, term, sort });
|
|
|
return { errmsg: '', errcode: 0 };
|
|
|
} catch (error) {
|
|
|
throw new Error('修改失败');
|
|
@@ -72,9 +72,9 @@ class ContentService extends Service {
|
|
|
total = await model.find({ ...filter });
|
|
|
if (skip && limit) {
|
|
|
res = await model.find({ ...filter }, { content: false }).skip(Number(skip) * Number(limit)).limit(Number(limit))
|
|
|
- .sort({ date: -1 });
|
|
|
+ .sort({ sort: -1, createAt: -1 });
|
|
|
} else {
|
|
|
- res = await model.find({ ...filter }, { content: false }).sort({ date: -1 });
|
|
|
+ res = await model.find({ ...filter }, { content: false }).sort({ sort: -1, createAt: -1 });
|
|
|
}
|
|
|
return { errmsg: '', errcode: 0, data: res, total: total.length };
|
|
|
} catch (error) {
|