Index.php 2.59 KB
<?php

class IndexController extends QLib_Actions_Api
{
    public function init()
    {
        Yaf_Dispatcher::getInstance()->autoRender(FALSE);
    }

    public function indexAction()
    {
        $params = array_merge($this->_getRequests(), $this->_getParams());
        $module = empty($params['module']) ? '' : $params['module'];
        $this->moduleMap = $moduleMap = explode('.', $module);
        if (count($moduleMap) !== 3) {
            $this->helpJsonResult(500, 'module 格式不正确.');
        }
        $params = array_merge($this->_getRequests(), $this->_getParams());
        if (empty($params['client_secret'])) {
            $this->helpJsonResult(500, ' 参数 client_secret 不能为空.');
        }
        if (empty($params['client_version'])) {
            $this->helpJsonResult(500, ' 参数 client_version 不能为空.');
        }
        if (empty($params['client_type'])) {
            $this->helpJsonResult(500, ' 参数 client_type 不能为空.');
        }
        //app.passport.signin
        list($project, $class, $function) = $this->moduleMap;
        //YHMApi_App_V1_Passport
        $v = empty($params['v']) ? 1 : $params['v'];
        $className = 'YHMApi_' . ucwords($project) . '_V' . $v . '_' . ucwords($class);
        $classPathFile = $this->_config()->application->brightKnight . Q_DS . implode(Q_DS, explode('_', $className)) . '.php';
        if (file_exists($classPathFile) === false) {
            $this->helpJsonResult(500, ' 没有这个接口. ');
        }
        if (class_exists($className) === false || method_exists($className, $function) === false) {
            $this->helpJsonResult(500, ' 没有这个接口方法.');
        }
        if (method_exists($className, 'init') === true) {
            $className::init($params);
        }
        $returnType = empty($params['return_type']) ? 'json' : $params['return_type'];
        $fields = empty($params['fields']) ? '*' : $params['fields'];
        $result = $className::$function ($params, $fields);
        switch ($returnType) {
            case 'txt':
                echo $result;
                break;
            case 'jsonp':
                header('Content-type: application/json');
                if (empty($params['callback'])) {
                    $this->helpJson($result);
                } else {
                    exit($params['callback'] . '(' . json_encode($result) . ')');
                }
                break;
            case 'sfxml':
                echo stripslashes($result);
                break;
            default:
                $this->helpJson($result);
                break;
        }
    }
}