71 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			71 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
|  | <?php | ||
|  | 
 | ||
|  | namespace app\controller; | ||
|  | 
 | ||
|  | use app\model\{Category, ProductModel}; | ||
|  | use page\DxtcPageA; | ||
|  | use think\Paginator; | ||
|  | 
 | ||
|  | class Search extends Base | ||
|  | { | ||
|  |     //列表页
 | ||
|  |     public function index() | ||
|  |     { | ||
|  |         $type = input('type/s', 'product'); | ||
|  |         if (!in_array($type, ['product', 'news', 'scheme'])) { | ||
|  |             return []; | ||
|  |         } | ||
|  | 
 | ||
|  |         $keyword = input('keyword/s', ''); | ||
|  | 
 | ||
|  |         if (empty($keyword)) { | ||
|  |             return $this->error('请输入搜索关键词'); | ||
|  |         } | ||
|  | 
 | ||
|  |         // 自定义分页驱动
 | ||
|  |         app('think\App')->bind(Paginator::class, DxtcPageA::class); | ||
|  | 
 | ||
|  |         $items     = []; | ||
|  |         $paginate  = [ | ||
|  |             'list_rows' => 20, | ||
|  |             'query'     => ['keyword' => $keyword, 'type' => $type] | ||
|  |         ]; | ||
|  |         $orderList = ['sort' => 'desc', 'id' => 'desc']; | ||
|  |         $path      = ''; | ||
|  |         switch ($type) { | ||
|  |             case 'product': | ||
|  |                 $items = ProductModel::where('title|description|content|params', 'like', '%'.$keyword.'%') | ||
|  |                     ->where('visible', 1) | ||
|  |                     ->order($orderList) | ||
|  |                     ->paginate($paginate); | ||
|  |                 $path  = '/product/detail.html'; | ||
|  |                 break; | ||
|  |             case 'news': | ||
|  |                 $categoryIds = Category::where('template_list', Category::TEMPLATE_NEWS)->column('id'); | ||
|  |                 $items       = \app\model\Article::where('title|summary|content', 'like', '%'.$keyword.'%') | ||
|  |                     ->whereIn('category_id', $categoryIds) | ||
|  |                     ->where('status', 1) | ||
|  |                     ->order($orderList) | ||
|  |                     ->paginate($paginate); | ||
|  |                 $path        = '/news/detail.html'; | ||
|  |                 break; | ||
|  |             case 'scheme': | ||
|  |                 $categoryIds = Category::where('template_list', Category::TEMPLATE_SCHEME)->column('id'); | ||
|  |                 $items       = \app\model\Article::where('title|summary|content', 'like', '%'.$keyword.'%') | ||
|  |                     ->whereIn('category_id', $categoryIds) | ||
|  |                     ->where('status', 1) | ||
|  |                     ->order($orderList) | ||
|  |                     ->paginate($paginate); | ||
|  |                 $path        = '/scheme/detail.html'; | ||
|  |                 break; | ||
|  |         } | ||
|  | 
 | ||
|  |         $this->data['items']     = $items; | ||
|  |         $this->data['bodyClass'] = 'main'; | ||
|  |         $this->data['type']      = $type; | ||
|  |         $this->data['keyword']   = $keyword; | ||
|  |         $this->data['path']      = $path; | ||
|  | 
 | ||
|  |         return $this->view(); | ||
|  |     } | ||
|  | } |