BaseController.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qiuzijian
  5. * Date: 2021-04-25
  6. * Time: 09:54
  7. */
  8. namespace App\Http\Controllers\Api;
  9. use App\Enum\ApiEnum;
  10. use Illuminate\Routing\Controller;
  11. use Illuminate\Support\Facades\Input;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Response;
  14. class BaseController extends Controller {
  15. public function __construct()
  16. {
  17. $input = Input::all();
  18. Log::info('接口请求参数------------');
  19. Log::info($input);
  20. }
  21. /**
  22. * 返回正确的接口数据
  23. * @param array $data
  24. * @return JsonResponse
  25. */
  26. public static function successResponse($data = [])
  27. {
  28. $status = ApiEnum::STATUS_CODE_SUCCESS;
  29. $msg = ApiEnum::TRANSFER_API_MSG[$status];
  30. $msgpack = [
  31. 'code' => $status, //返回状态码
  32. 'message' => $msg, //返回信息
  33. 'content' => $data, //返回数据
  34. ];
  35. return Response::json($msgpack);
  36. }
  37. /**
  38. * 返回错误的接口数据
  39. * @param string $errno
  40. * @return JsonResponse
  41. */
  42. public static function errorResponse($errno)
  43. {
  44. if (!isset(ApiEnum::TRANSFER_API_MSG[$errno])) {
  45. $errno = ApiEnum::STATUS_CODE_FAIL;
  46. }
  47. $msg = ApiEnum::TRANSFER_API_MSG[$errno];
  48. $msgpack = [
  49. 'code' => $errno, //返回状态码
  50. 'message' => $msg, //返回信息
  51. ];
  52. return Response::json($msgpack);
  53. }
  54. }