131 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			131 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			PHP
		
	
	
|  | <?php | ||
|  | 
 | ||
|  | namespace app\controller; | ||
|  | 
 | ||
|  | use app\model\{ProductModel, | ||
|  |     Category, | ||
|  |     Model | ||
|  | }; | ||
|  | use page\DxtcPageA; | ||
|  | use think\db\exception\DataNotFoundException; | ||
|  | use think\db\exception\DbException; | ||
|  | use think\db\exception\ModelNotFoundException; | ||
|  | use think\Paginator; | ||
|  | 
 | ||
|  | class Product extends Base | ||
|  | { | ||
|  |     //详情
 | ||
|  |     public function detail($id = 0) | ||
|  |     { | ||
|  |         if ($id <= 0) { | ||
|  |             return $this->error('错误页面'); | ||
|  |         } | ||
|  |         $article = ProductModel::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']; | ||
|  | 
 | ||
|  |         $pathArr          = explode(',', $category['path']); | ||
|  |         $secondCategoryId = $pathArr[3] ?: $article['category_id']; | ||
|  |         $secondCategory   = Category::getById($secondCategoryId); | ||
|  | 
 | ||
|  |         $this->data['images'] = json_decode($article['images'], true); | ||
|  | 
 | ||
|  |         // 相关推荐
 | ||
|  |         $recommendList = $this->recommendList($article, 3); | ||
|  | 
 | ||
|  |         $this->data['recommendList'] = $recommendList; | ||
|  |         $this->setSeo($title, $keywords, $description); | ||
|  |         $this->data['item']       = $article; | ||
|  |         $this->data['category']   = $category; | ||
|  |         $this->data['secondInfo'] = $secondCategory; | ||
|  |         $this->data['categoryId'] = $category['id']; | ||
|  |         $this->data['topCategoryId'] = Category::firstGradeById($category['id']) ; | ||
|  |         return $this->view($category['template_detail'] ?? ''); | ||
|  |     } | ||
|  | 
 | ||
|  |     /** | ||
|  |      * 相关推荐 | ||
|  |      * | ||
|  |      * @param $article | ||
|  |      * @param $num | ||
|  |      * @return array | ||
|  |      * @throws DataNotFoundException | ||
|  |      * @throws DbException | ||
|  |      * @throws ModelNotFoundException | ||
|  |      */ | ||
|  |     private function recommendList($article, $num) | ||
|  |     { | ||
|  |         return ProductModel::where('category_id', $article['category_id']) | ||
|  |             ->where('visible', 1) | ||
|  |             ->order('sort', 'desc') | ||
|  |             ->order('id', 'desc') | ||
|  |             ->limit($num) | ||
|  |             ->select()->toArray(); | ||
|  |     } | ||
|  | 
 | ||
|  |     //列表页
 | ||
|  |     public function index() | ||
|  |     { | ||
|  |         $second = input('second/d', 0); | ||
|  |         $third  = input('third/d', 0); | ||
|  | 
 | ||
|  |         $first          = Category::getByRuleAlias(Category::RULE_PRODUCT);//产品顶级分类
 | ||
|  |         $categoryList   = Category::getChildrenByParentId($first['id'], false); | ||
|  |         $secondMenus    = []; | ||
|  |         $secondChildren = []; | ||
|  |         foreach ($categoryList as $k => $cate) { | ||
|  |             if ($cate['parent_id'] == $first['id']) { | ||
|  |                 $secondMenus[$cate['id']] = $cate; | ||
|  |                 unset($categoryList[$k]); | ||
|  |             } | ||
|  |         } | ||
|  | 
 | ||
|  |         foreach ($categoryList as $thirdCate) { | ||
|  |             foreach ($secondMenus as $secondCate) { | ||
|  |                 if (!isset($secondChildren[$secondCate['id']])) { | ||
|  |                     $secondChildren[$secondCate['id']] = []; | ||
|  |                 } | ||
|  |                 if ($thirdCate['parent_id'] == $secondCate['id']) { | ||
|  |                     $secondChildren[$secondCate['id']][] = $thirdCate; | ||
|  |                 } | ||
|  |             } | ||
|  |         } | ||
|  | 
 | ||
|  |         $secondInfo = $second == 0 ? array_values($secondMenus)[0] : $secondMenus[$second]; | ||
|  |         $thirdMenus = $secondChildren[$secondInfo['id']]; | ||
|  | 
 | ||
|  |         $this->data['thirdId'] = $third; | ||
|  | 
 | ||
|  | 
 | ||
|  |         $description = $first['seo_description'] ?:  $this->system['seo_description']; | ||
|  |         $keywords    = $first['seo_keywords'] ?:  $this->system['seo_keywords']; | ||
|  |         $title       = $first['seo_title'] ?: $first['title'].' | '.$this->system['seo_title']; | ||
|  |         $this->setSeo($title, $keywords, $description); | ||
|  |         $listSort = ['a.sort' => 'desc']; | ||
|  | 
 | ||
|  |         $this->data['secondInfo']  = $secondInfo; | ||
|  |         $this->data['secondMenus'] = $secondMenus; | ||
|  |         $this->data['thirdMenus']  = $thirdMenus; | ||
|  | 
 | ||
|  |         // 自定义分页驱动
 | ||
|  |         app('think\App')->bind(Paginator::class, DxtcPageA::class); | ||
|  | 
 | ||
|  |         $cateId     = $third > 0 ? $third : $secondInfo['id']; | ||
|  |         $queryParam = ['second' => $secondInfo['id'], 'third' => $third]; | ||
|  |         $items      = ProductModel::getList($cateId, $first['number'], '', [], 1, $listSort, false, $queryParam); | ||
|  | 
 | ||
|  |         $this->data['items']      = $items; | ||
|  |         $this->data['category']   = $first; | ||
|  |         $this->data['categoryId'] = $first['id']; | ||
|  |         $this->data['bodyClass']  = 'main'; | ||
|  |         $this->data['topCategoryId'] = Category::firstGradeById($first['id']) ; | ||
|  |         return $this->view(); | ||
|  |     } | ||
|  | } |