123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace App\Services\DaborySearch;
- use App\Exceptions\ApiException;
- use Exception;
- use Unirest\Request;
- class SearchService
- {
- public $baseUrl;
- public function __construct()
- {
- $this->baseUrl = env('SEARCH_HOST');
- }
- public function textHubSearch($dbrhubUrl, $offset, $limit, $query = '', $sort = '')
- {
- $data = [
- 'DbrhubUrl' => $dbrhubUrl,
- 'Query' => $query,
- 'Offset' => $offset,
- 'Limit' => $limit,
- ];
- return $this->callTextSearch($data, $sort);
- }
- public function dItemPick($id)
- {
- try {
- $response = Request::post($this->baseUrl . '/ditem-pick',
- [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', ],
- json_encode([
- 'Page' => [
- [ 'Id' => $id ]
- ]
- ], JSON_UNESCAPED_UNICODE)
- );
- } catch (Exception $e) {
- throw new ApiException($e->getMessage(), 503);
- }
- if ($response->code === 200) {
- $data = json_encode($response->body ?? []);
- return json_decode($data, true);
- } else {
- return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
- }
- }
- public function textSearch($originUrl, $offset, $limit, $query = '', $sort = '')
- {
- $data = [
- 'OriginUrl' => $originUrl,
- 'Query' => $query,
- 'Offset' => $offset,
- 'Limit' => $limit,
- ];
- return $this->callTextSearch($data, $sort);
- }
- public function callTextSearch($request, $sort = '')
- {
- if ($sort) {
- $sortExplode = explode(' ', $sort);
- switch ($sortExplode[1]) {
- case 'Asc':
- $request = array_merge($request, [ 'Asc' => $sortExplode[0] ]);
- break;
- case 'Desc':
- $request = array_merge($request, [ 'Desc' => $sortExplode[0] ]);
- break;
- }
- } else {
- $request = array_merge($request, [ 'Asc' => 'ItemName' ]);
- }
- try {
- $response = Request::post($this->baseUrl . '/ditem-page',
- [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', ],
- json_encode([
- 'ElaPageVars' => $request
- ], JSON_UNESCAPED_UNICODE)
- );
- } catch (Exception $e) {
- throw new ApiException($e->getMessage(), 503);
- }
- if ($response->code === 200) {
- $data = json_encode($response->body ?? []);
- return json_decode($data, true);
- } else {
- return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
- }
- }
- public function autocomplete($originUrl, $query)
- {
- $request = [
- 'OriginUrl' => $originUrl,
- 'Query' => $query,
- ];
- return $this->callAutocomplete($request);
- }
- public function autocompleteHub($dbrhubUrl, $query)
- {
- $request = [
- 'DbrhubUrl' => $dbrhubUrl,
- 'Query' => $query,
- ];
- return $this->callAutocomplete($request);
- }
- public function callAutocomplete($request)
- {
- try {
- $response = Request::get($this->baseUrl . '/autocomplete',
- [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', ],
- $request
- );
- } catch (Exception $e) {
- throw new ApiException($e->getMessage(), 503);
- }
- if ($response->code === 200) {
- $data = json_encode($response->body ?? []);
- return json_decode($data, true);
- } else {
- return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
- }
- }
- }
|