<?php
/** 文章管理 
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

namespace Uapp\Controller;
class ArticleController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
    }
	public function index() {
		$article_cat_list = D('Mobile/Article')->article_cat_list();
        $catid=I('catid');//分类id
        $param=I('param.');
        if(empty($catid)){
            $param['is_tuijian'] = 1;
        }else{
            $param['is_tuijian'] = 0;
        }

        $data=D('Mobile/Article')->article_list($catid,$param);
        if($data['article_list']['list']){
            foreach ($data['article_list']['list'] as $k => $v){
                if($v['shop_id'] > 0)
                    $data['article_list']['list'][$k]['shop_name'] = idtoname ($v['shop_id'],'Shops');
                else
                $data['article_list']['list'][$k]['shop_name'] = '平台发布';
                $data['article_list']['list'][$k]['shop_names'] = C('WEB_SITE_NAME');
                $data['article_list']['list'][$k]['addtime_format']=time_format($v['addtime'],'Y-m-d');
                if(str_exists($v['thumb'],'http://')==false){
                    $data['article_list']['list'][$k]['thumb']='http://'.$_SERVER["HTTP_HOST"].$v['thumb'];
                }
                if($v['catid'])$data['article_list']['list'][$k]['catname']=idtoname($v['catid'],'ArticleCat','name','id',false);
            }
            $article_list=$data['article_list'];
            $return['data']['list']=$article_list;
            $return['status']=1;
        }else{
            $return['status']=2;
            $return['msg']='已加载全部数据';
        }
        
        // $catname=$data['catname'];
        $catname='头条';
        $return['data']['article_cat_list']=$article_cat_list;
        $return['data']['catname']=$catname;
        $return['data']['catid']=$catid;

        // $this ->assign('pid',I('pid'));
        $this->ajaxReturn($return);
	}
	public function content() {
		$return['status'] = 2;
		$paramdata = I('param.');
		if(empty($paramdata['id'])){
			$return['msg'] = '参数错误';
			$this->ajaxReturn($return);
		}
		$articlecont = D('Mobile/Article')->article_con($paramdata['id']);
		if($articlecont){
			$return['data'] = $this->formatcontent($articlecont);

            $return['data']['share_url'] = U('Mobile/Article/article_cont',array('id'=>$paramdata['id'],'eid'=>$paramdata['uid']),true,true);
			$return['status'] = 1;
		}else{
			$return['msg'] = '未获取到数据';
		}
		$this->ajaxReturn($return);
	}
	private function formatcontent($cont) {
		if($cont['content']){
			$return = $cont['content'];
			$return['addtime'] = time_format($return['addtime'],'Y-m-d');
			$return['content'] = h($return['content']);
		}
		return $return;
	}

    /**文章分类
     * @return array
     */
    public function  cate_article(){
        $return=array('status'=>1);
        $where=array();
        $where['status']=1;
        $article_cat=M('ArticleCat')->where($where)->field('id,name,img')->select();

        if($article_cat){
            $return['data']=$article_cat;
        }else{
            $return['status']=2;
            $return['msg']='没有获取到数据';
        }
       $this->ajaxReturn($return);
    }
}