100 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			100 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
| <?php
 | |
| 
 | |
| namespace app\controller;
 | |
| 
 | |
| use app\model\{CasesModel, Category};
 | |
| 
 | |
| class Cases extends Base
 | |
| {
 | |
|     /**
 | |
|      * 详情
 | |
|      *
 | |
|      * @throws \think\db\exception\ModelNotFoundException
 | |
|      * @throws \think\db\exception\DbException
 | |
|      * @throws \think\db\exception\DataNotFoundException
 | |
|      */
 | |
|     public function detail($id = 0)
 | |
|     {
 | |
|         if ($id <= 0) {
 | |
|             return $this->error('错误页面');
 | |
|         }
 | |
|         $article = CasesModel::getById($id);
 | |
|         if (empty($article)) {
 | |
|             return $this->error('无此案例');
 | |
|         }
 | |
| 
 | |
|         $category = Category::getById($article['category_id']);
 | |
| 
 | |
|         $description = $article['seo_description'] ?: $this->system['seo_description'];
 | |
|         $keywords    = $article['seo_keywords'] ?: $this->system['seo_keywords'];
 | |
|         $title       = $article['seo_title'] ?: $article['title'].' | '.$this->system['seo_title'];
 | |
| 
 | |
|         $this->data['images'] = json_decode($article['images'], true);
 | |
| 
 | |
|         // 相关推荐
 | |
|         $recommendList = $this->recommendList($article, 6, $id);
 | |
| 
 | |
|         $this->data['recommendList'] = $recommendList;
 | |
|         $this->setSeo($title, $keywords, $description);
 | |
|         $this->data['item']          = $article;
 | |
|         $this->data['category']      = $category;
 | |
|         $this->data['categoryId']    = $category['id'];
 | |
|         $this->data['categoryPath']  = Category::categoryPath($category['path']);
 | |
|         $this->data['topCategoryId'] = Category::firstGradeId($category['path'], $category['id']);
 | |
|         return $this->view($category['template_detail'] ?? '');
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 相关推荐
 | |
|      *
 | |
|      * @param $article
 | |
|      * @param  int  $num
 | |
|      * @param  int  $exceptId
 | |
|      * @return array
 | |
|      * @throws \think\db\exception\DataNotFoundException
 | |
|      * @throws \think\db\exception\DbException
 | |
|      * @throws \think\db\exception\ModelNotFoundException
 | |
|      */
 | |
|     private function recommendList($article, int $num = 0, int $exceptId = 0): array
 | |
|     {
 | |
|         return CasesModel::where('category_id', $article['category_id'])
 | |
|             ->where('visible', 1)
 | |
|             ->where('recommend', 1)
 | |
|             ->when($exceptId > 0, function ($q) use ($exceptId) {
 | |
|                 $q->where('id', '<>', $exceptId);
 | |
|             })
 | |
|             ->order('sort', 'desc')
 | |
|             ->order('id', 'desc')
 | |
|             ->limit($num)
 | |
|             ->select()->toArray();
 | |
|     }
 | |
| 
 | |
|     //文章列表接口,获取栏目下文章列表
 | |
|     public function getList()
 | |
|     {
 | |
|         $categoryId = input('category_id/d', 0);
 | |
|         $page       = input('page/d', 1);
 | |
|         $size       = input('size/d', 10);
 | |
| 
 | |
|         if ($categoryId <= 0) {
 | |
|             return $this->json(1, '参数错误');
 | |
|         }
 | |
|         $category = Category::getById($categoryId);
 | |
|         if (empty($category)) {
 | |
|             return $this->json(2, '栏目不存在');
 | |
|         }
 | |
| 
 | |
|         $categoryIds = Category::where('parent_id', $categoryId)->column('id');
 | |
|         $items = CasesModel::whereIn('category_id', $categoryIds)
 | |
|                         ->where('visible', 1)
 | |
|                         ->page($page, $size)
 | |
|                         ->select()
 | |
|                         ->toArray();
 | |
| 
 | |
|         foreach ($items as $item) {
 | |
|             $item['image'] = $this->isMobile ? $item['image_mobile'] : $item['image'];
 | |
|         }
 | |
| 
 | |
|         return $this->json(0, 'ok', $items);
 | |
|     }
 | |
| } |