BasicTrait.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Http\Traits\FetchesData;
  3. use Carbon\Carbon;
  4. trait BasicTrait
  5. {
  6. public function pick($url, $page, $isAjax = false)
  7. {
  8. $pick = $this->callApiService->callApi([
  9. 'url' => $url . '-pick',
  10. 'data' => [
  11. 'Page' => [ $page ]
  12. ],
  13. ], $isAjax);
  14. if ($this->callApiService->verifyApiError($pick)) {
  15. return $this->responseWithError($pick['body'], $pick['apiStatus']);
  16. }
  17. return $this->responseWithSuccess($pick['Page'][0]);
  18. }
  19. public function act($url, $page, $isAjax = false)
  20. {
  21. $act = $this->callApiService->callApi([
  22. 'url' => $url . '-act',
  23. 'data' => [
  24. 'Page' => [ $page ]
  25. ],
  26. ], $isAjax);
  27. if ($this->callApiService->verifyApiError($act)) {
  28. return $this->responseWithError($act['body'], $act['apiStatus']);
  29. }
  30. return $this->responseWithSuccess($act);
  31. }
  32. public function responseWithError($message, $code): array
  33. {
  34. return [
  35. 'Success' => false,
  36. // 'Message' => $message ??_e('API request failed. Please Check'),
  37. // 'Error' => [
  38. // 'Code' => $code,
  39. // 'Time' => Carbon::now()->timestamp
  40. // ],
  41. 'apiStatus' => $code,
  42. 'body' => $message ??_e('API request failed. Please Check'),
  43. ];
  44. }
  45. public function responseWithSuccess($data, $message = 'Success')
  46. {
  47. return [
  48. 'Success' => true,
  49. 'Message' => $message,
  50. 'Data' => $data,
  51. ];
  52. }
  53. }