DormantService-old.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Services;
  3. class DormantService
  4. {
  5. private $callApiService;
  6. private $gateToken;
  7. public function __construct(CallApiService $callApiService)
  8. {
  9. $this->callApiService = $callApiService;
  10. }
  11. public function setGateToken($gateToken)
  12. {
  13. return $this->gateToken = $gateToken;
  14. }
  15. public function getGateToken($type)
  16. {
  17. if ($type === 'all') {
  18. return $this->gateToken;
  19. }
  20. return $this->gateToken[$type ?: 'main'];
  21. }
  22. public function setMainToken($appBase64 = '')
  23. {
  24. $query = [
  25. 'ClientId' => config('app.api.main.ClientId'),
  26. 'BeforeBase64' => config('app.api.main.BeforeBase64'),
  27. 'AppBase64' => $appBase64
  28. ];
  29. $this->gateToken['main'] = $this->callApiService->getGateToken($query, 'main')['GateToken'];
  30. }
  31. public function getSetup()
  32. {
  33. $dormantDefault = $this->callApiService->callApi([
  34. 'url' => 'setup-get',
  35. 'data' => [
  36. 'SetupCode' => 'dormant-default',
  37. ],
  38. 'headers' => [
  39. 'GateToken' => $this->gateToken['main']
  40. ]
  41. ]);
  42. if ($dormantDefault['GuestApp']) {
  43. $appGuest = $this->getAppGuestPage($dormantDefault['GuestApp']);
  44. $this->setAppGateToken($appGuest['AppName'], $appGuest['ApiUri'], $appGuest['AppBase64']);
  45. }
  46. return $dormantDefault;
  47. }
  48. public function getAppGuestPage($appName)
  49. {
  50. return $this->callApiService->callApi([
  51. 'url' => 'app-guest-page',
  52. 'data' => [
  53. 'PageVars' => [
  54. 'Query' => "app_name = '$appName' and is_on_use = 1",
  55. 'Limit' => 1,
  56. ]
  57. ],
  58. 'headers' => [
  59. 'GateToken' => $this->gateToken['main']
  60. ]
  61. ])['Page'][0];
  62. }
  63. public function setAppGateToken($appName, $apiUri, $appBase64)
  64. {
  65. $response = \Unirest\Request::post(
  66. $apiUri . '/gate-token-get',
  67. [ 'Accept' => 'application/json' ],
  68. [ 'AppBase64' => $appBase64 ]
  69. );
  70. if ($response->code == 200) {
  71. $data = json_encode($response->body ?? []);
  72. $this->gateToken[$appName] = json_decode($data, true);
  73. } else {
  74. return false;
  75. }
  76. }
  77. }