<?php
/**
 * Created by PhpStorm.
 * User: 24147
 * Date: 2019/8/1
 * Time: 14:45
 */

namespace App\Http\Traits;


trait Response
{
    /**
     * @param string $message
     * @param array $data
     * @param int $status_code
     * @return mixed
     */
    public function success($message = 'success', $data = [], $status_code = 200)
    {
        return self::responseJson(0, $message, $data, [], $status_code);
    }

    protected static function responseJson($code, $message, $data, $errors, $status_code)
    {
        $data = [
            'code' => $code,
            'message' => $message,
            'data' => $data,
            'time' => microtime(true) - LARAVEL_START,
            'errors' => $errors,
            'status_code' => $status_code,

        ];
        return response()->json($data, $status_code);
    }

    /**
     * 成功响应
     *
     * @param string $message
     * @param int $code
     * @param int $status_code
     * @param array $errors
     * @param null $data
     * @return mixed
     */
    public function error(string $message = 'error', int $code = 1, $status_code = 200, $errors = [], $data = null)
    {
        return self::responseJson($code, $message, $data, $errors, $status_code);
    }
}
