SearchService.php-- 3.1 KB

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