44use ModStart \Core \Dao \ModelUtil ;
55use ModStart \Core \Util \HtmlUtil ;
66use ModStart \Core \Util \TagUtil ;
7+ use Module \Blog \Model \Blog ;
78use Module \Blog \Util \BlogCategoryUtil ;
89use Module \Blog \Util \BlogTagUtil ;
910
@@ -80,7 +81,15 @@ public static function paginateBlog($categoryId, $page = 1, $pageSize = 10, $opt
8081 ], $ option ['whereOperate ' ]);
8182
8283 $ paginateData = ModelUtil::paginate ('blog ' , $ page , $ pageSize , $ option );
83- $ records = $ paginateData ['records ' ];
84+ $ records = self ::buildRecords ($ paginateData ['records ' ]);
85+ return [
86+ 'records ' => $ records ,
87+ 'total ' => $ paginateData ['total ' ],
88+ ];
89+ }
90+
91+ private static function buildRecords ($ records )
92+ {
8493 ModelUtil::decodeRecordsJson ($ records , 'images ' );
8594 TagUtil::recordsString2Array ($ records , 'tag ' );
8695 foreach ($ records as $ i => $ v ) {
@@ -90,16 +99,46 @@ public static function paginateBlog($categoryId, $page = 1, $pageSize = 10, $opt
9099 if (isset ($ records [$ i ]['images ' ][0 ])) {
91100 $ records [$ i ]['_cover ' ] = $ records [$ i ]['images ' ][0 ];
92101 }
93- if (empty ($ records [$ i ]['_cover ' ])) {
102+ if (empty ($ records [$ i ]['_cover ' ]) && isset ( $ v [ ' content ' ]) ) {
94103 $ ret = HtmlUtil::extractTextAndImages ($ v ['content ' ]);
95104 if (isset ($ ret ['images ' ][0 ])) {
96105 $ records [$ i ]['_cover ' ] = AssetsUtil::fixFull ($ ret ['images ' ][0 ]);
97106 }
98107 }
99108 }
109+ return $ records ;
110+ }
111+
112+
113+ public static function listBlogByYear ($ option = [])
114+ {
115+
116+ $ records = Blog::query ()->where (['isPublished ' => true ])
117+ ->where ('postTime ' , '< ' , date ('Y-m-d H:i:s ' ))
118+ ->orderBy ('postTime ' , 'desc ' )
119+ ->get (['id ' , 'images ' , 'tag ' , 'title ' , 'categoryId ' , 'postTime ' ])
120+ ->toArray ();
121+ $ records = self ::buildRecords ($ records );
122+
123+ $ yearRecords = [];
124+ foreach ($ records as $ i => $ v ) {
125+ $ year = date ('Y ' , strtotime ($ v ['postTime ' ]));
126+ if (!isset ($ yearRecords [$ year ])) {
127+ $ yearRecords [$ year ] = [
128+ 'count ' => 0 ,
129+ 'year ' => $ year ,
130+ 'records ' => [],
131+ ];
132+ }
133+ $ yearRecords [$ year ]['records ' ][] = $ v ;
134+ }
135+ foreach ($ yearRecords as $ i => $ v ) {
136+ $ yearRecords [$ i ]['count ' ] = count ($ v ['records ' ]);
137+ }
138+
100139 return [
101- 'records ' => $ records ,
102- 'total ' => $ paginateData [ ' total ' ] ,
140+ 'total ' => count ( $ records) ,
141+ 'records ' => $ yearRecords ,
103142 ];
104143 }
105144
0 commit comments