@@ -12,7 +12,8 @@ const unpublishedPermission = require('../middleware/unpublishedPermission')
12
12
13
13
// 获取文章列表 / 按分类 / 按标签 筛选文章
14
14
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
16
17
if ( req . query . categoryId ) params . categoryId = req . query . categoryId
17
18
if ( req . query . tag ) params . tag = { $in : [ req . query . tag ] }
18
19
const limit = parseInt ( req . query . limit ) || 10
@@ -34,11 +35,13 @@ router.get('/api/front/article/gets', unpublishedPermission, async (req, res) =>
34
35
} )
35
36
36
37
// 获取文章详细信息
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 }
40
43
try {
41
- const detail = await db . article . find ( { publish, articleId } , project )
44
+ const detail = await db . article . find ( { publish : 1 , articleId } , project )
42
45
43
46
res . json ( {
44
47
status : 200 ,
@@ -173,7 +176,7 @@ router.get('/api/front/article/search', unpublishedPermission, async (req, res)
173
176
{ content_plain : { $regex : req . query . keyword , $options : 'i' } }
174
177
]
175
178
} ,
176
- { content : 0 }
179
+ { content : 0 , content_plain : 0 , content_draft : 0 }
177
180
)
178
181
. sort ( { _id : - 1 } )
179
182
. skip ( skip )
@@ -201,25 +204,6 @@ router.get('/api/front/article/search', unpublishedPermission, async (req, res)
201
204
} catch ( e ) {
202
205
res . status ( 500 ) . end ( )
203
206
}
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
- // }
223
207
} )
224
208
// 文章归档
225
209
router . get ( '/api/front/article/archives' , async ( req , res ) => {
@@ -308,17 +292,36 @@ router.get('/api/front/article/hot', (req, res) => {
308
292
309
293
/***********后台管理文章: 改动 删除 修改 TODO:待重构**************/
310
294
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
+ } )
311
312
// 存储文档
312
313
router . post ( '/api/admin/article/save' , confirmToken , async ( req , res ) => {
313
314
try {
314
315
const doc = await new db . article ( {
315
316
...req . body ,
316
317
content : '' ,
317
318
content_plain : '' ,
319
+ content_draft : '' ,
318
320
publish : 0 ,
319
321
commentNum : 0 ,
320
322
likeNum : 0 ,
321
323
pv : 0 ,
324
+ editing : 0 ,
322
325
createTime : new Date ( ) ,
323
326
updateTime : new Date ( )
324
327
} ) . save ( )
@@ -335,20 +338,37 @@ router.post('/api/admin/article/save', confirmToken, async (req, res) => {
335
338
336
339
// 编辑文档
337
340
router . patch ( '/api/admin/article/edit' , confirmToken , async ( req , res ) => {
338
- let content_plain = ''
341
+ const updates = { }
339
342
try {
340
- // 删除富文本中的标签等,保留静态文本字段
341
- if ( req . body . content ) content_plain = req . body . content . replace ( / < .* ?> | \n | & n b s p ; / g, '' )
343
+ if ( Reflect . get ( req . body , 'content' ) ) {
344
+ // 删除富文本中的标签等,保留静态文本字段
345
+ updates . content_plain = req . body . content . replace ( / < .* ?> | \n | & n b s p ; / 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
+
342
357
await db . article . update (
343
358
{ articleId : req . body . articleId } ,
344
359
{
345
360
...req . body ,
346
- content_plain ,
361
+ ... updates ,
347
362
updateTime : new Date ( )
348
363
}
349
364
)
365
+ const doc = await db . article . find (
366
+ { articleId : req . body . articleId } ,
367
+ { content : 0 , content_plain : 0 , content_draft : 0 }
368
+ )
350
369
res . json ( {
351
370
status : 200 ,
371
+ data : doc [ 0 ] ,
352
372
info : '文档编辑成功'
353
373
} )
354
374
} catch ( e ) {
@@ -357,20 +377,47 @@ router.patch('/api/admin/article/edit', confirmToken, async (req, res) => {
357
377
} )
358
378
359
379
// 删除文档
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
+ }
374
392
} )
375
393
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
+ } )
376
423
module . exports = router
0 commit comments