Action.php 9.62 KB
<?php

class QLib_Action extends Q_Action
{
    private $_appConfig = array();

    private $_sessionNamespace = array();

    protected $domain = '';

    protected $staticDomain = '';

    public function init()
    {
        $this->_appConfig = $this->_config();
        $this->domain = $this->_appConfig['website']['domain'];
        $this->staticDomain = $this->_appConfig['website']['static']['url'];
    }

    /**
     * @param $template
     * @return QLib_Paging
     */
    public function _paging($template = 'paging')
    {
        $paging = new QLib_Paging($template);
        return $paging;
    }

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

    /**
     * 获取初始化注册的config
     * @param string $name
     * @return mixed
     */
    public function _config($name = 'config')
    {
        return Yaf_Registry::get($name);
    }


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

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

    /**
     * TODO
     * @param string $layout
     * @param string $layoutPath
     */
    public function _setLayout($layout = 'default', $layoutPath = '')
    {
        if (!empty($layoutPath)) {
            $this->_config = Yaf_Application::app()->getConfig();
            $this->setLayoutPath();
        }
        $this->getView()->setLayout($layout);
    }

    /**
     * 获取参数
     * @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();
    }

    protected function _p()
    {
        return $_POST;
    }

    protected function _g()
    {
        return $_GET;
    }

    protected function requestParams()
    {
        return $_POST + $_GET;
    }

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

    /**
     *
     * @param unknown $key
     * @param string $default
     */
    protected function _param($key, $default = null)
    {
        $reqObj = $this->getRequest();
        if ($reqObj->isGet()) {
            return $reqObj->getQuery($key, $default);
        } else {
            return $reqObj->getPost($key, $default);
        }
    }

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

    /**
     * 关闭模板 & Layout
     */
    public function _disable()
    {
        $this->disableLayout();
        $this->disableView();
    }

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

    /**
     * 关闭Layout
     */
    public function disableLayout()
    {
        $this->getView()->setLayout('');
    }


    /**
     * 使用session
     * @param $namespace
     * @return Q_Core_SessionNamespace
     */
    public function _sessionNamespace($namespace)
    {
        $this->sessionStart();
        if (empty($this->_sessionNamespace[$namespace])) {
            $this->_sessionNamespace[$namespace] = new Q_Core_SessionNamespace ($namespace);
        }
        return $this->_sessionNamespace[$namespace];
    }

    /**
     * 需要验证请调用该函数
     * @param string $refer
     * @return int
     */
    public function _auth($refer = 'auto')
    {
        $this->sessionStart();
        if (($this->uid = $this->getProfileSession('pid')) < 1) {
            $referUrl = $refer == 'auto' ? $_SERVER['REQUEST_URI'] : $refer;
            $redirectUrl = '/?refer=' . rawurlencode($referUrl);
            header('Location: ' . $redirectUrl);
        }
        return $this->uid;
    }

    public function getProfileSession($keyName = null)
    {
        $profileSession = $this->_sessionNamespace('adminx_profile')->__get(QLibConfigs_GlobalConfig::$session_name);
        if (empty($keyName)) {
            return $profileSession;
        }
        return $profileSession['pid'];
    }

    /**
     * 获取token_session_key
     * @return mixed
     */
    public function _sessionToken()
    {
        $this->sessionStart();
        $session = QINAuth_Factory::profile('mini_web');
        return $session->getSession('token_session_key');
    }

    /**
     * 获取用户ID判断是否登录
     * @return int
     */
    public function _getUid()
    {
        $this->sessionStart();
        $this->uid = QINAuth_Factory::profile('mini_web')->getSession();
        return $this->uid;
    }

    /**
     * 跳转地址
     * @param string $refer
     * @return string
     */
    public function gotoUrl($refer = '')
    {
        $Uri = '';
        if ($refer == '') {
            $Uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        }
        $referer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];
        if ($Uri == $referer) {
            $gotoUrl = '/';
        } else {
            $gotoUrl = !empty($refer) ? rawurldecode($refer) : '/';
        }
        return $gotoUrl;
    }

    /**
     * 通过uid获取用户数据
     * @param integer $uid
     *
     */
    protected static function _getUserInfo($uid)
    {
        $baseInfo = YHMPassport_Models_Profile_Client::getByUid($uid);
        if (empty($baseInfo)) {
            return array();
        }
        //星级评定结果
        $store_info = YHMStore_Models_Store_Client::getByUid($uid);
        $star = YHMComment_Models_Comment_Client::getStarByStoreId($store_info['id']);
        $head_ico = empty($baseInfo['head_ico']) ? 'yhfair-user-head.d0861a87d5c6986dd490120524ff3e04.jpg' : $baseInfo['head_ico'];
        $backgroup = empty($baseInfo['background_img']) ? 'yhfair-user-background.7c1ab223b0aa88d95abd2d6a4ef6ddca.jpg' : $baseInfo['background_img'];
        $gender = empty($baseInfo['gender']) ? 0 : $baseInfo['gender'];
        $area_code = empty($baseInfo['area_code']) ? 0 : $baseInfo['area_code'];
        return array(
            'uid' => $uid,
            'store_id' => $store_info['id'],
            'nick_name' => $baseInfo['nick_name'],
            'gender' => $gender,
            'area_code' => $area_code,
            'identity' => $baseInfo['identity'],
            'head_ico' => 'http://yhfair-user-head.qiniudn.com/' . $head_ico,
            'background_img' => 'http://yhfair-user-background.qiniudn.com/' . $backgroup,
            'star' => $star,
            'store_type' => $store_info['store_type'], //3为Master,2为潮店,1为潮人
            'is_real_name' => 'N',
            'has_deposit' => 'N'
        );
    }

    /**
     * 为列表的商品信息获取数据
     * @param integer $product_skc
     */
    protected static function _getGoodsForList($product_skc)
    {
        $defalut_image = YHMProduct_Models_Goodsimages_Client::getDefaultImageBySkc($product_skc);
        if (empty($defalut_image)) {
            $defalut_image = YHMProduct_Models_Goodsimages_Client::getOneImageBySkc($product_skc);
        }
        return array(
            'product_skc' => $product_skc,
            'goods_image' => $defalut_image
        );
    }
}