RootAction.php 5.87 KB
<?php
namespace Action;

use Hood\Action as HoodAction;
use Yaf\Registry;
use Yaf\Dispatcher;
use Configs\StaticCss;
use Configs\StaticJs;
use Configs\Site;
use Plugin\Paging;

class RootAction extends HoodAction
{
    protected $_appConfig = array();

    /**
     * 账号信息
     * @var array
     */
    protected $_accountInfo = array();

    /**
     * 账号ID
     * @var int
     */
    protected $_pid = 0;

    protected $_token = '';

    public function init()
    {
        $this->_appConfig = Registry::get('appConfig');
        if ($this->getRequest()->isXmlHttpRequest() == false) {
            $this->getView()->setLayout('default');
        }
        $this->_assign('globalSiteName', Site::$globalSiteName);
    }

    public function _assign($name, $value)
    {
        return $this->getView()->assign($name, $value);
    }

    /**
     * 验证
     * @param string $refer
     * @return mixed
     * @throws \Hood\Debug\DebugException
     */
    public function _auth($refer = 'auto')
    {
        return $this->_accountInfo = $accountInfo;
    }

    /**
     * 分页
     * @param string $template
     * @return \Plugin\Paging
     */
    public function _paging($template)
    {
        $paging = new Paging($template);
        return $paging;
    }

    /**
     * 获取参数
     * @param null $name
     * @param null $default
     * @return mixed
     */
    protected function _getEnv($name = null, $default = null)
    {
        return $this->getRequest()->getEnv($name, $default);
    }

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

    /**
     * 获取所有参数
     * @return array
     */
    protected function _getParams()
    {
        return $this->_request->getParams();
    }

    /**
     * 获取所有参数
     * @return mixed
     */
    protected function _getRequests()
    {
        return $this->_request->getRequest();
    }

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

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

    /**
     *
     * 兼容POST和GET的调用方法
     *
     * @param $key
     * @param null $default
     * @return mixed|null
     */
    protected function _getParam2($key, $default = null)
    {
        if (($result = $this->_get($key)) || ($result = $this->_post($key))) {
            return $result;
        }
        return $default;
    }

    /**
     * 封装一下获取post参数
     * @param String $key
     * @param mixed $default
     * @return mixed
     */
    protected function _typePost($key, $settype = null, $default = null)
    {
        $result = $this->getRequest()->getPost($key, $default);
        switch ($settype) {
            case 'bool':
                $result = (bool)$result;
                break;
            case 'int':
                $result = (int)$result;
                break;
            case 'string':
                $result = (string)$result;
                break;
            case 'float':
                $result = (float)$result;
                break;
            case 'binary':
                $result = (binary)$result;
                break;
            default:
                $result = $result;
                break;
        }
        return $result;
    }

    /**
     * 关闭模板
     */
    public function disableView()
    {
        Dispatcher::getInstance()->autoRender(FALSE);
    }


    /**
     * 获取JS URL
     *
     * @param String $jsName
     * @return String
     */
    public function _js($jsName, $local = false)
    {
        if (isset(StaticJs::$res[$jsName])) {
            $js = $this->_appConfig['website']['static']['url'];
            if ($local == false) {
                $js .= StaticJs::$res[$jsName];
            } else {
                $js = StaticJs::$res[$jsName];
            }
            return $js;
        }
        return '';
    }

    /**
     * 获取JS URL
     *
     * @param String $jsName
     * @return String
     */
    public function _viewScriptPush(array $jsList, $scriptName = '_footerScript')
    {
        $script = $this->_viewScript($scriptName);
        foreach ($jsList as $js) {
            $script->appendFile($this->_js($js, true));
        }
    }

    /**
     * 获取JS URL
     *
     * @param String $jsName
     * @return String
     */
    public function _viewCssPush(array $cssList, $linkName = '_headLink')
    {
        $link = $this->_viewLink($linkName);
        foreach ($cssList as $css) {
            $link->appendFile($this->_css($css, true));
        }
    }

    /**
     * 获取Css URL
     *
     * @param String $cssName
     * @return String
     */
    public function _css($cssName, $local = false)
    {
        if (isset(StaticCss::$res[$cssName])) {
            if ($local == false) {
                $css = $this->_appConfig['website']['static']['url'] . StaticCss::$res[$cssName];
            } else {
                $css = StaticCss::$res[$cssName];
            }
            return $css;
        }
        return '';
    }

    public function result($code, $message, $data = array())
    {
        return array(
            'code' => $code,
            'message' => $message,
            'data' => $data
        );
    }

    public function resultJson($code, $message, $data = array())
    {
        header('Content-type: application/json');
        die(json_encode($this->result($code, $message, $data)));
    }
}