News.class.php 9.51 KB
<?php
/**
 * 默认控制器
 */
class Controller_News extends Controller_Abstract
{
    
    public function indexAction()
    {
        $requestTag =  trim($this->_request->tag);//标签
        $limit = 24;
        $tag = $requestTag;
        $exceptTags = array('手机视频');
        /*if($this->_platform == 'android' || $this->_platform == 'iphone')
        {
        	$exceptTags = array('视频');
        	if($requestTag == '视频')
        	{
        	    $tag = '手机视频';//html5视频为手机视频
        	}
        }
        else
        {
        	$exceptTags = array('手机视频');
        }*/
        $total = Facade_News::getTotal($tag, $exceptTags);
        $page = new Lib_Helper_Pagination($total, $limit);
        if ($tag)
        {
            $page->setParames(array('tag' => $requestTag)) ;
        }
        $tagKeys = Facade_News::$types;
        $list = Facade_News::getList($tag, $page->getOffset(), $limit, $exceptTags);
        $this->_view['list'] = $list;
        $newTags = Facade_News::getTags();
        $this->_view['total'] = 0;
        $tags = $temp = array();
        foreach($newTags as $tag)
        {
            $temp[$tag['tag']] = $tag;
        }
        foreach($tagKeys as $tagKey)
        {
        	$tags[$tagKey] = array('tag' => $tagKey, 'num'=> 0);
        	if(isset($temp[$tagKey]))
        	{
        	    $tags[$tagKey] = $temp[$tagKey];
        	}
        }
        if($this->_platform == 'android' || $this->_platform == 'iphone')
        {
            $tags['视频'] = $temp['手机视频'];
        }
        $this->_view['tags'] = $tags;
        foreach($this->_view['tags'] as $val)
        {
        	$this->_view['total']  += $val['num'];
        }
        $page->setOptions(array('afterAppend' => 'setLayout'));
        $this->_view['pagination'] = $page->getPagination();
        $this->_view['current_tag'] = $requestTag;
    }
    
    /**
     * 手机端最新资讯
     * 
     */
    public function indexMobileAction()
    {
        $requestTag =  $tag = trim($this->_request->tag);//标签
       // $exceptTags = array('视频');
        $exceptTags = array('手机视频');
        $limit = 8;
        $total = Facade_News::getTotal($tag, $exceptTags);
        $page = new Lib_Helper_Pagination($total, $limit);
        $tagKeys = Facade_News::$types;
        $list = Facade_News::getList($tag, $page->getOffset(), $limit, $exceptTags);
        $this->_view['list'] = $list;
        $newTags = Facade_News::getTags();
        $this->_view['total'] = 0;
        $tags = $temp = array();
        foreach($newTags as $tag)
        {
            $temp[$tag['tag']] = $tag;
        }
        foreach($tagKeys as $tagKey)
        {
            $tags[$tagKey] = array('tag' => $tagKey, 'num'=> 0);
            if(isset($temp[$tagKey]))
            {
                $tags[$tagKey] = $temp[$tagKey];
            }
        }
      //  $tags['视频'] = $temp['手机视频'];
        $this->_view['tags'] = $tags;
        foreach($this->_view['tags'] as $val)
        {
            $this->_view['total']  += $val['num'];
        }
        $this->_view['current_tag'] = $requestTag;
        $this->_view['isAjax'] = $this->_request->isAjax();
        $this->_view['pageTotal'] = $total;
        $this->_view['limit'] = $limit;
        $this->_view['page'] = $page->getCurrentPage();
    }
    
