Просмотр исходного кода

Merge branch 'zhongqi-pushMessage'

shish 9 месяцев назад
Родитель
Сommit
8d331c2dd8

+ 31 - 1
app-ghs/controllers/AdminController.php

@@ -14,6 +14,7 @@ use common\components\util;
 use Yii;
 use yii\helpers\Json;
 use bizHd\user\classes\UserClass;
+use bizGhs\device\classes\GhsDeviceClass;
 
 class AdminController extends BaseController
 {
@@ -98,12 +99,41 @@ class AdminController extends BaseController
     {
         $get = Yii::$app->request->get();
         $id = $get['id'] ?? '';
-        noticeUtil::push("客户端ID: " . $id);
+        $autoLogin = $get['auto'] ?? 0; // 是否自动登录的请求
+        noticeUtil::push("批发客户端ID: " . $id);
         if (empty($id)) {
             util::success(['returnStatus' => 'FAILURE']);
         }
         $this->admin->clientId = $id;
         $this->admin->save();
+
+        $ghsDevice = GhsDeviceClass::getByCondition(['clientId'=>$id], true);
+        if ($ghsDevice == null){
+            $device = [
+                'clientId' => $id,
+                'mainId' => $this->mainId,
+                'shopId' => $this->shopId,
+                'adminId' => $this->adminId,
+                'status' => 1
+            ];
+            if ($autoLogin == 1) {
+                $device['autoLoginTime'] = date('Y-m-d H:i:s');
+            } else {
+                $device['pwdLoginTime'] = date('Y-m-d H:i:s');
+            }
+            GhsDeviceClass::add($device);
+        } else {
+            if ($autoLogin == 1) {
+                $ghsDevice->autoLoginTime = date('Y-m-d H:i:s');
+            } else {
+                $ghsDevice->pwdLoginTime = date('Y-m-d H:i:s');
+            }
+            $ghsDevice->mainId = $this->mainId;
+            $ghsDevice->shopId = $this->shopId;
+            $ghsDevice->adminId = $this->adminId;
+            $ghsDevice->save();
+        }
+
         util::complete();
     }
 

+ 25 - 4
app-ghs/controllers/OrderController.php

@@ -34,6 +34,7 @@ use common\components\util;
 use biz\shop\classes\ShopAdminClass;
 use biz\shop\classes\ShopClass;
 use common\components\lakala\Lakala;
+use common\components\push;
 
 class OrderController extends BaseController
 {
@@ -1337,10 +1338,25 @@ class OrderController extends BaseController
                         Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
 
                         //通知花店
-//                        $noticeText = json_encode(['id' => $order->purchaseId]);
-//                        $noticeKey = "ghsKdNoticeHd";
-//                        Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
-
+                        //$noticeText = json_encode(['id' => $order->purchaseId]);
+                        //$noticeKey = "ghsKdNoticeHd";
+                        //Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
+
+                        Yii::info('向花店App发通知');
+                        //$custom = CustomClass::getById($order->customId);
+                        //$customShop = ShopClass::getById($custom['shopId'], true, 'merchantName, shopName');
+                        //向花店App发通知
+                        $allDevices = \bizHd\device\classes\HdDeviceClass::getAllByCondition(['shopId'=>$custom['shopId']]);
+                        $cids = array_column($allDevices, 'clientId');
+                        $title = '买花成功';
+                        $shopName = $shop->merchantName . ($shop->shopName != '首店' ? '(' . $shop->shopName . ')' : '');
+                        $content = $shopName . ' ¥' . $order->actPrice;
+                        $payload = [
+                            "page" => "pagesPurchase/purDetails",
+                            "params" => ["id" => $order->purchaseId]
+                        ];
+                        $push = new push('hd', push::MSG_TYPE_ORDER);
+                        $push->pushByCloud($cids, $title, $content, $payload);
                     }
                 }
                 //订单生成时唤起在线打印
@@ -1644,6 +1660,11 @@ class OrderController extends BaseController
             if ($order->status != 1 && $order->status != 5) {
                 //花店采购供货商端APP收到通知
                 //NoticeClass::ghsNewOrderNotice($order);
+
+                //向供货商APP发通知
+                $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
+                $push = new push('ghs', push::MSG_TYPE_ORDER);
+                $push->ghsOrderMessage($shop, $cgShop, $order);
             }
         }
 

