1- ' use strict' ;
1+ " use strict" ;
22
3- const controller = require ( ' egg' ) . Controller ;
3+ const controller = require ( " egg" ) . Controller ;
44class ArticleListController extends controller {
5-
65 async getArticleList ( ) {
76 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 === "全部" ) {
1312 sql = ` SELECT article.id as id, article.title as title, article.author as authorName,
1413 article.reprinted as reprinted, article.introduce as introduce, article.introduce_img as introduceImg,
1514 FROM_UNIXTIME(article.publish_time, '%Y-%m-%d %H:%i:%s') as publishTime,
1615 article.view_count as viewCount, article_type.name as type
1716 FROM article LEFT JOIN article_type ON article.type_id = article_type.id
1817 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" ;
2121 countResult = await this . app . mysql . query ( countSql , request . type ) ;
22- } else if ( request . type === ' 点击量' ) {
22+ } else if ( request . type === " 点击量" ) {
2323 sql = ` SELECT article.id as id, article.title as title, article.author as authorName,
2424 article.reprinted as reprinted, article.introduce as introduce, article.introduce_img as introduceImg,
2525 FROM_UNIXTIME(article.publish_time, '%Y-%m-%d %H:%i:%s') as publishTime,
2626 article.view_count as viewCount, article_type.name as type
2727 FROM article LEFT JOIN article_type ON article.type_id = article_type.id
2828 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" ;
3132 countResult = await this . app . mysql . query ( countSql , request . type ) ;
3233 } else {
3334 sql = ` SELECT article.id as id, article.title as title, article.author as authorName,
@@ -37,7 +38,11 @@ class ArticleListController extends controller {
3738 FROM article LEFT JOIN article_type ON article.type_id = article_type.id
3839 WHERE article.is_publish = 1 AND article_type.name = ?
3940 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+ ] ) ;
4146 countSql = `SELECT count(*) as total FROM article
4247 LEFT JOIN article_type ON article.type_id = article_type.id
4348 WHERE article.is_publish = 1
@@ -55,17 +60,43 @@ class ArticleListController extends controller {
5560 } else {
5661 this . ctx . body = {
5762 success : true ,
58- message : ' 获取文章列表失败' ,
63+ message : " 获取文章列表失败" ,
5964 } ;
6065 }
6166 }
6267
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" ) ;
6571 if ( result . length > 0 ) {
6672 this . ctx . body = { success : true , data : result } ;
6773 }
6874 }
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+ }
69100}
70101
71102module . exports = ArticleListController ;
0 commit comments