<?php
namespace Helpers;

trait ApiResponse
{
    
    /**
     * 成功返回格式
     *
     * @param array $data
     * @param string $status
     * @param array $headers
     *            $headers['content-type'] = 'application/json'
     * @return void
     * @throws \Exception
     */
    public function success($data = [], $status = "success", $headers = [])
    {
        $this->respond(0, $status, '', $data, $headers);
    }
    
    /**
     * 失败返回格式，有失败原因
     *
     * @param [type] $code
     * @param string $reason
     * @param array $headers
     * @return void
     * @throws \Exception
     */
    public function failed($code, $status = "failed", $reason = '', $headers = [])
    {
        $this->respond($code, $status, $reason, [], $headers);
    }
    
    public function respond($code, $status, $reason, array $data = [], $headers = [])
    {
        $resp = [
            'code' => $code,
            'status' => $status
        ];
        if ($reason) {
            $resp['reason'] = $reason;
        }
        
        if ($data && ! is_array($data)) {
            $data = json_decode(strval($data), true);
            if ($$data === NULL) {
                throw new \Exception('非json格式', 13);
            }
        }
        
        $response = \Yaf\Dispatcher::getInstance()->getResponse();
        $response->setHeader('content-type', 'application/json');
        
        if ($data) {
            $apiHeader = "";
            if (isset($data['code'])) {
                $apiHeader = $apiHeader . strval($data['code']);
            }
            if (isset($_SESSION['userid'])) {
                $apiHeader = $apiHeader . "_" . strval($_SESSION['userid']);
            }
            if (strlen($apiHeader) > 0) {
                $response->setHeader('Api-Result', $apiHeader);
            }
        }
        
        // headder中输出主要依赖服务的处理时间
        if (isset($GLOBALS['DEPENDENT-REQUEST-TIME'])) {
            $response->setHeader('Dependent-Request-Time', $GLOBALS['DEPENDENT-REQUEST-TIME']);
        }
        if (isset($GLOBALS['DEPENDENT-STATUS'])) {
            $response->setHeader('Dependent-Status', $GLOBALS['DEPENDENT-STATUS']);
        }
        if (isset($GLOBALS['DEPENDENT-URI'])) {
            $response->setHeader('Actual-Request-Url', $GLOBALS['DEPENDENT-URI']);
        }
        
        if ($headers) {
            foreach ($headers as $name => $value) {
                $response->setHeader($name, $value);
            }
        }
        $data = array_merge($resp, $data);
        $response->setBody(json_encode($data, JSON_UNESCAPED_UNICODE));
        $response->response();
        if (function_exists('fastcgi_finish_request')) {
            ignore_user_abort(true);
            fastcgi_finish_request();
        }
    }
}