fix some issu

This commit is contained in:
2026-08-26 21:47:09 +03:30
parent 74cc50149f
commit 40b185187c
14 changed files with 179 additions and 72 deletions
+57 -17
View File
@@ -31,11 +31,22 @@ class MontzarService
return $this->baseUrl() . '/' . ltrim($path, '/');
}
/** Pending HTTP با همان گزینه‌های پایدار (بدون پروکسی / IPv4 / HTTP1.1) */
private function http(float $timeout = 10)
{
$extraCurl = defined('CURLOPT_TCP_FASTOPEN')
? [(int) constant('CURLOPT_TCP_FASTOPEN') => 1]
: [];
return Http::timeout($timeout)
->acceptJson()
->withOptions(Controller::httpClientOptions($extraCurl, $timeout));
}
public function montzarLogin(): string
{
try {
$response = Http::timeout(10)
->acceptJson()
$response = $this->http(10)
->post($this->endpoint('/server/auth/login'), [
'mobile' => $this->config()->Config('montzar_mobile'),
'password' => $this->config()->Config('montzar_password'),
@@ -89,25 +100,57 @@ class MontzarService
}
public function productList(): array
{
$attempts = 3;
$lastError = null;
for ($attempt = 1; $attempt <= $attempts; $attempt++) {
try {
return $this->productListOnce();
} catch (\Throwable $e) {
$lastError = $e;
Log::warning('Montzar productList attempt failed', [
'attempt' => $attempt,
'max' => $attempts,
'base_url' => $this->baseUrl(),
'error' => $e->getMessage(),
]);
if ($attempt < $attempts) {
usleep(250_000 * $attempt);
}
}
}
throw $lastError ?? new \RuntimeException('Montzar product list failed');
}
private function productListOnce(): array
{
$token = $this->getToken();
$response = Http::timeout(7)
->acceptJson()
->withOptions(Controller::httpClientOptions(
defined('CURLOPT_TCP_FASTOPEN') ? [(int) constant('CURLOPT_TCP_FASTOPEN') => 1] : []
))
->withToken($token)
->get($this->endpoint('/server/customer/product/list'))
->json();
try {
$response = $this->http(7)
->withToken($token)
->get($this->endpoint('/server/customer/product/list'))
->throw()
->json();
} catch (\Throwable $e) {
// توکن مرده / قطع شبکه — یک بار لاگین مجدد
$token = $this->montzarLogin();
$response = $this->http(7)
->withToken($token)
->get($this->endpoint('/server/customer/product/list'))
->throw()
->json();
}
if ((isset($response['message']) && $response['message'] === 'no authorised') || $response === null) {
$token = $this->montzarLogin();
$response = Http::timeout(7)
->acceptJson()
$response = $this->http(7)
->withToken($token)
->get($this->endpoint('/server/customer/product/list'))
->throw()
->json();
}
@@ -263,9 +306,7 @@ class MontzarService
private function sendColleagueLedgerHttp(string $method, string $url, string $token, ?array $payload)
{
$pending = Http::timeout(30)
->acceptJson()
->withToken($token);
$pending = $this->http(30)->withToken($token);
if (strtoupper($method) === 'GET') {
return $pending->get($url, $payload ?? []);
@@ -368,8 +409,7 @@ class MontzarService
private function postToMontzar(string $url, string $token, array $payload)
{
return Http::timeout(15)
->acceptJson()
return $this->http(15)
->withToken($token)
->post($url, $payload);
}