<?php
// +----------------------------------------------------------------------
// | 奇乐自媒体管理系统 商业版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilewl.com   
// +----------------------------------------------------------------------
// | 产品网址：http://www.qilecms.com
// +----------------------------------------------------------------------
// | Author:合肥奇乐网络科技有限公司 
// +----------------------------------------------------------------------
// | 版权说明：本产品为免费版，使用请保留底部版权
// +----------------------------------------------------------------------
namespace app\portal\controller;
use \think\Db;
class Search  extends Base
{
    public function index()
    {

    	 if($this->request->isAjax()){
              
		         $keywords =$this->request->param('keywords','strip_tags');
                
		         $page   = !empty($param['page'])?intval($param['page']):1; //开始页面
		         $limit  = !empty($param['limit'])?intval($param['limit']):10;; //分页数量

		         $start_page  = ($page-1)*$limit;
		         $where['status'] = 1; //审核通过
                 $order['create_time'] ='desc';
		         $map1[] = ['title','like',"%$keywords%"];
	             $map2[] = ['username','like',"%$keywords%"];
		         $total   = Db::name('article')->where($map1)->whereOr($map2)->count();
		         $list    = Db::name('article')->where($map1)->whereOr($map2)->order($order)->page("$start_page,$limit")->select();
                 
                 $cate = $this->getCache('articleCategory','cid');
          $data = [];
		         foreach($list as $k=>$v){
		            $data[$k]['id']     = $v['aid'];
		            $data[$k]['thumb']  = $v['thumb'];
		            $data[$k]['url']    = $v['url'];
		            $data[$k]['title']  = str_replace($keywords,'<span class="red">'.$keywords.'</span>',$v['title']);
                    $data[$k]['username'] = str_replace($keywords,'<span class="red">'.$keywords.'</span>',$v['username']);
		            $data[$k]['cname']  = $v['cname'];
                    $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);
                    $data[$k]['hits'] = $v['hits'];
                    $data[$k]['content'] = get_filter_str(htmlspecialchars_decode($v['content']),110);
                    $data[$k]['cname']   = $cate[$v['cid']]['cname'];
                    $data[$k]['url']     = url('@portal/article/detail',['aid'=>$v['aid']]);
		           
		         }
		        return json(['code'=>0,'msg'=>'success','pages'=>$total,'data'=>$data]);

			}else{


				// 相关用户
			        $keywords =$this->request->param('keywords','strip_tags');
				    $map1[] = ['nickname','like',"%$keywords%"];
			    	$searchUser = model('user')->where($map1)->paginate($map1,'','',10);
			    	foreach($searchUser as $k=>$v){	          
			          $searchUser[$k]['nickname'] = str_replace($keywords,'<span class="red">'.$keywords.'</span>',$v['nickname']);
			    	}
                  
			    	$this->assign('searchUser',$searchUser);
			}



        return $this->fetch();
    }
}
