<?php
// +----------------------------------------------------------------------
// | 奇乐自媒体内容管理系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com
// +----------------------------------------------------------------------
// | 官方论坛：http://bbs.qilecms.com
// +----------------------------------------------------------------------
// | Author:奇乐网络
// +----------------------------------------------------------------------
// | 版权说明：本产品为免费版，使用请保留底部版权
// +----------------------------------------------------------------------
namespace app\user\controller;
use \think\Db;
class Home  extends Base
{

    public function initialize(){
       parent::initialize();
      $uid = $this->request->param('uid');
      if(empty($uid)){
         $this->redirect('/');
      }
      $map['uid']     = $uid;
      $map['status']  = 1;
      $userinfo     =  model('user')->infoData($map);
      if(empty($userinfo)){
         $this->redirect('/');
      }


    

      $this->assign('userinfo',$userinfo);
//获得文章数量
      $article_count = Db::name('article')->cache('article_count',60)->where($map)->count('aid'); //统计文章数量
 //我关注的用户数量
      $map2['who_follow'] = $uid;
      $follow_count = Db::name('follow')->cache('follow_count',60)->where($map2)->count('id');
//我的粉丝数量
      $map3['follow_who'] = $uid;
      $fans_count = Db::name('follow')->cache('follow_count',60)->where($map3)->count('id');

//获得评论列表
      $comment_list = model('article_comment')->listPageData('','','',1); 
      foreach($comment_list as $k=>$v){
        $artinfo = model('article')->infoData(['aid'=>$v['article_id']],'title');
        $comment_list[$k]['title'] = $artinfo['title'];

      }
      $this->assign('userinfo',$userinfo);
      $this->assign('article_count',$article_count);
      $this->assign('follow_count',$follow_count);
      $this->assign('fans_count',$fans_count);
      
      $this->assign('comment_list',$comment_list);
    }
    public function index()
    {
    
//获得文章
       $uid = $this->request->param('uid');
      if(empty($uid)){
         $this->redirect('/');
      }
      $map['uid']     = $uid;
      $map['status']  = 1; 
      $lists = model('article')->listPageData($map,'','',10);
             $list =$lists->toArray()['data'];
                 $cate = $this->getCache('articleCategory','cid');
                 $article_list = [];
             foreach($list as $k=>$v){
                    $article_list[$k]['id']     = $v['aid'];
                    $article_list[$k]['thumb']  = $v['thumb'];
                    $article_list[$k]['url']    = $v['url'];
                    $article_list[$k]['title']  = $v['title'];
                    $article_list[$k]['cname']  = $v['cname'];
                    $article_list[$k]['username'] = $v['username'];
                    $article_list[$k]['create_time'] = date('Y-m-d',$v['create_time']);
                    $article_list[$k]['hits'] = $v['hits'];
                    $article_list[$k]['content'] = get_filter_str(htmlspecialchars_decode($v['content']),50);
                    $article_list[$k]['cname']   = $cate[$v['cid']]['cname'];
                    $article_list[$k]['url']     = url('@portal/article/detail',['aid'=>$v['aid']]);
               
             }
            
    

  $page = $lists->render();
  $this->assign('page', $page);
  $this->assign('article_list',$article_list);

      return $this->fetch();
    }

        public function collect()
    {
        $uid = $this->request->param('uid');
       
        $map['uid']     = $uid;
 
        $lists = model('collect')->listPageData($map,'','',10);


                $list =$lists->toArray()['data'];
                $data = [];
		         foreach($list as $k=>$v){
                   $map = [];
                   $map[] = ['aid','=',$v['article_id']];
                   $map[] = ['status','=',1]; //审核通过
		               $article = model('article')->infoData($map);
                   
                   if(!empty($article)){
                     $data[$k]['content']     =  get_filter_str(htmlspecialchars_decode($article['content']),50);
                     $data[$k]['title']       =  $article['title'];
                  
                     $data[$k]['thumb']       =  $article['thumb'];
                     $data[$k]['thumb1']      =  $article['thumb1'];
                     $data[$k]['thumb2']      =  $article['thumb2'];
                     $data[$k]['url']         =  url('@portal/article/detail',['aid'=>$article['aid']]);
                     $data[$k]['create_time'] =  date('Y-m-d',$v['create_time']);
                   }

 
		
		         }
      $page = $lists->render();
      $this->assign('page', $page);     
      $this->assign('collect_article',$data);
      return      $this->fetch();
		      
    }

//评论
       public function comment()
    {

        $uid = $this->request->param('uid');   
        $map['uid']     = $uid;
        $lists = model('articleComment')->listPageData($map,'','',10);
        $list =$lists->toArray()['data'];
                
        $data = [];
		    foreach($list as $k=>$v){
		         	   $data[$k]['content'] = $v['content']; //评论内容
		         	   $data[$k]['like'] = $v['like'];
		         	   $where2[] = ['status','=',1]; //审核通过
		         	   $where2[] = ['aid','=',$v['article_id']];
		             $article = model('article')->infoData($where2);
		             $data[$k]['title']       =  $article['title'];          
		             $data[$k]['url']         =  url('@portal/article/detail',['aid'=>$article['aid']]);
		             $data[$k]['create_time'] =  date('Y-m-d',$v['create_time']);
		    }
		   $page = $lists->render();
       $this->assign('page', $page);
       $this->assign('comment_article',$data);
      return      $this->fetch();
    }  
}
