]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-356] call site api from mq
authorAlexander Smirnov <fredeom@mail.ru>
Tue, 4 Mar 2025 14:57:48 +0000 (17:57 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Tue, 4 Mar 2025 14:57:48 +0000 (17:57 +0300)
erp24/api2/controllers/BonusController.php
erp24/jobs/SendBonusInfoToSiteJob.php [new file with mode: 0644]
erp24/services/SiteService.php [new file with mode: 0644]

index 76b9222d309344baf55dfb2663c339a9de8c5997..2f5ad20c2a40e0686916749bbf40233babbc5cb0 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace app\controllers;
 
-use GuzzleHttp\Client;
+use app\jobs\SendBonusInfoToSiteJob;
 use DateTime;
 use DateTimeZone;
 use Yii;
@@ -961,6 +961,12 @@ class BonusController extends BaseController
                 LogService::apiErrorLog(json_encode(["error_id" => 5, "error" => $userBonus2->getErrors()], JSON_UNESCAPED_UNICODE));
 
                 return $this->asJson(["error_id" => 5, "error" => $userBonus2->getErrors()]);
+            } else {
+                Yii::$app->queue->push(new SendBonusInfoToSiteJob([
+                    'phone' => $phone,
+                    'bonus' => $back,
+                    'bonus_created_at' => date("Y-m-d H:i:s"),
+                ]));
             }
 
             file_put_contents(self::OUT_DIR . '/sale_bonuses_' . $fl . '.json', PHP_EOL . '--' . __LINE__, FILE_APPEND);
@@ -1012,6 +1018,12 @@ class BonusController extends BaseController
                     LogService::apiErrorLog(json_encode(["error_id" => 5.2, "error" => $userBonus2->getErrors()], JSON_UNESCAPED_UNICODE));
 
                     return $this->asJson(["error_id" => 5.2, "error" => $userBonus2->getErrors()]);
+                } else {
+                    Yii::$app->queue->push(new SendBonusInfoToSiteJob([
+                        'phone' => $phone,
+                        'bonus' => $back,
+                        'bonus_created_at' => date("Y-m-d H:i:s"),
+                    ]));
                 }
                 if ($userFound->telegram_created_at == null) {
                     $userFound->telegram_created_at = date("Y-m-d H:i:s");
@@ -1200,19 +1212,6 @@ class BonusController extends BaseController
         LogService::apiLogs(1, json_encode($mess, JSON_UNESCAPED_UNICODE));
       file_put_contents(self::OUT_DIR . '/sale_bonuses_' . $fl . '.json', PHP_EOL . '--' . __LINE__ . ' OK ', FILE_APPEND);
 
-        $client = new Client();
-        try {
-            $result = $client->post('https://api2.dev1.erp-flowers.ru/balance/test?key=getJH6GFi4tpU84YVPW9M__Xe_eQ24baWRFGl9ance', [
-                'phone' => $phone,
-                'balance' => $user->balans,
-                'created_at' => date('Y-m-d H:i:s'),
-            ]);
-            $mess['resultSiteSendAAA'] = json_encode($result->getBody()->getContents(), JSON_UNESCAPED_UNICODE);
-        } catch (\Exception $e) {
-            LogService::apiErrorLog(json_encode(["error_id" => 7, "error" => "Ошибка отправки сообщения на сайт: " .
-                $e->getMessage()], JSON_UNESCAPED_UNICODE));
-        }
-
         return $this->asJson($mess);
     }
 
diff --git a/erp24/jobs/SendBonusInfoToSiteJob.php b/erp24/jobs/SendBonusInfoToSiteJob.php
new file mode 100644 (file)
index 0000000..972b474
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace app\jobs;
+
+
+use Yii;
+use yii\queue\JobInterface;
+use yii_app\services\SiteService;
+
+class SendBonusInfoToSiteJob extends \yii\base\BaseObject implements JobInterface
+{
+    public $messageData;
+
+    public function execute($queue) {
+        $phone = ($this->messageData)['phone'];
+        $bonus = ($this->messageData)['bonus'];
+        $bonus_created_at = ($this->messageData)['bonus_created_at'];
+
+        try {
+            $result = SiteService::notifySiteAboutBonuses($phone, $bonus, $bonus_created_at);
+            if ($result == "OK") {
+                Yii::warning("Сообщение успешно отправлено на сайт", 'site');
+            } else {
+                Yii::warning("Сообщение не удалось отправить на сайт", 'site');
+            }
+        } catch (\Exception $e) {
+            Yii::error(
+                "Сообщение не удалось отправить на сайт: " . $e->getMessage(),
+                'site'
+            );
+        }
+    }
+}
diff --git a/erp24/services/SiteService.php b/erp24/services/SiteService.php
new file mode 100644 (file)
index 0000000..97c5583
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+namespace yii_app\services;
+
+use GuzzleHttp\Client;
+
+class SiteService {
+    public static function notifySiteAboutBonuses($phone, $bonus, $bonus_created_at) {
+        $client = new Client();
+        $result = null;
+        try {
+            $result = $client->post('https://api2.dev1.erp-flowers.ru/balance/test?key=getJH6GFi4tpU84YVPW9M__Xe_eQ24baWRFGl9ance', [
+                'phone' => $phone,
+                'bonus' => $bonus,
+                'created_at' => $bonus_created_at,
+            ]);
+            $result = $result->getBody()->getContents();
+        } catch (\Exception $e) {
+            LogService::apiErrorLog(json_encode(["error_id" => 7, "error" => "Ошибка отправки сообщения на сайт: " .
+                $e->getMessage()], JSON_UNESCAPED_UNICODE));
+        }
+        return $result;
+    }
+}