Api23GateTokenController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\Api23GateTokenService;
  5. use App\Services\CallApiService;
  6. use App\Interfaces\WithdrawInterface;
  7. use App\Interfaces\DormantInterface;
  8. use Exception;
  9. use Illuminate\Http\Request;
  10. class Api23GateTokenController extends Controller
  11. {
  12. private $callApiService;
  13. private $withdrawService;
  14. private $dormantService;
  15. private $api23GateTokenService;
  16. public function __construct(
  17. CallApiService $callApiService, WithdrawInterface $withdrawService,
  18. Api23GateTokenService $api23GateTokenService, DormantInterface $dormantService
  19. )
  20. {
  21. $this->callApiService = $callApiService;
  22. $this->withdrawService = $withdrawService;
  23. $this->dormantService = $dormantService;
  24. $this->api23GateTokenService = $api23GateTokenService;
  25. }
  26. public function api23Cronjobs(Request $request)
  27. {
  28. $result = $this->api23GateTokenService->getToken($request->header('Api23Key'));
  29. if ($result['error']) {
  30. return response()->json($result['message'], 401);
  31. }
  32. return $this->dormantService->execute($result['data']);
  33. // $this->withdrawService->execute($result['data']);
  34. }
  35. public function api23Js(Request $request)
  36. {
  37. $result = $this->api23GateTokenService->getToken($request->header('Api23Key'));
  38. if ($result['error']) {
  39. return response()->json($result['message'], 401);
  40. }
  41. $request = new \Illuminate\Http\Request([
  42. 'url' => $request->header('Url'),
  43. 'data' => $request->getContent(),
  44. 'encode_status' => true,
  45. 'headers' => $this->buildHeader($result['data']),
  46. ]);
  47. return $this->callApiService->getData($request);
  48. }
  49. public function api23App(Request $request)
  50. {
  51. try {
  52. return $this->api23GateTokenService->callGateTokenApi($request->header('Api23Key'));
  53. } catch (Exception $e) {
  54. return response()->json('Unauthorized', 401);
  55. }
  56. }
  57. private function buildHeader($gateToken = false): array
  58. {
  59. $headers = [
  60. 'Content-Type' => 'application/json',
  61. 'FrontendHost' => url('/'),
  62. 'RemoteIp' => request()->ip(),
  63. 'Referer' => request()->headers->get('referer'),
  64. ];
  65. if ($gateToken) {
  66. $headers['GateToken'] = $gateToken;
  67. }
  68. return $headers;
  69. }
  70. public function tokenNotFoundResponse()
  71. {
  72. return response()->json('GateToken Not Found', 505);
  73. }
  74. }