Controller.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Foundation\Bus\DispatchesJobs;
  4. use Illuminate\Routing\Controller as BaseController;
  5. use Illuminate\Foundation\Validation\ValidatesRequests;
  6. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  7. class Controller extends BaseController
  8. {
  9. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  10. protected $error;
  11. function __construct()
  12. {
  13. $this->error = config("errorcode");
  14. }
  15. /**
  16. * HTTP请求成功时的数据返回
  17. *
  18. * @param array $data
  19. * @param string $message
  20. * @return \Illuminate\Http\JsonResponse
  21. */
  22. public function success($data = array(), $message = "success")
  23. {
  24. $return = array(
  25. 'code' => REQUEST_SUCCESS,
  26. 'message' => $message,
  27. 'data' => $data
  28. );
  29. return response()->json($return);
  30. }
  31. /**
  32. * HTTP请求失败时错误信息返回
  33. *
  34. * @param $code
  35. * @param string $codeMessage
  36. * @param null $message
  37. * @return \Illuminate\Http\JsonResponse
  38. */
  39. public function fail($code, $codeMessage = "", $message = null)
  40. {
  41. return response()->json([
  42. 'code' => (int)$code,
  43. "codeMessage" => $codeMessage,
  44. 'message' => $message
  45. ]);
  46. }
  47. /**
  48. * HTTP请求响应
  49. *
  50. * @param $code
  51. * @param string $codeMessage
  52. * @param null $message
  53. * @return \Illuminate\Http\JsonResponse
  54. */
  55. public function response($code, $codeMessage = "", $message = null)
  56. {
  57. return response()->json([
  58. 'code' => (int)$code,
  59. "codeMessage" => $codeMessage,
  60. 'message' => $message
  61. ]);
  62. }
  63. }