diff --git a/app/Http/Controllers/Site/ReportController.php b/app/Http/Controllers/Site/ReportController.php index 566c52e..ee339d5 100644 --- a/app/Http/Controllers/Site/ReportController.php +++ b/app/Http/Controllers/Site/ReportController.php @@ -9,6 +9,7 @@ use App\Models\Cnt; use App\Models\Setting; use App\Models\ConfigStatic; use App\Models\Customer; +use App\Services\LimanService; use App\Models\Price; use App\Models\PriceCandle; use App\Models\PriceTrigger; @@ -211,16 +212,15 @@ class ReportController extends Controller CURLOPT_TCP_FASTOPEN => 1, ] ]) - ->withToken(Cnt::where('key', 'liman')->value('value')) + ->withToken(Setting::where('key', 'liman_token')->value('value')) ->get('http://212.23.201.173/server/customer/product/list') ->json(); if ((isset($liman['message']) && $liman['message'] == 'no authorised') || $liman == null) { - $cnt = new Cnt(); - $cnt->liman_login(); + $newToken = app(LimanService::class)->liman_login(); - $liman = Http::timeout(2) - ->withToken(Cnt::where('key', 'liman')->value('value')) + $liman = Http::timeout(7) + ->withToken($newToken) ->get('http://212.23.201.173/server/customer/product/list') ->json(); } diff --git a/app/Services/ConfigService.php b/app/Services/ConfigService.php index 5bf3900..64cd63b 100644 --- a/app/Services/ConfigService.php +++ b/app/Services/ConfigService.php @@ -2,17 +2,17 @@ namespace App\Services; -use Illuminate\Support\Facades\Http; -use League\Flysystem\Config; +use App\Models\Setting; class ConfigService { - public static function Config($name) + public function Config($name) { - return Setting::where('key',$name)->first()->value ?? false; + return Setting::where('key', $name)->value('value') ?? false; } - public static function UpdateConfig($name,$value) + + public function UpdateConfig($name, $value) { - return Setting::where('key',$name)->update(['value'=>$value]); + return Setting::where('key', $name)->update(['value' => $value]); } -} +} \ No newline at end of file diff --git a/app/Services/LimanService.php b/app/Services/LimanService.php index fcf44b4..6dbeaf4 100644 --- a/app/Services/LimanService.php +++ b/app/Services/LimanService.php @@ -3,23 +3,31 @@ namespace App\Services; use Illuminate\Support\Facades\Http; -use League\Flysystem\Config; -use app\services\ConfigService; class LimanService { - public function liman_login(){ - $mobile = app(ConfigService::class)->Config('liman_mobile'); - $password = app(ConfigService::class)->Config('liman_password'); + public function liman_login() + { + $config = app(ConfigService::class); - $liman = Http::post('http://212.23.201.173/server/auth/login', [ - 'mobile' => $mobile,//شماره اکانت اپلیکیشن هنزا تو لیمان ایدی 1377 + $mobile = $config->Config('liman_mobile'); + $password = $config->Config('liman_password'); + + $liman = Http::timeout(10)->post('http://212.23.201.173/server/auth/login', [ + 'mobile' => $mobile, 'password' => $password, 'provider' => 'customer', 'type_app' => 'client', ])->json(); - app(ConfigService::class)->UpdateConfig('liman_token', $liman['token']); - return $liman['access_token']; + $token = $liman['access_token'] ?? $liman['token'] ?? null; + + if (!$token) { + throw new \Exception('Liman login failed: ' . json_encode($liman, JSON_UNESCAPED_UNICODE)); + } + + $config->UpdateConfig('liman_token', $token); + + return $token; } -} +} \ No newline at end of file