@@ -36,7 +36,7 @@ router.get('/api/front/article/gets', unpublishedPermission, async (req, res) =>
36
36
// 获取文章详细信息
37
37
router . get ( '/api/front/article/detail' , unpublishedPermission , async ( req , res ) => {
38
38
const { publish, articleId, excludeContent } = req . query
39
- const project = excludeContent ? { content : 0 , content_plain : 0 } : { }
39
+ const project = excludeContent ? { content : 0 , content_plain : 0 } : { content_plain : 0 }
40
40
try {
41
41
const detail = await db . article . find ( { publish, articleId } , project )
42
42
@@ -46,7 +46,7 @@ router.get('/api/front/article/detail', unpublishedPermission, async (req, res)
46
46
} )
47
47
48
48
// 获取访客信息
49
- if ( process . env . NODEW_ENV === 'production' ) {
49
+ if ( process . env . NODE_ENV === 'production' ) {
50
50
// 更新pv
51
51
await db . article . update ( { articleId } , { $inc : { pv : 1 } } )
52
52
const ipInfo = await api . get ( 'https://ip.help.bj.cn' , { ip : getIp ( req ) } )
@@ -308,45 +308,55 @@ router.get('/api/front/article/hot', (req, res) => {
308
308
309
309
/***********后台管理文章: 改动 删除 修改 TODO:待重构**************/
310
310
311
- // 修改文章
312
- router . patch ( '/api/admin/article/update' , confirmToken , ( req , res ) => {
313
- const { publish, original, title, abstract, tag, content } = req . body
314
- db . article . update (
315
- { articleId : req . body . articleId } ,
316
- { publish, original, title, abstract, tag, content } ,
317
- ( err , doc ) => {
318
- if ( err ) {
319
- res . status ( 500 ) . end ( )
320
- } else {
321
- res . json ( { code : 200 } )
322
- }
323
- }
324
- )
311
+ // 存储文档
312
+ router . post ( '/api/admin/article/save' , confirmToken , async ( req , res ) => {
313
+ try {
314
+ const doc = await new db . article ( {
315
+ ...req . body ,
316
+ content : '' ,
317
+ content_plain : '' ,
318
+ publish : 0 ,
319
+ commentNum : 0 ,
320
+ likeNum : 0 ,
321
+ pv : 0 ,
322
+ createTime : new Date ( ) ,
323
+ updateTime : new Date ( )
324
+ } ) . save ( )
325
+
326
+ res . json ( {
327
+ status : 200 ,
328
+ data : doc ,
329
+ info : '文档存储成功'
330
+ } )
331
+ } catch ( e ) {
332
+ res . status ( 500 ) . end ( )
333
+ }
325
334
} )
326
- // 存储文章
327
- router . post ( '/api/admin/article/save' , confirmToken , ( req , res ) => {
328
- const { original , title , abstract , content , tag , publish , date } = req . body
329
- new db . article ( {
330
- articleId : 0 ,
331
- original ,
332
- title ,
333
- abstract ,
334
- content ,
335
- tag ,
336
- publish ,
337
- date : date ,
338
- commentNum : 0 ,
339
- likeNum : 0 ,
340
- pv : 0
341
- } ) . save ( ( err , doc ) => {
342
- if ( err ) {
343
- res . json ( { code : 500 } )
344
- } else {
345
- res . json ( { code : 200 } )
346
- }
347
- } )
335
+
336
+ // 编辑文档
337
+ router . patch ( '/api/admin/article/edit' , confirmToken , async ( req , res ) => {
338
+ let content_plain = ''
339
+ try {
340
+ // 删除富文本中的标签等,保留静态文本字段
341
+ if ( req . body . content ) content_plain = req . body . content . replace ( / < . * ? > | \n | & n b s p ; / g , '' )
342
+ await db . article . update (
343
+ { articleId : req . body . articleId } ,
344
+ {
345
+ ... req . body ,
346
+ content_plain ,
347
+ updateTime : new Date ( )
348
+ }
349
+ )
350
+ res . json ( {
351
+ status : 200 ,
352
+ info : '文档编辑成功'
353
+ } )
354
+ } catch ( e ) {
355
+ res . status ( 500 ) . end ( )
356
+ }
348
357
} )
349
- // 删除文章
358
+
359
+ // 删除文档
350
360
router . delete ( '/api/admin/article/del' , confirmToken , ( req , res ) => {
351
361
//$in是为了批量删除,出入的articleId是数组
352
362
db . article . remove ( { articleId : { $in : req . query . articleId } } , ( err ) => {
0 commit comments