    /**
     * 发布资讯
     */
    public function publishAction()
    {
        $secret = $this->_request->secret;
    	if($secret == SITE_PUBLISH_NEWS_SECRET)
    	{
    	    $tag_id = $this->_request->tag_id;
    	    if(!isset(Facade_News::$types[$tag_id]))
    	    {
    	        return $this->returnJson(false, 403, '','频道不存在');
    	    }
    	    $tag = Facade_News::$types[$tag_id];
    	    $brand_ids =  $this->_request->brand_ids;
    	    $title = $this->_request->title;
    	    $content = $this->_request->content;
    	    $pics = $this->_request->pics;
    	    $thumb = $this->_request->thumb;
    	    $is_video = $this->_request->is_video;
    	    if(empty($title) || empty($content) || empty($thumb))
    	    {
    	        return $this->returnJson(false, 403, '','参数不能为空');
    	    }
    	    $brand_ids = array_filter(explode(',', $brand_ids));
    	    $pics = array_filter(explode(',', $pics));
    	    list($width, $height, $type, $attr) = getimagesize($thumb);
    	    $thumb_size = json_encode(array('width'=> $width,'height'=> $height));
    	    //是否存在视频
    	    if(!empty($is_video))
    	    {
    	        $tag = '视频';
    	    }
/*    	    $videos = array_filter(explode(',', $videos));
    	    $html = '';
    	    foreach($videos as $video)
    	    {
                $html = <<<EOT
        	      <object class="content-web-flash" width="700px" height="394px" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
        	        <param value="res_site/swf/YohoodPlayer.swf?url=video_url" name="movie" /><param value="high" name="quality" />
        	        <param value="#ffffff" name="bgcolor" />
        	        <param name="allowFullScreen" value="true" />
        	        <param value="always" name="allowScriptAccess" />
        	        <param value="transparent" name="wmode" />
        	        <embed align="middle" style="width:700px;height:394px;" 
        	           allowscriptaccess="always" loop="false" quality="high" src="res_site/swf/YohoodPlayer.swf?url=video_url" 
        	        width="0" height="0" />
        	        </object>
EOT;
                 $html = str_replace('res_site', SITE_RES, $html);
                 $html = str_replace('video_url', $video, $html);
                 $content .= '<br/>'.$html;
    	    }*/
    	    $data = array('title' => $title, 'content' => $content, 'pics' => implode('|', $pics),'tag'=> $tag,
    	            'thumb' => $thumb, 'thumb_size'=> $thumb_size);
    	    //新建
    	    $id = Facade_News::setInfo($data);
    	    foreach($brand_ids as $brandID)
    	    {
    	        if(Facade_Brand::getBrandByID($brandID))
    	        {
    	            Facade_Brand::setBrandNewsRela($id, $brandID);
    	        }
    	    }
    	    return $this->returnJson(true, 200, '','发布成功');
    	}
    	else
    	{
    	    return $this->returnJson(false, 403, '','密钥不正确');
    	}
    }
    
    /**
     * 获取标签
     * 
     * @return json
     */
    public function gettagsAction()
    {
        $res = array();
        foreach(Facade_News::$types as $key=> $name)
        {
            $res[] = array('id'=> $key, 'name'=> $name);
        }
    	return $this->returnJson(true, 200, $res);
    }
    
    /**
     * 详情页
     */
    public function detailAction()
    {
        $id = intval($this->_request->id);
        $news = array();
        if (empty($id))
        {
            return $this->_redirect('news/index');
        }
        $info = Facade_News::getOneById($id);
        if (!$info)
        {
            return $this->_redirect('news/index');
        }
        if($this->_platform == 'android' || $this->_platform == 'iphone')
        {
            //$exceptTags = array('视频');
            $exceptTags = array('手机视频');
            $temp = Facade_News::getSameNewsById($id, $exceptTags);
            $temp2 = Facade_News::getLatestNews(array(), 0, 5, $exceptTags);
            unset($temp2[$id]);
            if(!empty($temp))
            {
                $news = array($temp['id'] => $temp);
            }
            $news = array_slice(array_filter($news + $temp2), 0, 3);
        }
        $info['title'] = strip_tags($info['title']);
        $info['pics'] = array_filter(explode('|', $info['pics']));
        $info['thumb'] = Lib_Images::getImageUrl($info['thumb'], 'source','fragmentimg');
        //增加浏览量
        Facade_News::updateHits($id);
        $this->_view['info'] = $info;
        $this->_view['banners'] = Facade_Index::getIndex(4);
        $this->_view['news'] = $news;
    }
    
    /**
     * 详情页相关新闻
     * 
     * @return json
     */
    public function getdetailnewsAction()
    {
        $id = intval($this->_request->id);
        $list = array('recom'=> array(), 'hotVideo'=> array(), 'lastVideo' => array());
        $tag = '视频';
        $exceptTags = array('手机视频');
        $recom = array();
        $temp = Facade_News::getSameNewsById($id, $exceptTags);
        $temp2 = Facade_News::getLatestNews(array(), 0, 5, $exceptTags);
        unset($temp2[$id]);
        if(!empty($temp))
        {
            $recom = array($temp['id'] => $temp);
        }
        $recom = array_slice(array_filter($recom + $temp2), 0, 3);
        $hotVideo = array_values(Facade_News::getListByDateHits($tag, 0, 5, $exceptTags));
        $lastVideo = array_values(Facade_News::getLatestNews($tag, 0, 5, $exceptTags));
        $list['recom'] = $recom;
        $list['hotVideo'] = $hotVideo;
        $list['lastVideo'] = $lastVideo;
        foreach($list as $key => $listnews)
        {
            if(!empty($listnews))
            {
            	foreach($listnews as $key2 => $news)
            	{
            	    //不需要内容
            	    $list[$key][$key2]['content'] = '';
            	}
            }	
        }
        return $this->returnJson(true, 200, $list);
    }
    
    public function getwechatsignpackageAction()
    {
        $signPackage = Lib_Wechat_JSSDK::getSignPackage();
        $callback = $this->_request->query('callback', '');
        $json = json_encode($signPackage);
        header('Content-Type: application/javascript;');
        return  $callback . '(' . $json . ');';
    }
}