Skip to content

Instantly share code, notes, and snippets.

@ifreesec
Last active March 21, 2020 12:01
Show Gist options
  • Save ifreesec/cbacdd84e59bc5703cfc5c314c8057c7 to your computer and use it in GitHub Desktop.
Save ifreesec/cbacdd84e59bc5703cfc5c314c8057c7 to your computer and use it in GitHub Desktop.
简单封装了下 php 开发 API json化快捷输出结果的 Trait

以下是 laravel 版本,增加了 headers

OutFormatTrait.php

<?php
/**
 * 格式化输出数据
 *
 * Class outFormat
 * @package App\Sanjieke\Logic
 */
trait OutFormatTrait
{
    protected static $http_200 = 200;
    protected static $default_headers = [
        'Content-Type' => 'application/json; charset=UTF-8'
    ];

    /**
     *  成功,返回数据
     * @param array $data
     * @param string|array $msg
     * @return \Illuminate\Http\JsonResponse
     */
    protected function successOutDataMsg(array $data, $msg = 'ok')
    {
        return $this->outFormat(self::$http_200, $data, $msg);
    }

    /**
     * 成功只返回提示信息
     * @param string $msg
     * @return \Illuminate\Http\JsonResponse
     */
    protected function successOutMsg($msg = 'ok')
    {
        return $this->outFormat(self::$http_200, [], $msg);
    }

    /**
     * 返回错误 code 及 信息
     *
     * @param int $code
     * @param string|array $msg
     * @param array $data
     * @return \Illuminate\Http\JsonResponse
     */
    protected function errorOutFormat($code, $msg = '有点问题,请稍后重试', array $data = [])
    {
        return $this->outFormat($code, $data, $msg);
    }

    /**
     * 统一格式输出
     *
     * @param int $code
     * @param array|string $data
     * @param string|array $msg
     * @param array $headers
     * @param int $options
     * @return \Illuminate\Http\JsonResponse
     */
    protected function outFormat($code, $data, $msg, array $headers = [], $options = JSON_UNESCAPED_UNICODE)
    {
        return response()->json([
            'code' => $code,
            'data' => $data,
            'msg' => $msg,
        ], self::$http_200, array_merge(self::$default_headers, $headers), $options);
    }
}

普通 json_encode

OutFormatTrait.php

<?php
/**
 * 格式化输出数据
 *
 * Class outFormat
 * @package App\Sanjieke\Logic
 */
trait OutFormatTrait
{
    protected static $http_200 = 200;

    /**
     *  成功,返回数据
     * @param array $data
     * @param string|array $msg
     * @return string
     */
    protected function successOutDataMsg(array $data, $msg = 'ok')
    {
        return $this->outFormat(self::$http_200, $data, $msg);
    }

    /**
     * 成功只返回提示信息
     * @param string $msg
     * @return string
     */
    protected function successOutMsg($msg = 'ok')
    {
        return $this->outFormat(self::$http_200, [], $msg);
    }

    /**
     * 返回错误 code 及 信息
     *
     * @param int $code
     * @param string|array $msg
     * @param array $data
     * @return string
     */
    protected function errorOutFormat($code, $msg = '有点问题,请稍后重试', array $data = [])
    {
        return $this->outFormat($code, $data, $msg);
    }

    /**
     * 统一格式输出
     *
     * @param int $code
     * @param array|string $data
     * @param string|array $msg
     * @param int $options
     * @return string
     */
    protected function outFormat($code, $data, $msg, $options = JSON_UNESCAPED_UNICODE)
    {
        return json_encode([
            'code' => $code,
            'data' => $data,
            'msg' => $msg,
        ], $options);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment