Browse Source

向供货商 APP 发送新订单通知统一化

shizhongqi 9 months ago
parent
commit
de222707fe

+ 1 - 9
app-ghs/controllers/OrderController.php

@@ -1658,16 +1658,8 @@ class OrderController extends BaseController
                 //NoticeClass::ghsNewOrderNotice($order);
 
                 //向供货商APP发通知
-                $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId'=>$shopId]);
-                $cids = array_column($allDevices, 'clientId');
-                $title = '你有新的订单';
-                $content = $shop->shopName . ' ¥' . $order->actPrice;
-                $payload = [
-                    "page" => "pagesOrder/detail",
-                    "params" => ["id" => $order->id]
-                ];
                 $push = new push('ghs', push::MSG_TYPE_ORDER);
-                $push->pushByCloud($cids, $title, $content, $payload);
+                $push->ghsOrderMessage($shop, $order);
             }
         }
 

+ 3 - 27
app-hd/controllers/NoticeController.php

@@ -240,16 +240,8 @@ class NoticeController extends PublicController
                             Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
 
                             //向供货商APP发通知
-                            $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId'=>$shopId]);
-                            $cids = array_column($allDevices, 'clientId');
-                            $title = '你有新的订单';
-                            $content = $shop->shopName . ' ¥' . $order->actPrice;
-                            $payload = [
-                                "page" => "pagesOrder/detail",
-                                "params" => ["id" => $order->id]
-                            ];
                             $push = new push('ghs', push::MSG_TYPE_ORDER);
-                            $push->pushByCloud($cids, $title, $content, $payload);
+                            $push->ghsOrderMessage($shop, $order);
                         }
                         //花店采购供货商端APP收到通知
                         //NoticeClass::ghsNewOrderNotice($order);
@@ -479,16 +471,8 @@ class NoticeController extends PublicController
                             Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
 
                             //向供货商APP发通知
-                            $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId'=>$shopId]);
-                            $cids = array_column($allDevices, 'clientId');
-                            $title = '你有新的订单';
-                            $content = $shop->shopName . ' ¥' . $order->actPrice;
-                            $payload = [
-                                "page" => "pagesOrder/detail",
-                                "params" => ["id" => $order->id]
-                            ];
                             $push = new push('ghs', push::MSG_TYPE_ORDER);
-                            $push->pushByCloud($cids, $title, $content, $payload);
+                            $push->ghsOrderMessage($shop, $order);
                         }
                         //花店采购供货商端APP收到通知
                         //NoticeClass::ghsNewOrderNotice($order);
@@ -872,16 +856,8 @@ class NoticeController extends PublicController
                                 Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
 
                                 //向供货商端APP发通知
-                                $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId'=>$shopId]);
-                                $cids = array_column($allDevices, 'clientId');
-                                $title = '你有新的订单';
-                                $content = $shop->shopName . ' ¥' . $order->actPrice;
-                                $payload = [
-                                    "page" => "pagesOrder/detail",
-                                    "params" => ["id" => $order->id]
-                                ];
                                 $push = new push('ghs', push::MSG_TYPE_ORDER);
-                                $push->pushByCloud($cids, $title, $content, $payload);
+                                $push->ghsOrderMessage($shop, $order);
                             }
                             //花店采购供货商端APP收到通知
                             //NoticeClass::ghsNewOrderNotice($order);

+ 1 - 9
app-hd/controllers/PurchaseController.php

@@ -770,16 +770,8 @@ class PurchaseController extends BaseController
                     Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
 
                     //向供货商APP发通知
-                    $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId'=>$shopId]);
-                    $cids = array_column($allDevices, 'clientId');
-                    $title = '你有新的订单';
-                    $content = $shop->shopName . ' ¥' . $order->actPrice;
-                    $payload = [
-                        "page" => "pagesOrder/detail",
-                        "params" => ["id" => $order->id]
-                    ];
                     $push = new push('ghs', push::MSG_TYPE_ORDER);
-                    $push->pushByCloud($cids, $title, $content, $payload);
+                    $push->ghsOrderMessage($shop, $order);
                 }
                 //订单生成时唤起在线打印
                 if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {

+ 7 - 1
common/base/classes/BaseClass.php

@@ -164,7 +164,13 @@ class BaseClass
     }
 
     /**
-     *根据条件查出多条记录
+     * 根据条件查出多条记录
+     * @param array $condition
+     * @param null $order
+     * @param string $field
+     * @param null $indexBy
+     * @param bool $returnObject
+     * @return array|\yii\db\ActiveRecord[]
      */
     public static function getAllByCondition($condition, $order = null, $field = '*', $indexBy = null, $returnObject = false)
     {

+ 50 - 9
common/components/push.php

@@ -1,6 +1,9 @@
 <?php
 namespace common\components;
 
+use yii\db\ActiveRecord;
+use Yii;
+
 class push
 {
     // 消息类型
@@ -245,15 +248,53 @@ class push
         ];
 
         $ch = curl_init($this->url);
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-        curl_setopt($ch, CURLOPT_POST, true);
-        curl_setopt($ch, CURLOPT_HTTPHEADER, [
-            'Content-Type: application/json'
-        ]);
-        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
-        $res = curl_exec($ch);
-        curl_close($ch);
 
-        return json_decode($res, true);
+        if ($ch === false) {
+            throw new \Exception('Failed to initialize curl');
+        }
+
+        try {
+            curl_setopt_array($ch, [
+                CURLOPT_RETURNTRANSFER => true,
+                CURLOPT_POST => true,
+                CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
+                CURLOPT_POSTFIELDS => json_encode($data),
+                CURLOPT_TIMEOUT => 10,
+                CURLOPT_CONNECTTIMEOUT => 10,
+            ]);
+
+            $response = curl_exec($ch);
+
+            if ($response === false) {
+                throw new \Exception('CURL Error: ' . curl_error($ch));
+            }
+
+            return $response;
+        } finally {
+            curl_close($ch);
+        }
+    }
+
+    /**
+     * 向供货商 APP 发送新订单通知
+     * @param ActiveRecord $shop
+     * @param ActiveRecord $order
+     * @throws \Exception
+     */
+    public function ghsOrderMessage($shop, $order)
+    {
+        try {
+            $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId'=>$shop->shopId]);
+            $cids = array_column($allDevices, 'clientId');
+            $title = '你有新的订单';
+            $content = $shop->shopName . ' ¥' . $order->actPrice;
+            $payload = [
+                "page" => "pagesOrder/detail",
+                "params" => ["id" => $order->id]
+            ];
+            $this->pushByCloud($cids, $title, $content, $payload);
+        } catch (\Exception $e) {
+            Yii::error('向供货商 APP 发送新订单通知失败: ' . $e->getMessage());
+        }
     }
 }

+ 1 - 9
console/controllers/MqController.php

@@ -52,16 +52,8 @@ class MqController extends Controller
                         WxMessageClass::ghsHasNewOrderInform($shop, $order);
 
                         // 向供货商App发通知
-                        $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId'=>$shopId]);
-                        $cids = array_column($allDevices, 'clientId');
-                        $title = '你有新的订单';
-                        $content = $shop->shopName . ' ¥' . $order->actPrice;
-                        $payload = [
-                            "page" => "pagesOrder/detail",
-                            "params" => ["id" => $order->id]
-                        ];
                         $push = new push('ghs', push::MSG_TYPE_ORDER);
-                        $push->pushByCloud($cids, $title, $content, $payload);
+                        $push->ghsOrderMessage($shop, $order);
                     }
                 }
             }