Starclass.php 8.84 KB
<?php

use Action\AbstractAction;
use Guang\StarClassModel;
use LibModels\Wap\Guang\StarClassData;
use Plugin\Helpers;

/**
 * 星潮教室
 * Class StarClassController
 */
class StarClassController extends AbstractAction
{

    private $_isApp = false;

    /**
     * 星潮教室首页
     */
    public function indexAction()
    {
        //获取uid
        $uid = $this->getLoggedUid() ? $this->getLoggedUid() : $this->getCookie('uid');

        if (!$this->_isApp) {
            if ($uid) {
                $this->setCookie('uid', $uid);
            }
        }else{
            $this->setCookie('isApp',true);
            if ($uid) {
                $this->setCookie('uid', $uid);
            }
        }
        //设置头部公共导航
        $app = $this->getCookie('isApp', false);
        $isApp = $app || $this->_isApp;
        if (!$isApp) {
            $this->setNavHeader('星潮教室', true, false);
        }

        $this->setTitle('星潮教室', false, '');
        //调用模型获得星潮教室首页数据
        $data = StarClassModel::getStarClass($uid, $isApp);

        //渲染模板
        $this->_view->display('index', array(
                'trendClassHome' => $data,
                'trendClassHomePage' => true
            )
        );
    }


    /**
     * 星潮教室-星专题
     */
    public function specialAction()
    {
        //获取uid
        $isApp = $this->getCookie('isApp', false);

        if (!$isApp) {
            //设置头部公共导航
            $this->setNavHeader('星潮教室', true, false);
        }
        $this->setTitle('星潮教室', false, '');

        //调用模型获得星专题数据
        $data = StarClassModel::getspecial($isApp);

        //渲染模板
        $this->_view->display('subject', array(
            'trendClassSubject' => $data,
            'trendClassHomePage' => true
        ));
    }


    /**
     * 星潮教室-星搭配
     */
    public function collocationAction()
    {
        //获取uid
        $isApp = $this->getCookie('isApp', false);

        if (!$isApp) {
            //设置头部公共导航
            $this->setNavHeader('星潮教室', true, false);
        }
        $this->setTitle('星潮教室', false, '');

        //调用模板获得星搭配数据
        $data = StarClassModel::getCollocation();

        //渲染模板
        $this->_view->display('collocation', array(
            'trendClassCollocation' => $data,
            'trendClassCollocationPage' => true
        ));
    }


    /**
     * 星潮教室-星搭配文章 ajax请求
     */
    public function ajaxCollocationAction()
    {
        if (!$this->isAjax()) {
            return;
        }
        //获得必要参数
        $page = $this->get('page',1);
        $isApp = $this->getCookie('isApp', false);
        $uid = $this->getCookie('uid', false);

        //调取模型获得星搭配文章
        $data = StarClassModel::ajaxCollocation($page, $uid, $isApp, $limit = 20);

        //返回数据到页面,没有数据返回空格
        if (isset($data) && !empty($data)) {
            $this->_view->display('collocation-list',$data);
        }else{
            echo ' ';
        }
    }


    /**
     * 星潮教室-签到-Top100排行榜
     */
    public function topAction()
    {
        //获取uid
        $isApp = $this->getCookie('isApp', false);
        $uid = $this->getCookie('uid', false);

        if (!$isApp) {
            //设置头部公共导航(此处返回键调转星潮教室首页)
            $url = Helpers::url('/guang/starclass/index');
            $this->setNavHeader('星潮粉丝榜', $url, false);
        }
        $this->setTitle('星潮粉丝榜', false, '');

        //根据客户端类型,进行未登录跳转
        if (!$uid) {
            $refer = Helpers::url('/guang/starclass/top');
            if (!$isApp) {
                $this->go(Helpers::url('/signin.html', array('refer' => $refer)));
            } else {
                $referEncode = strtr($refer, array('/' => '\\/'));
                $this->go($refer . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . $referEncode . '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}');
            }
            return;
        }

        //调取模型获得Top100数据
        $data = StarClassModel::getTop($uid);

        //渲染模板
        $this->_view->display('check-in',array(
            'trendClassCheckIn'=>$data,
            'trendClassTopPage' => true
        ));
    }


