Index.php 3.04 KB
<?php

class IndexController extends QLib_Action
{

    public function init()
    {
        Yaf_Dispatcher::getInstance()->autoRender(FALSE);
    }

    //http://api.beastore.com/?method=orders.orders.li
    public function indexAction()
    {
        $module = $this->_get('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 不能为空.');
        }
        list($project, $class, $function) = $this->moduleMap;
        $version = $this->_getParam('version', 1);
        $className = 'QModels_' . ucwords($project) . '_V' . $version . '_' . ucwords($class);
        $classPathFile = $this->_config()->application->library . Q_DS . implode(Q_DS, explode('_', $className)) . '.php';
        if (file_exists($classPathFile) === false) {
            $this->helpJsonResult(500, ' Error\'s Api Method.');
        }
        if (class_exists($className) === false || method_exists($className, $function) === false) {
            $this->helpJsonResult(500, ' Error\'s Api Class Or Function.');
        }
        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;
        }
    }

    //http://api.open.yohobuy.com/?method=product
    public function WebserviceAction()
    {
        $module = $this->_get('module');
        $moduleMap = explode('.', $module);
        if (count($moduleMap) !== 2) {
            throw new Yar_Server_Exception('module 格式不正确.');
        }
        list($project, $class) = $moduleMap;
        $className = 'QModels_' . ucwords($project) . '_' . ucwords($class);
        $classPathFile = $this->_config()->application->library . Q_DS . implode(Q_DS, explode('_', $className)) . '.php';
        if (file_exists($classPathFile) === false) {
            throw new Yar_Server_Exception('Not Class.');
        }
        $className = 'QModels_' . ucwords($project) . '_' . ucwords($class);
        $service = new Yar_Server(new $className());
        $service->handle();
    }
}