+ 14 - 3
app-ghs/controllers/TestController.php

@@ -1384,9 +1384,19 @@ class TestController extends BaseController
 
     public function actionPush()
     {
-        $push = new \common\components\push('ghs');
-
-        $cids = ['2539a06dde18170d61f235dd40253d36', '3a424d2fcce148fbe38f2dea569e8228', '08fd2c3c72a5da46d2274aaa05c321e3', '9a5f7a63e290bf28f35d0691484e0721'];
+        $push = new \common\components\push('ghs', \common\components\push::MSG_TYPE_ORDER);
+
+        $cids = [
+            '5badab3ce8d531aa00f0ff5ddf6f37d8', // OPPO
+            '335cea1f91394178d1b302f2e240c0ad', // 小米 - 线上
+            '08fd2c3c72a5da46d2274aaa05c321e3', //
+            '9a5f7a63e290bf28f35d0691484e0721', // 华为
+            '2278477ef2fe50fed89bc27dd4b9bde2', // VIVO
+            'e980d01fb4699ed23d79a8c1136d4322', // VIVO 线上
+            '6012fd931b5aa8cc028bec036bca9bc2', // 荣耀
+            'd3ff7eed43d51bf6e0c54091eb9d48c0', // Apple XS - 我的(线下)
+            '4a8e4da3b0161f1a8ee352f293236254', // Apple XS Max - 测试机(线上)
+        ];
         $title = '新订单';
         $content = '您有新的订单,请查看 order_id: ' . random_int(100000, 999999);
         $payload = [
@@ -1396,6 +1406,7 @@ class TestController extends BaseController
         $rs = $push->pushByCloud($cids, $title, $content, $payload);
         Yii::info(json_encode($rs));
 
+        // clientId 后接状态:successed_online / successed_offline
         echo json_encode($rs);
     }
 }

+ 34 - 8
app-hd/controllers/AdminController.php

@@ -8,15 +8,14 @@ use bizHd\admin\classes\AdminClass;
 use bizHd\admin\classes\ShopAdminClass;
 use bizHd\admin\services\AdminService;
 use bizHd\staff\classes\StaffClass;
-use bizHd\staff\services\StaffService;
 use bizHd\wx\services\WxOpenService;
-use common\components\dict;
 use common\components\imgUtil;
 use common\components\noticeUtil;
 use common\components\util;
 use Yii;
 use yii\helpers\Json;
 use bizHd\user\classes\UserClass;
+use \bizHd\device\classes\HdDeviceClass;
 
 class AdminController extends BaseController
 {
@@ -433,17 +432,44 @@ class AdminController extends BaseController
         util::success($data);
     }
 