    /**
     * 星潮教室-签到
     */
    public function signAction()
    {
        if(!$this->isAjax()){
            return;
        }

        //根据客户端类型,进行未登录跳转
        $isApp = $this->getCookie('isApp', false);
        $uid = $this->getCookie('uid', false);

        if (!$uid) {
            $refer = Helpers::url('/guang/starclass/index');
            if (!$isApp) {
                $this->echoJson(array('code'=>201,'data'=>Helpers::url('/signin.html', array('refer' => $refer))));
            } else {
                $referEncode = strtr($refer, array('/' => '\\/'));
                $this->echoJson(array('code'=>201,'data'=>$refer . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . $referEncode . '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}'));
            }
            return;
        }
        //调取模型完成签到,并返回数据
        $res = StarClassModel::sign($uid);

        //输出签到结果到页面
        $this->echoJson($res);
    }


    /**
     * 星搭配-文章分享
     */
    public function forwardAction()
    {
        if(!$this->isAjax()){
            return;
        }
        $result = array();

        //根据客户端类型,进行未登录跳转
        $isApp = $this->getCookie('isApp', false);
        $uid = $this->getCookie('uid', false);
        if (!$uid) {
            $refer = Helpers::url('/guang/starclass/collocation');
            if (!$isApp) {
                $this->echoJson(array('code'=>201,'data'=>Helpers::url('/signin.html', array('refer' => $refer))));
            } else {
                $referEncode = strtr($refer, array('/' => '\\/'));
                $this->echoJson(array('code'=>201,'data'=>$refer . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . $referEncode . '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}'));
            }
            return;
        }else{
            //调取接口分享文章
            $result = StarClassData::forward($uid);
        }

        $this->echoJson($result);
    }

    /**
     * 星搭配-收藏资讯文章
     */
    public function setFavoriteAction()
    {
        if (!$this->isAjax()) {
            return;
        }

        //根据客户端类型,进行未登录跳转
        $uid = $this->getCookie('uid', false);
        $isApp = $this->getCookie('isApp', false);
        if (!$uid) {
            $refer = Helpers::url('/guang/starclass/collocation');;
            if (!$isApp) {
                $this->echoJson(array('code'=>201,'data'=>Helpers::url('/signin.html', array('refer' => $refer))));
            } else {
                $referEncode = strtr($refer, array('/' => '\\/'));
                $this->echoJson(array('code'=>201,'data'=>$refer . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . $referEncode . '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}'));
            }
            return;
        }

        //获取文章ID,接口Type(fav调取收藏接口,del调取取消收藏接口)
        $articleId = $this->post('articleId');
        $type = $this->post('type');

        if (!isset($articleId) || !isset($type)) {
            $this->echoJson(array('code'=>400,'message'=>'参数错误!'));
        }
        $result = array();
        //调取接口进行收藏
        $result = StarClassData::setFavorite($articleId, $uid, $type);

        if (!empty($result)) {
            $this->echoJson($result);
        }

    }

    /**
     * 星鲜事-点赞
     */
    public function setPraiseAction()
    {
        if(!$this->isAjax()){
            return;
        }
        //获得客户端ID(点赞不需要登录)
        $udid = $this->getUdid();
        //获得文章ID
        $articleId = $this->post('articleId');

        //调取接口点赞
        $result = StarClassData::setPraise($articleId, $udid);

        $this->echoJson($result);
    }

    /**
     * 检测登录状态
     *
     * @return int
     */
    private function getLoggedUid()
    {
        // 判断是否是应用访问, 拼接APP需要的URL参数
        $this->_isApp = null !== $this->get('app_version');
        $uid = false;
        if ($this->_isApp) {
            $uid = $this->get('uid');
        } else {
            $uid = $this->getUid(true);
        }

        return $uid;
    }

}