AbstractAction.php 10 KB
<?php

/**
 * 所有Controller控制器的基类
 * 
 * @name AbstractAction
 * @package 
 * @copyright yoho.inc
 * @version 1.0 (2015-9-15 11:55:25)
 * @author fei.hong <fei.hong@yoho.cn>
 */

namespace Action;

use Yaf\Controller_Abstract;
use Yaf\Dispatcher;
use Plugin\Cache;

class AbstractAction extends Controller_Abstract
{

    /**
     * HTTP请求对象
     * 
     * @var object 
     */
    protected $_request;

    /**
     * 初始化
     */
    public function init()
    {
        $this->_request = $this->getRequest();
        
        // 设置环境变量
        switch (APPLICATION_ENV) {
            case 'production':
                $this->_view->assign('rlsEnv', true);
                break;
            case 'testing':
                $this->_view->assign('testEnv', true);
                break;
            case 'develop':
            default:
                $this->_view->assign('devEnv', true);
                break;
        }
    }

    /**
     * 封装一下获取get参数
     * 
     * @param String $key
     * @param mixed $default
     * @return mixed
     */
    protected function get($key, $default = null)
    {
        return $this->_request->getQuery($key, $default);
    }

    /**
     * 封装一下获取post参数
     * 
     * @param String $key
     * @param mixed $default
     * @return mixed
     */
    protected function post($key, $default = null)
    {
        return $this->_request->getPost($key, $default);
    }

    /**
     * 关闭模板自动渲染
     * 
     * @return void
     */
    protected function disableView()
    {
        Dispatcher::getInstance()->autoRender(false);
    }

    /**
     * 输出JSON数据到浏览器
     * 
     * @return void
     */
    protected function echoJson($json)
    {
        headers_sent() || header('Content-Type: application/json; charset=utf-8;');

        if (is_array($json)) {
            $json = json_encode($json);
        }

        echo $json;
    }

    /**
     * 返回JSON数据
     * 
     * @param int $code 状态编码
     * @param string $message 提示信息
     * @param mixed $data 数据内容
     * @return json
     */
    protected function returnJson($code, $message, $data)
    {
        return json_encode(array(
            'code' => $code,
            'message' => $message,
            'data' => $data,
        ));
    }

    /**
     * 判断是不是AJAX请求
     * 
     * @return bool
     */
    protected function isAjax()
    {
        return $this->_request->isXmlHttpRequest();
    }

    /**
     * 跳转到错误页面
     */
    protected function error()
    {
        headers_sent() || header('Location: /error.html');

        exit();
    }

    /**
     * 设置Cookie
     * 
     * @param string  $name   cookie的名字
     * @param string  $value  cookie的值
     * @param integer $expire cookie过期时间
     * @param integer $path   cookie可用的路径
     * @param string  $domain cookie可用域名
     */
    protected function setCookie($name, $value, $expire = 0, $path = '/',$domain = 'yohobuy.com')
    {
        setcookie($name, $value, $expire, $path, $domain);
    }

    /**
     * 返回Cookie变量
     * 
     * @param  string $name    cookie名称
     * @param  string $default 未获取到返回的默认值
     * @return string          获取到的cookie值
     */
    protected function getCookie($name, $default = '')
    {
        return $this->request->getCookie($name, $default);
    }
    
    /**
     * 设置缓存
     * 
     * @param string $key 键名
     * @param mixed $value 需要缓存的数据
     * @param int $expire 缓存有效期(单位秒, 0表示永久)
     * @return void
     */
    protected function setCache($key, $value, $expire)
    {
        Cache::set($key, $value, $expire);
    }
    
    /**
     * 获取缓存
     * 
     * @param string $key 键名
     * @param bool $isMaster 控制是到主服务器取,还是到从服务器取缓存
     * @return mixed
     */
    protected function getCache($key, $isMaster = true)
    {
        if ($isMaster) {
            return Cache::get($key, 'master');
        } else {
            return Cache::get($key, 'slave');
        }
    }

    /*
     * 设置网站SEO的标题
     * 
     * @param string $title 标题
     * @return void
     */
    protected function setTitle($title)
    {
        $this->_view->assign('title', $title);
    }

    /**
     * 设置网站SEO的关键词
     * 
     * @param string $keywords 关键词,多个之间用","逗号分隔
     * @return void
     */
    protected function setKeywords($keywords)
    {
        $this->_view->assign('keywords', $keywords);
    }

    /**
     * 设置网站SEO的描述内容
     * 
     * @param string $description  描述内容
     * @return void
     */
    protected function setDescription($description)
    {
        $this->_view->assign('description', $description);
    }