-    //修改保存安卓clientId
+    //修改或保存 clientId
     public function actionReplaceClientId()
     {
         $get = Yii::$app->request->get();
-        $id = $get['id'] ?? '';
-        if (empty($id)) {
+        $clientId = $get['id'] ?? '';
+        $autoLogin = $get['auto'] ?? 0; // 是否自动登录的请求
+        if (empty($clientId)) {
             util::success(['returnStatus' => 'FAILURE']);
         }
-        $this->admin->clientId = $id;
-        $this->admin->save();
-        //noticeUtil::push("保存客户端ID:" . $id, '15280215347');
+
+        $hdDevice = HdDeviceClass::getByCondition(['clientId'=>$clientId], true);
+        if ($hdDevice == null) {
+            $device = [
+                'clientId' => $clientId,
+                'mainId' => $this->mainId,
+                'shopId' => $this->shopId,
+                'adminId' => $this->adminId,
+                'status' => 1
+            ];
+            if ($autoLogin == 1) {
+                $device['autoLoginTime'] = date('Y-m-d H:i:s');
+            } else {
+                $device['pwdLoginTime'] = date('Y-m-d H:i:s');
+            }
+            HdDeviceClass::add($device);
+        } else {
+            if ($autoLogin == 1) {
+                $hdDevice->autoLoginTime = date('Y-m-d H:i:s');
+            } else {
+                $hdDevice->pwdLoginTime = date('Y-m-d H:i:s');
+            }
+            $hdDevice->mainId = $this->mainId;
+            $hdDevice->shopId = $this->shopId;
+            $hdDevice->adminId = $this->adminId;
+            $hdDevice->save();
+        }
+
+        noticeUtil::push("保存花店零售客户端ID:" . $clientId, '15280215347');
         util::complete();
     }
 

+ 22 - 1
app-hd/controllers/NoticeController.php

@@ -21,6 +21,7 @@ use biz\wx\classes\WxMessageClass;
 use bizGhs\order\classes\OrderClass;
 use biz\shop\classes\ShopExtClass;
 use common\components\lakala\Lakala;
+use common\components\push;
 
 class NoticeController extends PublicController
 {
@@ -232,11 +233,16 @@ class NoticeController extends PublicController
                         $shopId = $order->shopId ?? 0;
                         $shop = ShopClass::getById($shopId, true);
                         if (!empty($shop)) {
-                            //花店采购供货商端微信收到通知
+                            //花店采购供货商端微信收到通知
                             //WxMessageClass::ghsHasNewOrderInform($shop, $order);
                             $noticeText = json_encode(['orderId' => $saleId]);
                             $noticeKey = "hdCgNoticeGhs";
                             Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
+
+                            //向供货商APP发通知
+                            $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
+                            $push = new push('ghs', push::MSG_TYPE_ORDER);
+                            $push->ghsOrderMessage($shop, $cgShop, $order);
                         }
                         //花店采购供货商端APP收到通知
                         //NoticeClass::ghsNewOrderNotice($order);
@@ -464,6 +470,11 @@ class NoticeController extends PublicController
                             $noticeText = json_encode(['orderId' => $saleId]);
                             $noticeKey = "hdCgNoticeGhs";
                             Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
+
+                            //向供货商APP发通知
+                            $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
+                            $push = new push('ghs', push::MSG_TYPE_ORDER);
+                            $push->ghsOrderMessage($shop, $cgShop, $order);
                         }
                         //花店采购供货商端APP收到通知
                         //NoticeClass::ghsNewOrderNotice($order);
@@ -595,6 +606,11 @@ class NoticeController extends PublicController
                             $noticeText = json_encode(['orderId' => $saleId]);
                             $noticeKey = "hdCgNoticeGhs";
                             Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
+
+                            //向供货商端APP发通知
+                            $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
+                            $push = new push('ghs', push::MSG_TYPE_ORDER);
+                            $push->ghsOrderMessage($shop, $cgShop, $order);
                         }
                         //花店采购供货商端APP收到通知
                         //NoticeClass::ghsNewOrderNotice($order);
@@ -833,6 +849,11 @@ class NoticeController extends PublicController
                                 $noticeText = json_encode(['orderId' => $saleId]);
                                 $noticeKey = "hdCgNoticeGhs";
                                 Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
+
+                                //向供货商端APP发通知
+                                $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
+                                $push = new push('ghs', push::MSG_TYPE_ORDER);
+                                $push->ghsOrderMessage($shop, $cgShop, $order);
                             }
                             //花店采购供货商端APP收到通知
                             //NoticeClass::ghsNewOrderNotice($order);

+ 6 - 0
app-hd/controllers/PurchaseController.php

@@ -23,6 +23,7 @@ use common\components\util;
 use bizGhs\order\services\OrderService;
 use common\components\lakala\Lakala;
 use common\components\qrCodeUtil;
+use common\components\push;
 
 class PurchaseController extends BaseController
 {
@@ -767,6 +768,11 @@ class PurchaseController extends BaseController
                     $noticeText = json_encode(['orderId' => $saleId]);
                     $noticeKey = "hdCgNoticeGhs";
                     Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
+
+                    //向供货商APP发通知
+                    $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
+                    $push = new push('ghs', push::MSG_TYPE_ORDER);
+                    $push->ghsOrderMessage($shop, $cgShop, $order);
                 }
                 //订单生成时唤起在线打印
                 if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {

+ 25 - 0
app-hd/controllers/TestController.php

@@ -38,4 +38,29 @@ class TestController extends BaseController
 //            util::fail('操作失败');
 //        }
     }
