HiboxInterfaceTestController.php 3.02 KB
<?php
/**
 * Hibox接口测试类控制器
 */
class HiboxInterfaceTestController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column1', meaning
	 * using two-column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			//'accessControl',
		);
	}
	
	/**
	 * 首页
	 */
	public function actionIndex()
	{
	    $model = new HiboxInterfaceTestForm;
	    $this->performAjaxValidation($model);

	    $this->render('index', array(
	    	'model' => $model,
	    ));
	}
	
	/**
	 * AJAX方式测试接口
	 */
	public function actionAjaxTestInterface()
	{
	    $yiiRequest = Yii::app()->request;
	    $platform = $yiiRequest->getPost('PLAT', null);
	    $request = $yiiRequest->getPost('CALL', null);
	    
	    if ( isset($request, $platform) && array_key_exists($platform, ReplaceViewDataValue::getReplaced(95, null, false)) )
	    {
	        // 将字符串解析到变量中
	        parse_str($request);
	        // 加载的测试类文件名称
	        $testClassName = ReplaceViewDataValue::getReplaced(95, $platform);
	        
	        if ( isset($r, $CALL) && is_string($testClassName))
	        {
	            $GLOBALS["PLAT_DIR_PATH"] = $platform;
	            $GLOBALS["POST_PARAM_CALL"] = $CALL;
	            include realpath(Yii::app()->basePath. "/extensions/HiboxInterface/TestBase.php");
	            include realpath(Yii::app()->basePath. "/extensions/HiboxInterface/interface.php");
	            include realpath(Yii::app()->basePath. "/extensions/HiboxInterface/$testClassName.php");
	            
	            // 字符串分割为数组: array(0 => Action动作, 1 => Method方法)
	            $request = explode('/', $r);
                echo "输入格式为:". PHP_EOL;
                print_r($request);
                // 调用各个平台下的接口测试方法
	        	if (class_exists($testClassName, false) && is_callable(array($testClassName, $request[0]), false))
	            {
	                @call_user_func(array($testClassName, $request[0]), $request[1]);
	            }
	            else
	            {
	                echo "请检查接口是否存在.";
	            }
	            Yii::app()->end();
	        }
	    }
	}
	
	/**
	 * AJAX方式翻译JSON
	 */
	public function actionAjaxTransferJson()
	{
	    $data = "";
	    $jsonData = Yii::app()->request->getPost('JSON', null);
	    
	    if ( $jsonData !== null )
	    {
	        $jsonData = "{\"data\":\"$jsonData\"}";
	        $transferObject = json_decode($jsonData);

	        if ( is_object($transferObject) && isset($transferObject->data) )
	        {
	            $data = $transferObject->data;
	        }
	    }
	    echo $data;
	}
	
	/**
	 * AJAX方式验证表单
	 * @param object CModel
	 */
	protected function performAjaxValidation($model)
	{
		if (isset($_POST['ajax']) && $_POST['ajax']==='hibox-interface-test-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}