1
- ' use strict' ;
1
+ " use strict" ;
2
2
3
- const controller = require ( ' egg' ) . Controller ;
3
+ const controller = require ( " egg" ) . Controller ;
4
4
class ArticleListController extends controller {
5
-
6
5
async getArticleList ( ) {
7
6
const request = this . ctx . request . body ;
8
- let sql = '' ;
9
- let countSql = '' ;
10
- let result = '' ;
11
- let countResult = '' ;
12
- if ( request . type === '全部' ) {
7
+ let sql = "" ;
8
+ let countSql = "" ;
9
+ let result = "" ;
10
+ let countResult = "" ;
11
+ if ( request . type === "全部" ) {
13
12
sql = ` SELECT article.id as id, article.title as title, article.author as authorName,
14
13
article.reprinted as reprinted, article.introduce as introduce, article.introduce_img as introduceImg,
15
14
FROM_UNIXTIME(article.publish_time, '%Y-%m-%d %H:%i:%s') as publishTime,
16
15
article.view_count as viewCount, article_type.name as type
17
16
FROM article LEFT JOIN article_type ON article.type_id = article_type.id
18
17
WHERE article.is_publish = 1 ORDER BY article.publish_time DESC LIMIT ?,?` ;
19
- result = await this . app . mysql . query ( sql , [ request . offset , request . limit ] ) ;
20
- countSql = 'SELECT count(*) as total FROM article WHERE article.is_publish = 1' ;
18
+ result = await this . app . mysql . query ( sql , [ request . offset , request . limit ] ) ;
19
+ countSql =
20
+ "SELECT count(*) as total FROM article WHERE article.is_publish = 1" ;
21
21
countResult = await this . app . mysql . query ( countSql , request . type ) ;
22
- } else if ( request . type === ' 点击量' ) {
22
+ } else if ( request . type === " 点击量" ) {
23
23
sql = ` SELECT article.id as id, article.title as title, article.author as authorName,
24
24
article.reprinted as reprinted, article.introduce as introduce, article.introduce_img as introduceImg,
25
25
FROM_UNIXTIME(article.publish_time, '%Y-%m-%d %H:%i:%s') as publishTime,
26
26
article.view_count as viewCount, article_type.name as type
27
27
FROM article LEFT JOIN article_type ON article.type_id = article_type.id
28
28
WHERE article.is_publish = 1 ORDER BY article.view_count DESC LIMIT ?,?` ;
29
- result = await this . app . mysql . query ( sql , [ request . offset , request . limit ] ) ;
30
- countSql = 'SELECT count(*) as total FROM article WHERE article.is_publish = 1' ;
29
+ result = await this . app . mysql . query ( sql , [ request . offset , request . limit ] ) ;
30
+ countSql =
31
+ "SELECT count(*) as total FROM article WHERE article.is_publish = 1" ;
31
32
countResult = await this . app . mysql . query ( countSql , request . type ) ;
32
33
} else {
33
34
sql = ` SELECT article.id as id, article.title as title, article.author as authorName,
@@ -37,7 +38,11 @@ class ArticleListController extends controller {
37
38
FROM article LEFT JOIN article_type ON article.type_id = article_type.id
38
39
WHERE article.is_publish = 1 AND article_type.name = ?
39
40
ORDER BY article.publish_time DESC LIMIT ?,?` ;
40
- result = await this . app . mysql . query ( sql , [ request . type , request . offset , request . limit ] ) ;
41
+ result = await this . app . mysql . query ( sql , [
42
+ request . type ,
43
+ request . offset ,
44
+ request . limit ,
45
+ ] ) ;
41
46
countSql = `SELECT count(*) as total FROM article
42
47
LEFT JOIN article_type ON article.type_id = article_type.id
43
48
WHERE article.is_publish = 1
@@ -55,17 +60,43 @@ class ArticleListController extends controller {
55
60
} else {
56
61
this . ctx . body = {
57
62
success : true ,
58
- message : ' 获取文章列表失败' ,
63
+ message : " 获取文章列表失败" ,
59
64
} ;
60
65
}
61
66
}
62
67
63
- async getArticleTypes ( ) { // 获取所有的文章类别
64
- const result = await this . app . mysql . select ( 'article_type' ) ;
68
+ async getArticleTypes ( ) {
69
+ // 获取所有的文章类别
70
+ const result = await this . app . mysql . select ( "article_type" ) ;
65
71
if ( result . length > 0 ) {
66
72
this . ctx . body = { success : true , data : result } ;
67
73
}
68
74
}
75
+
76
+ async getLatestArticle ( ) {
77
+ const limit = this . ctx . params . limit ;
78
+ // 获取最近的几篇文章
79
+ const sql = ` SELECT article.id as id, article.title as title, article.author as authorName,
80
+ article.reprinted as reprinted, article.introduce as introduce, article.introduce_img as introduceImg,
81
+ FROM_UNIXTIME(article.publish_time, '%Y-%m-%d %H:%i:%s') as publishTime,
82
+ article.view_count as viewCount, article_type.name as type
83
+ FROM article LEFT JOIN article_type ON article.type_id = article_type.id
84
+ WHERE article.is_publish = 1 ORDER BY article.publish_time DESC LIMIT 0,3` ;
85
+ const result = await this . app . mysql . query ( sql , [ limit ] ) ;
86
+ if ( result . length > 0 ) {
87
+ this . ctx . body = {
88
+ success : true ,
89
+ data : {
90
+ list : result ,
91
+ } ,
92
+ } ;
93
+ } else {
94
+ this . ctx . body = {
95
+ success : true ,
96
+ message : "获取文章列表失败" ,
97
+ } ;
98
+ }
99
+ }
69
100
}
70
101
71
102
module . exports = ArticleListController ;
0 commit comments