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. //
  19. // Log::info('接口请求参数------------');
  20. // Log::info($input);
  21. }
  22. /**
  23. * 返回正确的接口数据
  24. * @param array $data
  25. * @return JsonResponse
  26. */
  27. public static function successResponse($data = [])
  28. {
  29. $status = ApiEnum::STATUS_CODE_SUCCESS;
  30. $msg = ApiEnum::TRANSFER_API_MSG[$status];
  31. $msgpack = [
  32. 'code' => $status, //返回状态码
  33. 'message' => $msg, //返回信息
  34. 'content' => $data, //返回数据
  35. ];
  36. return Response::json($msgpack);
  37. }
  38. /**
  39. * 返回错误的接口数据
  40. * @param string $errno
  41. * @return JsonResponse
  42. */
  43. public static function errorResponse($errno)
  44. {
  45. if (!isset(ApiEnum::TRANSFER_API_MSG[$errno])) {
  46. $errno = ApiEnum::STATUS_CODE_FAIL;
  47. }
  48. $msg = ApiEnum::TRANSFER_API_MSG[$errno];
  49. $msgpack = [
  50. 'code' => $errno, //返回状态码
  51. 'message' => $msg, //返回信息
  52. ];
  53. return Response::json($msgpack);
  54. }
  55. }