9
9
reply : ReplyModel ,
10
10
user : UserModel ,
11
11
record : RecordModel ,
12
- sequelize
12
+ sequelize,
13
13
} = require ( '../models' )
14
14
15
15
const fs = require ( 'fs' )
@@ -25,7 +25,7 @@ class ArticleController {
25
25
ArticleModel . create ( {
26
26
id : - 1 ,
27
27
title : '关于页面' ,
28
- content : '关于页面存档,勿删'
28
+ content : '关于页面存档,勿删' ,
29
29
} )
30
30
}
31
31
}
@@ -39,7 +39,7 @@ class ArticleController {
39
39
categoryList : Joi . array ( ) ,
40
40
tagList : Joi . array ( ) ,
41
41
type : Joi . boolean ( ) ,
42
- top : Joi . boolean ( )
42
+ top : Joi . boolean ( ) ,
43
43
} )
44
44
45
45
if ( validator ) {
@@ -65,7 +65,7 @@ class ArticleController {
65
65
{ ...ctx . params , ...ctx . query } ,
66
66
{
67
67
id : Joi . number ( ) . required ( ) ,
68
- type : Joi . number ( ) // type 用于区分是否增加浏览次数 1 新增浏览次数 0 不新增
68
+ type : Joi . number ( ) , // type 用于区分是否增加浏览次数 1 新增浏览次数 0 不新增
69
69
}
70
70
)
71
71
if ( validator ) {
@@ -82,22 +82,22 @@ class ArticleController {
82
82
{
83
83
model : ReplyModel ,
84
84
attributes : [ 'id' , 'content' , 'createdAt' ] ,
85
- include : [ { model : UserModel , as : 'user' , attributes : { exclude : [ 'updatedAt' , 'password' ] } } ]
85
+ include : [ { model : UserModel , as : 'user' , attributes : { exclude : [ 'updatedAt' , 'password' ] } } ] ,
86
86
} ,
87
- { model : UserModel , as : 'user' , attributes : { exclude : [ 'updatedAt' , 'password' ] } }
87
+ { model : UserModel , as : 'user' , attributes : { exclude : [ 'updatedAt' , 'password' ] } } ,
88
88
] ,
89
- row : true
90
- }
89
+ row : true ,
90
+ } ,
91
91
] ,
92
92
order : [ [ CommentModel , 'createdAt' , 'DESC' ] , [ [ CommentModel , ReplyModel , 'createdAt' , 'ASC' ] ] ] , // comment model order
93
- row : true
93
+ row : true ,
94
94
} )
95
95
96
96
const { type = 1 } = ctx . query
97
97
// viewer count ++
98
98
type === 1 && ArticleModel . update ( { viewCount : ++ data . viewCount } , { where : { id : ctx . params . id } } )
99
99
// 每个浏览记录都存一个stamp,这样后续能够看出文章的阅读趋势方便推荐
100
- type === 1 && RecordModel . create ( { articleId : ctx . params . id } )
100
+ type === 1 && RecordModel . create ( { articleId : ctx . params . id } )
101
101
// JSON.parse(github)
102
102
data . comments . forEach ( comment => {
103
103
comment . user . github = JSON . parse ( comment . user . github )
@@ -119,7 +119,7 @@ class ArticleController {
119
119
tag : Joi . string ( ) ,
120
120
preview : Joi . number ( ) ,
121
121
order : Joi . string ( ) ,
122
- type : Joi . boolean ( )
122
+ type : Joi . boolean ( ) ,
123
123
} )
124
124
125
125
if ( validator ) {
@@ -135,36 +135,36 @@ class ArticleController {
135
135
const data = await ArticleModel . findAndCountAll ( {
136
136
where : {
137
137
id : {
138
- $not : - 1 // 过滤关于页面的副本
138
+ $not : - 1 , // 过滤关于页面的副本
139
139
} ,
140
140
$and : {
141
141
type : {
142
- $eq : JSON . parse ( type )
143
- }
142
+ $eq : JSON . parse ( type ) ,
143
+ } ,
144
144
} ,
145
145
$or : {
146
146
title : {
147
- $like : `%${ keyword } %`
147
+ $like : `%${ keyword } %` ,
148
148
} ,
149
149
content : {
150
- $like : `%${ keyword } %`
151
- }
152
- }
150
+ $like : `%${ keyword } %` ,
151
+ } ,
152
+ } ,
153
153
} ,
154
154
include : [
155
155
{ model : TagModel , attributes : [ 'name' ] , where : tagFilter } ,
156
156
{ model : CategoryModel , attributes : [ 'name' ] , where : categoryFilter } ,
157
157
{
158
158
model : CommentModel ,
159
159
attributes : [ 'id' ] ,
160
- include : [ { model : ReplyModel , attributes : [ 'id' ] } ]
161
- }
160
+ include : [ { model : ReplyModel , attributes : [ 'id' ] } ] ,
161
+ } ,
162
162
] ,
163
163
offset : ( page - 1 ) * pageSize ,
164
164
limit : parseInt ( pageSize ) ,
165
165
order : articleOrder ,
166
166
row : true ,
167
- distinct : true // count 计算
167
+ distinct : true , // count 计算
168
168
} )
169
169
if ( preview === 1 ) {
170
170
data . rows . forEach ( d => {
@@ -177,31 +177,31 @@ class ArticleController {
177
177
const data = await ArticleModel . findAndCountAll ( {
178
178
where : {
179
179
id : {
180
- $not : - 1 // 过滤关于页面的副本
180
+ $not : - 1 , // 过滤关于页面的副本
181
181
} ,
182
182
$or : {
183
183
title : {
184
- $like : `%${ keyword } %`
184
+ $like : `%${ keyword } %` ,
185
185
} ,
186
186
content : {
187
- $like : `%${ keyword } %`
188
- }
189
- }
187
+ $like : `%${ keyword } %` ,
188
+ } ,
189
+ } ,
190
190
} ,
191
191
include : [
192
192
{ model : TagModel , attributes : [ 'name' ] , where : tagFilter } ,
193
193
{ model : CategoryModel , attributes : [ 'name' ] , where : categoryFilter } ,
194
194
{
195
195
model : CommentModel ,
196
196
attributes : [ 'id' ] ,
197
- include : [ { model : ReplyModel , attributes : [ 'id' ] } ]
198
- }
197
+ include : [ { model : ReplyModel , attributes : [ 'id' ] } ] ,
198
+ } ,
199
199
] ,
200
200
offset : ( page - 1 ) * pageSize ,
201
201
limit : parseInt ( pageSize ) ,
202
202
order : articleOrder ,
203
203
row : true ,
204
- distinct : true // count 计算
204
+ distinct : true , // count 计算
205
205
} )
206
206
if ( preview === 1 ) {
207
207
data . rows . forEach ( d => {
@@ -212,15 +212,14 @@ class ArticleController {
212
212
ctx . body = data
213
213
}
214
214
}
215
-
216
215
}
217
216
218
217
// 修改文章
219
218
static async update ( ctx ) {
220
219
const validator = ctx . validate (
221
220
{
222
221
articleId : ctx . params . id ,
223
- ...ctx . request . body
222
+ ...ctx . request . body ,
224
223
} ,
225
224
{
226
225
articleId : Joi . number ( ) . required ( ) ,
@@ -229,27 +228,27 @@ class ArticleController {
229
228
categories : Joi . array ( ) ,
230
229
tags : Joi . array ( ) ,
231
230
type : Joi . boolean ( ) ,
232
- top : Joi . boolean ( )
231
+ top : Joi . boolean ( ) ,
233
232
}
234
233
)
235
234
if ( validator ) {
236
235
const { title, content, categories = [ ] , tags = [ ] , type, top } = ctx . request . body
237
236
const articleId = parseInt ( ctx . params . id )
238
237
const tagList = tags . map ( tag => ( { name : tag , articleId } ) )
239
238
const categoryList = categories . map ( cate => ( { name : cate , articleId } ) )
240
- await ArticleModel . update ( { title, content, type, top} , { where : { id : articleId } } )
239
+ await ArticleModel . update ( { title, content, type, top } , { where : { id : articleId } } )
241
240
await TagModel . destroy ( { where : { articleId } } )
242
241
await TagModel . bulkCreate ( tagList )
243
242
await CategoryModel . destroy ( { where : { articleId } } )
244
243
await CategoryModel . bulkCreate ( categoryList )
245
- ctx . body = { ' type' : type }
244
+ ctx . body = { type : type }
246
245
}
247
246
}
248
247
249
248
// 删除文章
250
249
static async delete ( ctx ) {
251
250
const validator = ctx . validate ( ctx . params , {
252
- id : Joi . number ( ) . required ( )
251
+ id : Joi . number ( ) . required ( ) ,
253
252
} )
254
253
if ( validator ) {
255
254
const articleId = ctx . params . id
@@ -269,7 +268,7 @@ class ArticleController {
269
268
// 删除多个文章
270
269
static async delList ( ctx ) {
271
270
const validator = ctx . validate ( ctx . params , {
272
- list : Joi . string ( ) . required ( )
271
+ list : Joi . string ( ) . required ( ) ,
273
272
} )
274
273
275
274
if ( validator ) {
@@ -295,7 +294,7 @@ class ArticleController {
295
294
*/
296
295
static async checkExist ( ctx ) {
297
296
const validator = ctx . validate ( ctx . request . body , {
298
- fileNameList : Joi . array ( ) . required ( )
297
+ fileNameList : Joi . array ( ) . required ( ) ,
299
298
} )
300
299
301
300
if ( validator ) {
@@ -343,7 +342,7 @@ class ArticleController {
343
342
static async uploadConfirm ( ctx ) {
344
343
const validator = ctx . validate ( ctx . request . body , {
345
344
authorId : Joi . number ( ) ,
346
- uploadList : Joi . array ( )
345
+ uploadList : Joi . array ( ) ,
347
346
} )
348
347
if ( validator ) {
349
348
const { uploadList, authorId } = ctx . request . body
@@ -364,7 +363,7 @@ class ArticleController {
364
363
categories : categories . map ( d => ( { name : d } ) ) ,
365
364
tags : tags . map ( d => ( { name : d } ) ) ,
366
365
content,
367
- authorId
366
+ authorId,
368
367
}
369
368
if ( date ) data . createdAt = date
370
369
if ( item . articleId ) data . articleId = item . articleId
@@ -400,7 +399,7 @@ class ArticleController {
400
399
// 导出文章
401
400
static async output ( ctx ) {
402
401
const validator = ctx . validate ( ctx . params , {
403
- id : Joi . number ( ) . required ( )
402
+ id : Joi . number ( ) . required ( ) ,
404
403
} )
405
404
406
405
if ( validator ) {
@@ -409,8 +408,8 @@ class ArticleController {
409
408
include : [
410
409
// 查找 分类
411
410
{ model : TagModel , attributes : [ 'name' ] } ,
412
- { model : CategoryModel , attributes : [ 'name' ] }
413
- ]
411
+ { model : CategoryModel , attributes : [ 'name' ] } ,
412
+ ] ,
414
413
} )
415
414
416
415
const { filePath, fileName } = await generateFile ( article )
@@ -421,20 +420,20 @@ class ArticleController {
421
420
422
421
static async outputList ( ctx ) {
423
422
const validator = ctx . validate ( ctx . params , {
424
- list : Joi . string ( ) . required ( )
423
+ list : Joi . string ( ) . required ( ) ,
425
424
} )
426
425
if ( validator ) {
427
426
const articleList = ctx . params . list . split ( ',' )
428
427
429
428
const list = await ArticleModel . findAll ( {
430
429
where : {
431
- id : articleList
430
+ id : articleList ,
432
431
} ,
433
432
include : [
434
433
// 查找 分类
435
434
{ model : TagModel , attributes : [ 'name' ] } ,
436
- { model : CategoryModel , attributes : [ 'name' ] }
437
- ]
435
+ { model : CategoryModel , attributes : [ 'name' ] } ,
436
+ ] ,
438
437
} )
439
438
440
439
// const filePath = await generateFile(list[0])
@@ -447,7 +446,7 @@ class ArticleController {
447
446
zip . pipe ( zipStream )
448
447
list . forEach ( item => {
449
448
zip . append ( fs . createReadStream ( `${ outputPath } /${ item . title } .md` ) , {
450
- name : `${ item . title } .md` // 压缩文件名
449
+ name : `${ item . title } .md` , // 压缩文件名
451
450
} )
452
451
} )
453
452
await zip . finalize ( )
@@ -461,14 +460,14 @@ class ArticleController {
461
460
const list = await ArticleModel . findAll ( {
462
461
where : {
463
462
id : {
464
- $not : - 1 // 过滤关于页面的副本
465
- }
463
+ $not : - 1 , // 过滤关于页面的副本
464
+ } ,
466
465
} ,
467
466
include : [
468
467
// 查找 分类
469
468
{ model : TagModel , attributes : [ 'name' ] } ,
470
- { model : CategoryModel , attributes : [ 'name' ] }
471
- ]
469
+ { model : CategoryModel , attributes : [ 'name' ] } ,
470
+ ] ,
472
471
} )
473
472
474
473
// const filePath = await generateFile(list[0])
@@ -481,7 +480,7 @@ class ArticleController {
481
480
zip . pipe ( zipStream )
482
481
list . forEach ( item => {
483
482
zip . append ( fs . createReadStream ( `${ outputPath } /${ item . title } .md` ) , {
484
- name : `${ item . title } .md` // 压缩文件名
483
+ name : `${ item . title } .md` , // 压缩文件名
485
484
} )
486
485
} )
487
486
await zip . finalize ( )
0 commit comments