| 123456789101112131415161718192021222324252627282930313233343536 |
- <?php
- namespace App\Http\Controllers;
- use App\Constants\HttpStatus;
- use Illuminate\Http\JsonResponse;
- abstract class Controller
- {
- protected function success($data = [], $msg = '')
- {
- return response()->json([
- 'code' => HttpStatus::OK,
- 'timestamp' => time(),
- 'msg' => (!empty($msg) ? lang($msg) : 'ok'),
- 'data' => $data
- ]);
- }
- protected function error($code, string $msg = '', $data = []): JsonResponse
- {
- $code = intval($code);
- if ($code === 0) $code = -1;
- $m = __('messages.' . $code);
- if ($msg) $m .= ":{$msg}";
- if ($code === -3) $m = $msg;
- return response()->json([
- 'code' => $code,
- 'timestamp' => time(),
- 'msg' => $m,
- 'data' => $data
- ]);
- }
- }
|