ResponseMessage.class.php 1.53 KB
<?php
require_once dirname(__FILE__).'/ResponseStatus.class.php';
require_once dirname(__FILE__).'/ResponseAjaxResponse.class.php';
/**
 * ajax回应类
 *
 */
class ResponseMessage
{
    /**
     * 状态对象
     * @var object
     */
    private $mStatus;
    
    /**
     * 前端结果对象
     * @var object
     */
    private $mAjaxResponse;
    
    /**
     * 初始化函数
     */
    public function __construct()
    {
        $this->mStatus = new ResponseStatus();
        $this->mAjaxResponse = new ResponseAjaxResponse();
    }
    
    /**
     * 设置ajaxResponse的data数据函数
     * 
     * @param $data
     * @param $content
     * @param $code
     */
    public function setData($data, $content = '', $code = AJAX_RESPONSE_STATUS_OK)
    {
        if(isset($data))
        {
             $this->mAjaxResponse->data = $data;
             $this->mStatus->content = $content;
             $this->mStatus->code = $code;
        }
    }
    
    /**
     * 设置错误信息函数
     * 
     * @param string $errorMsg
     * @param int $code
     */
    public function setError($errorMsg, $code = AJAX_RESPONSE_STATUS_FAIL)
    {
        if(isset($errorMsg))
        {
            $this->mStatus->content = $errorMsg;
            $this->mStatus->code = $code;
            $this->mAjaxResponse->data = null;
        }
    }
    
    /**
     * 返回结果
     * 
     * @return object
     */
    public function getResult()
    {
        $this->mAjaxResponse->status = $this->mStatus;
        return $this->mAjaxResponse;
    }
}
?>