    /**
     * 设置网站导航头部信息
     * 
     * @param string $backUrl  返回的链接
     * @param string $title  头部标题
     * @param string $homeUrl 返回首页的链接
     * @return void
     */
    protected function setNavHeader($backUrl, $title, $homeUrl)
    {
        $header = array();

        if (!empty($backUrl)) {
            $header['pageHeader']['navBack'] = $backUrl;
        }
        if (!empty($title)) {
            $header['pageHeader']['navTitle'] = $title;
        }
        if (!empty($homeUrl)) {
            $header['pageHeader']['navHome'] = $homeUrl;
        }

        $this->_view->assign('header', $header);
    }

    /**
     * 设置网站导航底部信息
     * 
     * @return void
     */
    protected function setNavFooter()
    {
        $footer = array();

        // 已登录 @todo
        if (false) {
            $footer['pageFooter']['user'] = array();
            $footer['pageFooter']['user']['name'] = 'goodboy'; // 昵称
            $footer['pageFooter']['user']['url'] = ''; // 个人中心链接
            $footer['pageFooter']['user']['signoutUrl'] = ''; // 登出链接
        }
        // 未登录
        else {
            $footer['pageFooter'] = array();
            $footer['pageFooter']['loginUrl'] = ''; // 登录链接
            $footer['pageFooter']['signupUrl'] = ''; // 注册链接
        }

        $this->_view->assign('footer', $footer);
    }
    
    /**
     * 设置侧边栏信息
     * 
     * @param string $guangChoosed 逛默认选中项 "all"表示全部,"boys":只看男生,"girls":只看女生
     * @return void
     */
    protected function setNavSide($guangChoosed = 'all')
    {
        $this->_view->assign('subNav', array(
            0 => array(
                'textCn' => '男生',
                'textEn' => 'BOYS',
                'styleClass' => 'boys',
                'url' => '/boys.html',
            ),
            1 => array(
                'textCn' => '女生',
                'textEn' => 'GIRLS',
                'styleClass' => 'girls',
                'url' => '/girls.html',
            ),
            2 => array(
                'textCn' => '潮童',
                'textEn' => 'KIDS',
                'styleClass' => 'kids',
                'url' => '/kids.html',
            ),
            3 => array(
                'textCn' => '创意生活',
                'textEn' => 'LIFE STYLE',
                'styleClass' => 'life',
                'url' => '/life.html',
            ),
            4 => array(
                'textCn' => '逛',
                'textEn' => 'TRENDFINDER',
                'styleClass' => 'guang',
                'url' => '/guang.html',
                'subNav' => array(
                    'list' => array(
                        0 => array(
                            'textCn' => '逛',
                            'textEn' => 'TrendFinder',
                            'back' => true,
                            'isSelect' => false,
                        ),
                        1 => array(
                            'textCn' => '查看全部',
                            'textEn' => '',
                            'back' => false,
                            'isSelect' => ($guangChoosed === 'all') ? true : false,
                            'url' => '/guang/list/index?gender=1,2,3'
                        ),
                        2 => array(
                            'textCn' => '只看男生',
                            'textEn' => 'Boys',
                            'back' => false,
                            'isSelect' => ($guangChoosed === 'boys') ? true : false,
                            'url' => '/guang/list/index?gender=1,3'
                        ),
                        3 => array(
                            'textCn' => '只看女生',
                            'textEn' => 'Girls',
                            'back' => false,
                            'isSelect' => ($guangChoosed === 'girls') ? true : false,
                            'url' => '/guang/list/index?gender=2,3',
                        ),
                    )
                )
            ),
        ));
        
    }
    
    /**
     * 返回顶部软件下载有关数据
     * @return array 下载有关数据
     */
    protected function getHeaderDownload()
    {
        return array(
            'img' => 'http://img11.static.yhbimg.com/adpic/2015/02/28/18/01d83bfad41c8fca8fd1ad334216d7d733.jpg?imageView/2/w/640/h/480',
            'url' =>  'http://www.baidu.com'
        );
    }

    /**
     * 设置首页以及频道页顶部信息
     * 
     * @return void
     */
    protected function setHomeChannelHeader()
    {
        $header['searchUrl'] = '';

        $this->_view->assign('homeHeader', $header);
    }

    /**
     * 返回顶部软件下载有关数据
     * @return array 下载有关数据
     */
    protected function setHeaderDownload()
    {
        $download = array(
            'img' => 'http://img11.static.yhbimg.com/adpic/2015/02/28/18/01d83bfad41c8fca8fd1ad334216d7d733.jpg?imageView/2/w/640/h/480',
            'url' =>  'http://www.baidu.com'
        );

        $this->_view->assign('headerDownload', $download);
    }

}