SearchService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Services\DaborySearch;
  3. use App\Exceptions\ApiException;
  4. use Exception;
  5. use Unirest\Request;
  6. class SearchService
  7. {
  8. public $baseUrl;
  9. public function __construct()
  10. {
  11. $this->baseUrl = env('SEARCH_HOST');
  12. }
  13. public function textHubSearch($dbrhubUrl, $offset, $limit, $query = '', $sort = '')
  14. {
  15. $data = [
  16. 'DbrhubUrl' => $dbrhubUrl,
  17. 'Query' => $query,
  18. 'Offset' => $offset,
  19. 'Limit' => $limit,
  20. ];
  21. return $this->callTextSearch($data, $sort);
  22. }
  23. public function dItemPick($id)
  24. {
  25. try {
  26. $response = Request::post($this->baseUrl . '/ditem-pick',
  27. [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', ],
  28. json_encode([
  29. 'Page' => [
  30. [ 'Id' => $id ]
  31. ]
  32. ], JSON_UNESCAPED_UNICODE)
  33. );
  34. } catch (Exception $e) {
  35. throw new ApiException($e->getMessage(), 503);
  36. }
  37. if ($response->code === 200) {
  38. $data = json_encode($response->body ?? []);
  39. return json_decode($data, true);
  40. } else {
  41. return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
  42. }
  43. }
  44. public function textSearch($originUrl, $offset, $limit, $query = '', $sort = '')
  45. {
  46. $data = [
  47. 'OriginUrl' => $originUrl,
  48. 'Query' => $query,
  49. 'Offset' => $offset,
  50. 'Limit' => $limit,
  51. ];
  52. return $this->callTextSearch($data, $sort);
  53. }
  54. public function callTextSearch($request, $sort = '')
  55. {
  56. if ($sort) {
  57. $sortExplode = explode(' ', $sort);
  58. switch ($sortExplode[1]) {
  59. case 'Asc':
  60. $request = array_merge($request, [ 'Asc' => $sortExplode[0] ]);
  61. break;
  62. case 'Desc':
  63. $request = array_merge($request, [ 'Desc' => $sortExplode[0] ]);
  64. break;
  65. }
  66. } else {
  67. $request = array_merge($request, [ 'Asc' => 'ItemName' ]);
  68. }
  69. try {
  70. $response = Request::post($this->baseUrl . '/ditem-page',
  71. [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', ],
  72. json_encode([
  73. 'ElaPageVars' => $request
  74. ], JSON_UNESCAPED_UNICODE)
  75. );
  76. } catch (Exception $e) {
  77. throw new ApiException($e->getMessage(), 503);
  78. }
  79. if ($response->code === 200) {
  80. $data = json_encode($response->body ?? []);
  81. return json_decode($data, true);
  82. } else {
  83. return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
  84. }
  85. }
  86. public function autocomplete($originUrl, $query)
  87. {
  88. $request = [
  89. 'OriginUrl' => $originUrl,
  90. 'Query' => $query,
  91. ];
  92. return $this->callAutocomplete($request);
  93. }
  94. public function autocompleteHub($dbrhubUrl, $query)
  95. {
  96. $request = [
  97. 'DbrhubUrl' => $dbrhubUrl,
  98. 'Query' => $query,
  99. ];
  100. return $this->callAutocomplete($request);
  101. }
  102. public function callAutocomplete($request)
  103. {
  104. try {
  105. $response = Request::get($this->baseUrl . '/autocomplete',
  106. [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', ],
  107. $request
  108. );
  109. } catch (Exception $e) {
  110. throw new ApiException($e->getMessage(), 503);
  111. }
  112. if ($response->code === 200) {
  113. $data = json_encode($response->body ?? []);
  114. return json_decode($data, true);
  115. } else {
  116. return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
  117. }
  118. }
  119. }