Skip to content

Commit 49a7195

Browse files
committed
fix: some api modules
1 parent c118736 commit 49a7195

File tree

5 files changed

+133
-68
lines changed

5 files changed

+133
-68
lines changed

front/server/api/articles.js

Lines changed: 90 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const unpublishedPermission = require('../middleware/unpublishedPermission')
1212

1313
// 获取文章列表 / 按分类 / 按标签 筛选文章
1414
router.get('/api/front/article/gets', unpublishedPermission, async (req, res) => {
15-
const params = { publish: req.query.publish }
15+
const params = {}
16+
if (req.query.publish) params.publish = req.query.publish
1617
if (req.query.categoryId) params.categoryId = req.query.categoryId
1718
if (req.query.tag) params.tag = { $in: [req.query.tag] }
1819
const limit = parseInt(req.query.limit) || 10
@@ -34,11 +35,13 @@ router.get('/api/front/article/gets', unpublishedPermission, async (req, res) =>
3435
})
3536

3637
// 获取文章详细信息
37-
router.get('/api/front/article/detail', unpublishedPermission, async (req, res) => {
38-
const { publish, articleId, excludeContent } = req.query
39-
const project = excludeContent ? { content: 0, content_plain: 0 } : { content_plain: 0 }
38+
router.get('/api/front/article/detail', async (req, res) => {
39+
const { articleId, excludeContent } = req.query
40+
const project = excludeContent
41+
? { content: 0, content_plain: 0, content_draft: 0 }
42+
: { content_plain: 0, content_draft: 0 }
4043
try {
41-
const detail = await db.article.find({ publish, articleId }, project)
44+
const detail = await db.article.find({ publish: 1, articleId }, project)
4245

4346
res.json({
4447
status: 200,
@@ -173,7 +176,7 @@ router.get('/api/front/article/search', unpublishedPermission, async (req, res)
173176
{ content_plain: { $regex: req.query.keyword, $options: 'i' } }
174177
]
175178
},
176-
{ content: 0 }
179+
{ content: 0, content_plain: 0, content_draft: 0 }
177180
)
178181
.sort({ _id: -1 })
179182
.skip(skip)
@@ -201,25 +204,6 @@ router.get('/api/front/article/search', unpublishedPermission, async (req, res)
201204
} catch (e) {
202205
res.status(500).end()
203206
}
204-
205-
////
206-
// if (req.query.according === 'key') {
207-
// //前台时间轴根据时间范围搜索
208-
// } else {
209-
// const start = new Date(parseInt(req.query.start))
210-
// const end = new Date(parseInt(req.query.end))
211-
// db.article
212-
// .find({ publish: req.query.publish, date: { $gte: start, $lte: end } }, { content: 0 }, (err, doc) => {
213-
// if (err) {
214-
// res.status(500).end()
215-
// } else {
216-
// res.json(doc)
217-
// }
218-
// })
219-
// .sort({ _id: -1 })
220-
// .skip(skip)
221-
// .limit(limit)
222-
// }
223207
})
224208
// 文章归档
225209
router.get('/api/front/article/archives', async (req, res) => {
@@ -308,17 +292,36 @@ router.get('/api/front/article/hot', (req, res) => {
308292

309293
/***********后台管理文章: 改动 删除 修改 TODO:待重构**************/
310294

295+
// 获取文章详细信息
296+
router.get('/api/admin/article/getDraft', unpublishedPermission, async (req, res) => {
297+
const { articleId, excludeContent } = req.query
298+
const project = excludeContent ? { content: 0, content_plain: 0, content_draft: 0 } : { content_plain: 0 }
299+
try {
300+
const detail = await db.article.find({ articleId }, project)
301+
const doc = detail[0].toObject()
302+
doc.content = doc.content_draft
303+
delete doc.content_draft
304+
res.json({
305+
status: 200,
306+
data: doc || {}
307+
})
308+
} catch (e) {
309+
res.status(500).end()
310+
}
311+
})
311312
// 存储文档
312313
router.post('/api/admin/article/save', confirmToken, async (req, res) => {
313314
try {
314315
const doc = await new db.article({
315316
...req.body,
316317
content: '',
317318
content_plain: '',
319+
content_draft: '',
318320
publish: 0,
319321
commentNum: 0,
320322
likeNum: 0,
321323
pv: 0,
324+
editing: 0,
322325
createTime: new Date(),
323326
updateTime: new Date()
324327
}).save()
@@ -335,20 +338,37 @@ router.post('/api/admin/article/save', confirmToken, async (req, res) => {
335338

336339
// 编辑文档
337340
router.patch('/api/admin/article/edit', confirmToken, async (req, res) => {
338-
let content_plain = ''
341+
const updates = {}
339342
try {
340-
// 删除富文本中的标签等,保留静态文本字段
341-
if (req.body.content) content_plain = req.body.content.replace(/<.*?>|\n|&nbsp;/g, '')
343+
if (Reflect.get(req.body, 'content')) {
344+
// 删除富文本中的标签等,保留静态文本字段
345+
updates.content_plain = req.body.content.replace(/<.*?>|\n|&nbsp;/g, '')
346+
// 默认存储为草稿
347+
updates.content_draft = req.body.content
348+
// 重要!一定要删除!
349+
delete req.body.content
350+
351+
// 文档 发布/更新 content 与 content_draft保持一致同步
352+
if (req.body.editing === '0') {
353+
updates.content = updates.content_draft
354+
}
355+
}
356+
342357
await db.article.update(
343358
{ articleId: req.body.articleId },
344359
{
345360
...req.body,
346-
content_plain,
361+
...updates,
347362
updateTime: new Date()
348363
}
349364
)
365+
const doc = await db.article.find(
366+
{ articleId: req.body.articleId },
367+
{ content: 0, content_plain: 0, content_draft: 0 }
368+
)
350369
res.json({
351370
status: 200,
371+
data: doc[0],
352372
info: '文档编辑成功'
353373
})
354374
} catch (e) {
@@ -357,20 +377,47 @@ router.patch('/api/admin/article/edit', confirmToken, async (req, res) => {
357377
})
358378

359379
// 删除文档
360-
router.delete('/api/admin/article/del', confirmToken, (req, res) => {
361-
//$in是为了批量删除,出入的articleId是数组
362-
db.article.remove({ articleId: { $in: req.query.articleId } }, (err) => {
363-
if (err) {
364-
res.status(500).end()
365-
} else {
366-
res.json({ deleteCode: 200 })
367-
db.comment.remove({ articleId: { $in: req.query.articleId } }, (err) => {
368-
if (err) {
369-
console.log(err)
370-
}
371-
})
372-
}
373-
})
380+
router.delete('/api/admin/article/del', confirmToken, async (req, res) => {
381+
try {
382+
const params = typeof req.query.id === 'string' ? { _id: req.query.id } : { _id: { $in: req.query.id } }
383+
// $in 批量删除
384+
await db.article.remove(params)
385+
res.json({
386+
status: 200,
387+
info: '删除成功'
388+
})
389+
} catch (e) {
390+
res.status(500).end()
391+
}
374392
})
375393

394+
// 筛选文章
395+
router.get('/api/admin/article/search', confirmToken, async (req, res) => {
396+
const limit = 8
397+
const skip = req.query.page * limit - limit
398+
try {
399+
const searchDoc = await db.article
400+
.find(
401+
{
402+
...req.query,
403+
$or: [
404+
{ title: { $regex: req.query.keyword, $options: 'i' } },
405+
{ content_plain: { $regex: req.query.keyword, $options: 'i' } }
406+
]
407+
},
408+
{ content: 0, content_plain: 0, content_draft: 0 }
409+
)
410+
.sort({ _id: -1 })
411+
.skip(skip)
412+
.limit(limit)
413+
414+
res.json({
415+
status: 200,
416+
data: searchDoc,
417+
info: '搜索成功'
418+
})
419+
} catch (e) {
420+
res.status(500).end()
421+
}
422+
})
376423
module.exports = router

front/server/api/comments.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ router.get('/api/front/comments/get', async (req, res) => {
3434
{ $set: { liked: 0 } }
3535
])
3636
let ids = doc
37-
.map(item => {
37+
.map((item) => {
3838
if (item.reply && item.reply.length) {
39-
return item.reply.map(erp => erp._id.toString()).concat(item._id.toString())
39+
return item.reply.map((erp) => erp._id.toString()).concat(item._id.toString())
4040
}
4141
return item._id.toString()
4242
})
4343
.flat()
4444
const existed = await db.commentIp.find({ ip, type: 1, like: 1, msgid: { $in: ids } })
4545
if (existed.length) {
46-
const ipIds = existed.map(ee => ee.msgid.toString())
47-
doc.forEach(d => {
46+
const ipIds = existed.map((ee) => ee.msgid.toString())
47+
doc.forEach((d) => {
4848
if (ipIds.includes(d._id.toString())) d.liked = 1
4949
if (d.reply && d.reply.length) {
50-
d.reply.forEach(er => {
50+
d.reply.forEach((er) => {
5151
if (ipIds.includes(er._id.toString())) er.liked = 1
5252
})
5353
}
@@ -69,11 +69,7 @@ router.get('/api/front/comments/new', async (req, res) => {
6969
const limit = parseInt(req.query.limit) || 10
7070
const skip = req.query.page * limit - limit
7171
try {
72-
const doc = await db
73-
.comment({})
74-
.sort({ _id: -1 })
75-
.skip(skip)
76-
.limit(limit)
72+
const doc = await db.comment({}).sort({ _id: -1 }).skip(skip).limit(limit)
7773
res.json({
7874
status: 200,
7975
data: doc,
@@ -86,9 +82,17 @@ router.get('/api/front/comments/new', async (req, res) => {
8682
//添加文章评论
8783
router.post('/api/front/comments/save', async (req, res) => {
8884
try {
85+
const { articleId, name, imgUrl, email, link, content, parentId, aite } = req.body
8986
// 存储文章评论
9087
const commentDoc = await new db.comment({
91-
...req.body,
88+
articleId,
89+
name,
90+
imgUrl,
91+
email,
92+
link,
93+
content,
94+
parentId,
95+
aite,
9296
like: 0,
9397
date: new Date()
9498
}).save()
@@ -208,13 +212,13 @@ router.get('/api/getAdminComments', confirmToken, (req, res) => {
208212
})
209213
//后台管理删除评论 TODO: 前端进行吧区分一二级的逻辑去掉
210214
router.delete('/api/removeComments', confirmToken, (req, res) => {
211-
db.comment.remove({ _id: { $in: req.query.id } }, err => {
215+
db.comment.remove({ _id: { $in: req.query.id } }, (err) => {
212216
if (err) {
213217
res.status(500).end()
214218
} else {
215219
//删除一级评论,联动文章评论数
216220
if (req.query.level === 1) {
217-
db.article.update({ articleId: req.query.articleId }, { $inc: { commentNum: -1 } }, (err, doc) => {
221+
db.article.update({ articleId: req.query.articleId }, { $inc: { commentNum: -1 } }, (err) => {
218222
if (err) {
219223
res.status(500)
220224
}

front/server/api/msgBoard.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ router.get('/api/front/messageBoard/gets', async (req, res) => {
3232
{ $set: { liked: 0 } }
3333
])
3434
let ids = doc
35-
.map(item => {
35+
.map((item) => {
3636
if (item.reply && item.reply.length) {
37-
return item.reply.map(erp => erp._id.toString()).concat(item._id.toString())
37+
return item.reply.map((erp) => erp._id.toString()).concat(item._id.toString())
3838
}
3939
return item._id.toString()
4040
})
@@ -43,12 +43,12 @@ router.get('/api/front/messageBoard/gets', async (req, res) => {
4343
const existed = await db.commentIp.find({ ip, type: 0, like: 1, msgid: { $in: ids } })
4444

4545
if (existed.length) {
46-
const ipIds = existed.map(ee => ee.msgid.toString())
47-
doc.forEach(d => {
46+
const ipIds = existed.map((ee) => ee.msgid.toString())
47+
doc.forEach((d) => {
4848
if (ipIds.includes(d._id.toString())) d.liked = 1
4949

5050
if (d.reply && d.reply.length) {
51-
d.reply.forEach(er => {
51+
d.reply.forEach((er) => {
5252
if (ipIds.includes(er._id.toString())) er.liked = 1
5353
})
5454
}
@@ -145,8 +145,15 @@ router.get('/api/getAdminBoard', confirmToken, (req, res) => {
145145
// 留言存储
146146
router.post('/api/front/messageBoard/save', async (req, res) => {
147147
try {
148+
const { name, imgUrl, email, link, content, parentId, aite } = req.body
148149
const doc = await new db.msgBoard({
149-
...req.body,
150+
name,
151+
imgUrl,
152+
email,
153+
link,
154+
content,
155+
parentId,
156+
aite,
150157
like: 0
151158
}).save()
152159
res.json({
@@ -180,7 +187,7 @@ router.post('/api/front/messageBoard/save', async (req, res) => {
180187

181188
//后台管理删除二级留言
182189
router.patch('/api/reduceLeavewords', confirmToken, (req, res) => {
183-
db.msgBoard.update({ _id: req.body.mainId }, { $pull: { reply: { _id: req.body.secondId } } }, (err, doc) => {
190+
db.msgBoard.update({ _id: req.body.mainId }, { $pull: { reply: { _id: req.body.secondId } } }, (err) => {
184191
if (err) {
185192
res.status(500).end()
186193
} else {
@@ -190,7 +197,7 @@ router.patch('/api/reduceLeavewords', confirmToken, (req, res) => {
190197
})
191198
router.delete('/api/removeLeavewords', confirmToken, (req, res) => {
192199
//因为用到批量删除,所以删除项的_id均放到数组中
193-
db.msgBoard.remove({ _id: { $in: req.query.id } }, err => {
200+
db.msgBoard.remove({ _id: { $in: req.query.id } }, (err) => {
194201
if (err) {
195202
res.status(500).end()
196203
} else {

front/server/db/schema.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,16 @@ const articleSchema = new mongoose.Schema({
4444
title: 'string',
4545
abstract: 'string',
4646
content: 'string',
47+
// 当前最新编辑内容,当文档发布后,与content保持严格一致
48+
content_draft: 'string',
4749
content_plain: 'string',
4850
headerPic: 'string',
51+
// 是否发布 0:未发布 1:发布
52+
// 新建文档初始值为 0
53+
// 文档编辑阶段 可保存,可发布/更新;
4954
publish: 'number',
55+
// 是否存在未经发布的编辑内容 0:不存在(即已发布) 1:存在最新编辑内容
56+
editing: 'number',
5057
tag: 'array',
5158
commentNum: 'number',
5259
likeNum: 'number',

front/src/views/messageBoard/index.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default {
6262
messages: []
6363
}
6464
},
65-
async asyncData({ route }) {
65+
async asyncData() {
6666
const msgRes = await api.getMessageBoard({
6767
page: 1
6868
})
@@ -121,14 +121,14 @@ export default {
121121
})
122122
if (likeRes.status === 200) {
123123
let finder
124-
this.messages.some(msg => {
124+
this.messages.some((msg) => {
125125
if (msg._id === likeRes.data._id) {
126126
finder = msg
127127
return true
128128
}
129129
if (msg.reply && msg.reply.length) {
130130
let done = false
131-
msg.reply.some(er => {
131+
msg.reply.some((er) => {
132132
if (er._id === likeRes.data._id) {
133133
finder = er
134134
done = true
@@ -147,7 +147,7 @@ export default {
147147
})
148148
}
149149
},
150-
currentChange(val) {
150+
currentChange() {
151151
this.getMessageBoard()
152152
}
153153
}

0 commit comments

Comments
 (0)