<?php
/**
 * Created by PhpStorm.
 * User: qiuzijian
 * Date: 2021-04-25
 * Time: 09:54
 */

namespace App\Http\Controllers\Api;

use App\Enum\ApiEnum;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response;

class BaseController extends Controller {

    public function __construct()
    {
        $input = Input::all();
        Log::info('接口请求地址------------');
        Log::info($_SERVER['REQUEST_URI']);
        Log::info('接口请求参数------------');
        Log::info($input);
    }

    /**
     * 返回正确的接口数据
     * @param array $data
     * @return JsonResponse
     */
    public static function successResponse($data = [])
    {
        $status = ApiEnum::STATUS_CODE_SUCCESS;

        $msg = ApiEnum::TRANSFER_API_MSG[$status];

        $msgpack = [
            'code'    => $status,  //返回状态码
            'message' => $msg,  //返回信息
            'content' => $data,  //返回数据
        ];

        return Response::json($msgpack);
    }

    /**
     * 返回错误的接口数据
     * @param string $errno
     * @return JsonResponse
     */
    public static function errorResponse($errno)
    {
        if (!isset(ApiEnum::TRANSFER_API_MSG[$errno])) {
            $errno = ApiEnum::STATUS_CODE_FAIL;
        }

        $msg = ApiEnum::TRANSFER_API_MSG[$errno];

        $msgpack = [
            'code'    => $errno,  //返回状态码
            'message' => $msg,  //返回信息
        ];

        return Response::json($msgpack);
    }

    protected function error($code = 1, $msg = '')
    {
        return [
            'code'    => $code,
            'message' => $msg,
        ];
    }

    protected function success($data = '')
    {
        return [
            'code'    => 0,
            'message' => 'SUCCESS',
            'data'    => $data,
        ];
    }
}