+
+    public function actionPush()
+    {
+        $push = new \common\components\push('hd', \common\components\push::MSG_TYPE_ORDER);
+
+        $cids = [
+            '030e02ba8fe602f3c6222b1f360aa88f', // 华为
+            '4570ecb8970811d07629742c1612f3cd', // OPPO
+            'acec28309378a16c4ed10bbea6d40635', // 小米
+            'ee1763340e6e8778b9d6af259e65511f', // 荣耀 -- ee1763340e6e8778b9d6af259e65511f
+            'b768e3af3991d00f27d3ee728401b769', // Apple XS - 我的(线下)
+            'c9f4ccdf5b75da87abe1e9bcef6ef33f', // Apple XS Max - 测试机
+        ];
+        $title = '新订单';
+        $content = '您有新的订单,请查看 order_id: ' . random_int(100000, 999999);
+        $payload = [
+            "page" => "pagesClient/member/detail",
+            "params" => ["id" => 682]
+        ];
+        $rs = $push->pushByCloud($cids, $title, $content, $payload);
+        Yii::info(json_encode($rs));
+
+        // clientId 后接状态:successed_online / successed_offline
+        echo json_encode($rs);
+    }
 }

+ 9 - 0
biz-ghs/device/classes/GhsDeviceClass.php

@@ -0,0 +1,9 @@
+<?php
+namespace bizGhs\device\classes;
+
+use \bizGhs\base\classes\BaseClass;
+
+class GhsDeviceClass extends BaseClass
+{
+    public static $baseFile = '\bizGhs\device\models\GhsDevice';
+}

+ 12 - 0
biz-ghs/device/models/GhsDevice.php

@@ -0,0 +1,12 @@
+<?php
+namespace bizGhs\device\models;
+
+use bizGhs\base\models\Base;
+
+class GhsDevice extends Base
+{
+    public static function tableName()
+    {
+        return 'xhGhsDevice';
+    }
+}

+ 9 - 0
biz-hd/device/classes/HdDeviceClass.php

@@ -0,0 +1,9 @@
+<?php
+namespace bizHd\device\classes;
+
+use \bizHd\base\classes\BaseClass;
+
+class HdDeviceClass extends BaseClass
+{
+    public static $baseFile = '\bizHd\device\models\HdDevice';
+}

+ 12 - 0
biz-hd/device/models/HdDevice.php

@@ -0,0 +1,12 @@
+<?php
+namespace bizHd\device\models;
+
+use bizHd\base\models\Base;
+
+class HdDevice extends Base
+{
+    public static function tableName()
+    {
+        return 'xhHdDevice';
+    }
+}

