Controller.php 843 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Constants\HttpStatus;
  4. use Illuminate\Http\JsonResponse;
  5. abstract class Controller
  6. {
  7. protected function success($data = [], $msg = '')
  8. {
  9. return response()->json([
  10. 'code' => HttpStatus::OK,
  11. 'timestamp' => time(),
  12. 'msg' => (!empty($msg) ? lang($msg) : 'ok'),
  13. 'data' => $data
  14. ]);
  15. }
  16. protected function error($code, string $msg = '', $data = []): JsonResponse
  17. {
  18. $code = intval($code);
  19. if ($code === 0) $code = -1;
  20. $m = __('messages.' . $code);
  21. if ($msg) $m .= ":{$msg}";
  22. if ($code === -3) $m = $msg;
  23. return response()->json([
  24. 'code' => $code,
  25. 'timestamp' => time(),
  26. 'msg' => $m,
  27. 'data' => $data
  28. ]);
  29. }
  30. }