119 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			119 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | ||
| /**
 | ||
|  *	后台管理系统-首页
 | ||
|  */
 | ||
| namespace app\index\controller;
 | ||
| use app\index\controller\Base;
 | ||
| use think\facade\Db;
 | ||
| use think\facade\View;
 | ||
| use think\facade\Env;
 | ||
| use think\facade\Config;
 | ||
| 
 | ||
| class Index extends Base
 | ||
| {
 | ||
|     /**
 | ||
|      * 首页
 | ||
|      */
 | ||
|     public function index()
 | ||
|     {
 | ||
|         return View::fetch();
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 获取站点资讯列表
 | ||
|      */
 | ||
|     public function siteNewslist()
 | ||
|     {
 | ||
|         // 获取站点资讯分类(顶级分类id为1的子分类)
 | ||
|         $categories = Db::name('yz_article_category')
 | ||
|             ->where('cid', 1)
 | ||
|             ->where('delete_time', null)
 | ||
|             ->select()
 | ||
|             ->toArray();
 | ||
|         
 | ||
|         $articles = [];
 | ||
|         $categoryData = [];
 | ||
|         
 | ||
|         // 提取分类名称和ID用于前端tab显示
 | ||
|         foreach ($categories as $category) {
 | ||
|             $categoryData[] = [
 | ||
|                 'id' => $category['id'],
 | ||
|                 'name' => $category['name']
 | ||
|             ];
 | ||
|             
 | ||
|             // 获取该分类下的文章,限制4条
 | ||
|             $articles = Db::name('yz_article')
 | ||
|                 ->where('cate', $category['id'])
 | ||
|                 ->where('delete_time', null)
 | ||
|                 ->where('status', 2)
 | ||
|                 ->order('id', 'desc')
 | ||
|                 ->field('id, cate, title, image, author, publishdate, views')
 | ||
|                 ->limit(4)
 | ||
|                 ->select()
 | ||
|                 ->toArray();
 | ||
|             
 | ||
|         }
 | ||
|         
 | ||
|         return json([
 | ||
|             'code' => 1, 
 | ||
|             'msg' => '获取成功', 
 | ||
|             'articles' => $articles,
 | ||
|             'categories' => $categoryData
 | ||
|         ]);
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 获取技术文章列表
 | ||
|      */
 | ||
|     public function technicalArticleslist()
 | ||
|     {
 | ||
|         // 获取技术文章分类(顶级分类id为3的子分类)
 | ||
|         $categories = Db::name('yz_article_category')
 | ||
|             ->where('cid', 3)
 | ||
|             ->where('delete_time', null)
 | ||
|             ->select()
 | ||
|             ->toArray();
 | ||
| 
 | ||
|         // 组装分类数据,方便后续查找
 | ||
|         $categoryData = [];
 | ||
|         $categoryImageMap = [];
 | ||
|         $articlesByCategory = [];
 | ||
| 
 | ||
|         foreach ($categories as $category) {
 | ||
|             $categoryData[] = [
 | ||
|                 'id' => $category['id'],
 | ||
|                 'name' => $category['name']
 | ||
|             ];
 | ||
|             $categoryImageMap[$category['id']] = $category['image'] ?? '';
 | ||
| 
 | ||
|             // 获取每个分类下的文章,限制12条
 | ||
|             $articles = Db::name('yz_article')
 | ||
|                 ->where('cate', $category['id'])
 | ||
|                 ->where('delete_time', null)
 | ||
|                 ->where('status', 2)
 | ||
|                 ->order('id', 'desc')
 | ||
|                 ->field('id, cate, title, image, author, publishdate, views')
 | ||
|                 ->limit(12)
 | ||
|                 ->select()
 | ||
|                 ->toArray();
 | ||
| 
 | ||
|             // 替换image为空的文章
 | ||
|             foreach ($articles as &$article) {
 | ||
|                 if (empty($article['image']) && !empty($categoryImageMap[$article['cate']])) {
 | ||
|                     $article['image'] = $categoryImageMap[$article['cate']];
 | ||
|                 }
 | ||
|             }
 | ||
|             unset($article);
 | ||
| 
 | ||
|             $articlesByCategory[$category['id']] = $articles;
 | ||
|         }
 | ||
| 
 | ||
|         return json([
 | ||
|             'code' => 1,
 | ||
|             'msg' => '获取成功',
 | ||
|             'articles' => $articlesByCategory,
 | ||
|             'categories' => $categoryData
 | ||
|         ]);
 | ||
|     }
 | ||
| }
 | 