+ 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)
     {

+ 272 - 28
common/components/push.php

@@ -1,37 +1,241 @@
 <?php
 namespace common\components;
 
+use yii\db\ActiveRecord;
+use Yii;
+
 class push
 {
-    private $ghsUrl = 'https://fc-mp-42d1a66f-2aec-4fc5-9d92-4d176bf3d337.next.bspapp.com/ghs/push';
-    private $hdUrl = 'https://fc-mp-027cce01-d39a-4ed2-8b09-1f4ff43ec945.next.bspapp.com/hd/push';
+    // 消息类型
+    const MSG_TYPE_ORDER = 'order';         // 订单消息
+    const MSG_TYPE_LOGISTICS = 'logistics'; // 物流消息
+    const MSG_TYPE_ACCOUNT = 'account';     // 账户消息
+    const MSG_TYPE_WORK = 'work';           // 工作事项提醒
+    const MSG_TYPE_SUBSCRIPTION = 'subscription'; // 订阅消息
+
+    const SECRET = 'RIVATE_KEY_S^@VJDy7'; // 👈 与云函数中保持一致
+    const GHS_URL = 'https://fc-mp-42d1a66f-2aec-4fc5-9d92-4d176bf3d337.next.bspapp.com/ghs/push';
+    const HD_URL = 'https://fc-mp-027cce01-d39a-4ed2-8b09-1f4ff43ec945.next.bspapp.com/hd/push';
+
+    // 私有属性
     private $url = '';
-    private $secret = 'RIVATE_KEY_S^@VJDy7'; // 👈 与云函数中保持一致
+    private $client = '';
+    private $msgType = '';
 
-    public function __construct($client) {
+    public function __construct($client, $msgType) {
+        $this->client = $client;
         if ($client == 'ghs') {
-            $this->url = $this->ghsUrl;
+            $this->url = self::GHS_URL;
         } else if ($client == 'hd') {
-            $this->url = $this->hdUrl;
+            $this->url = self::HD_URL;
         } else {
             throw new \Exception('client not found');
         }
+        $this->msgType = $msgType;
+    }
+
+    /**
+     * 实现部分厂商特定功能,包括仅部分厂商支持、不常用或厂商临时新增的功能(不依赖 uni-push,厂商文档支持的参数可直接使用)。
+     * 例如:推送渠道 ID、消息分类(部分厂商未配置时可能被限量推送或静默推送,即静音且需下拉系统通知栏才可见通知内容)、通知栏富文本
+     */
+    public function getOptions($client) {
+        if ($client == 'ghs') {
+            return $this->initGhsOptions();
+        }
+
+        return $this->initHdOptions();
+    }
+
+    /**
+     * 花店的厂商options
+     * @return array
+     */
+    public function initHdOptions() {
+        $optionsArr = [];
+        switch ($this->msgType) {
+            case self::MSG_TYPE_ORDER:
+                $optionsArr = [ "android" =>
+                    [
+                        "XM" => [
+                            "/extra.channel_id" => "142737" //小米后台申请的通知类别id -- https://admin.xmpush.xiaomi.com/zh_CN/channel/list?appId=2882303761520146676
+                        ],
+                        "HW" => [
+                            "/message/android/category" => "EXPRESS"
+                        ],
+                        "VV" => [
+                            "/category" => "ORDER"
+                        ],
+                    ]
+                ];
+                break;
+            case self::MSG_TYPE_LOGISTICS:
+                $optionsArr = [ "android" =>
+                    [
+                        "XM" => [
+                            "/extra.channel_id" => "142737"
+                        ],
+                        "HW" => [
+                            "/message/android/category" => "EXPRESS"
+                        ],
+                        "VV" => [
+                            "/category" => "ORDER"
+                        ],
+                    ]
+                ];
+                break;
+            case self::MSG_TYPE_ACCOUNT:
+                $optionsArr = [ "android" =>
+                    [
+                        "XM" => [
+                            "/extra.channel_id" => "143019"
+                        ],
+                        "HW" => [
+                            "/message/android/category" => "ACCOUNT"
+                        ],
+                        "VV" => [
+                            "/category" => "ACCOUNT"
+                        ],
+                    ]
+                ];
+                break;
+            case self::MSG_TYPE_WORK:
+                $optionsArr = [ "android" =>
+                    [
+                        "XM" => [
+                            "/extra.channel_id" => "143018"
+                        ],
+                        "HW" => [
+                            "/message/android/category" => "WORK"
+                        ],
+                        "VV" => [
+                            "/category" => "TODO"
+                        ],
+                    ]
+                ];
+                break;
+            case self::MSG_TYPE_SUBSCRIPTION:
+                $optionsArr = [ "android" =>
+                    [
+                        "XM" => [
+                            "/extra.channel_id" => "0" // TODO 暂无
+                        ],
+                        "HW" => [
+                            "/message/android/category" => "SUBSCRIPTION"
+                        ],
+                        "VV" => [
+                            "/category" => "SUBSCRIPTION"
+                        ],
+                    ]
+                ];
+                break;
+        }
+
+        // 【荣耀】
+        $optionsArr["android"]["HO"] = [
+            "/android/notification/importance" => "NORMAL"
+        ];
+
+
+        return $optionsArr;
+    }
+
+    /**
+     * 批发商的厂商options
+     * @return array
+     */
+    public function initGhsOptions() {
+        $optionsArr = [];
+        switch ($this->msgType) {
+            case self::MSG_TYPE_ORDER:
+                $optionsArr = [ "android" =>
+                    [
+                        "XM" => [
+                            "/extra.channel_id" => "142743" //小米后台申请的通知类别id -- https://admin.xmpush.xiaomi.com/zh_CN/channel/list?appId=2882303761520146676
+                        ],
+                        "HW" => [
+                            "/message/android/category" => "EXPRESS"
+                        ],
+                        "VV" => [
+                            "/category" => "ORDER"
+                        ],
+                    ]
+                ];
+                break;
+            case self::MSG_TYPE_LOGISTICS:
+                $optionsArr = [ "android" =>
+                    [
+                        "XM" => [
+                            "/extra.channel_id" => "142744"
+                        ],
+                        "HW" => [
+                            "/message/android/category" => "EXPRESS"
+                        ],
+                        "VV" => [
+                            "/category" => "ORDER"
+                        ],
+                    ]
+                ];
+                break;
+            case self::MSG_TYPE_ACCOUNT:
+                $optionsArr = [ "android" =>
+                    [
+                        "XM" => [
+                            "/extra.channel_id" => "142895"
+                        ],
+                        "HW" => [
+                            "/message/android/category" => "ACCOUNT"
+                        ],
+                        "VV" => [
+                            "/category" => "ACCOUNT"
+                        ],
+                    ]
+                ];
+                break;
+            case self::MSG_TYPE_WORK:
+                $optionsArr = [ "android" =>
+                    [
+                        "XM" => [
+                            "/extra.channel_id" => "142892"
+                        ],
+                        "HW" => [
+                            "/message/android/category" => "WORK"
+                        ],
+                        "VV" => [
+                            "/category" => "TODO"
+                        ],
+                    ]
+                ];
+                break;
+            case self::MSG_TYPE_SUBSCRIPTION:
+                $optionsArr = [ "android" =>
+                    [
+                        "XM" => [
+                            "/extra.channel_id" => "0" // TODO 暂无
+                        ],
+                        "HW" => [
+                            "/message/android/category" => "SUBSCRIPTION"
+                        ],
+                        "VV" => [
+                            "/category" => "SUBSCRIPTION"
+                        ],
+                    ]
+                ];
+                break;
+        }
+        return $optionsArr;
     }
 
     public function pushByCloud($cids, $title, $content, $payload = [])
     {
-        $sign = md5($title . $content . $this->secret);
-
-        $optionsArr = [ "android" =>
-            [
-    //            "XM" => [
-    //                "/extra.channel_id" => "" //小米后台申请的通知类别id
-    //            ],
-                "HW" => [
-                    "/message/android/category" => "EXPRESS"
-                ]
-            ]
-        ];
+        $sign = md5($title . $content . self::SECRET);
+
+        $optionsArr = $this->getOptions($this->client);
+        // 测试环境的特殊处理
+        if ( getenv('YII_ENV') == 'dev') {
+            $optionsArr["android"]["HW"]["/message/android/target_user_type"] = 1; //华为 -- 值为int 类型。1 表示华为测试消息,华为每个应用每日可发送该测试消息500条,此target_user_type 参数请勿发布至线上。
+            $optionsArr["android"]["VV"]["/pushMode"] =  1;                 //VIVO  --  值为int 类型。0 表示正式推送;1 表示测试推送,不填默认为0。线上请勿使用测试推送
+            $optionsArr["android"]["HO"]["/android/targetUserType"] = 1;    //荣耀  -- 值为int 类型。1 表示测试推送,不填默认为0。荣耀每个应用每日可发送该测试消息1000条。此测试参数请勿发布至线上。
+        }
 
         $data = [
             'cid' => is_array($cids) ? $cids : [$cids],
@@ -44,15 +248,55 @@ 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 $cgShop 采购花店门店
+     * @param ActiveRecord $order xhGhsOrder
+     * @throws \Exception
+     */
+    public function ghsOrderMessage($shop, $cgShop, $order)
+    {
+        try {
+            $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId'=>$shop->id]);
+            $cids = array_column($allDevices, 'clientId');
+            $title = '你有新的订单';
+            $shopName = $cgShop->merchantName . ($cgShop->shopName != '首店' ? '(' . $cgShop->shopName . ')' : '');
+            $content = $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());
+        }
     }
 }

+ 6 - 0
console/controllers/MqController.php

@@ -8,6 +8,7 @@ use bizHd\shop\classes\ShopClass;
 use biz\wx\classes\WxMessageClass;
 use yii\console\Controller;
 use bizGhs\order\classes\OrderClass;
+use \common\components\push;
 use Yii;
 
 class MqController extends Controller
@@ -49,6 +50,11 @@ class MqController extends Controller
                     $shop = ShopClass::getById($shopId, true);
                     if (!empty($shop)) {
                         WxMessageClass::ghsHasNewOrderInform($shop, $order);
+
+                        // 向供货商App发通知
+                        $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
+                        $push = new push('ghs', push::MSG_TYPE_ORDER);
+                        $push->ghsOrderMessage($shop, $cgShop, $order);
                     }
                 }
             }