shish 3 anni fa
parent
commit
6da35866e8
1 ha cambiato i file con 99 aggiunte e 2 eliminazioni
  1. 99 2
      app-hd/controllers/MtController.php

+ 99 - 2
app-hd/controllers/MtController.php

@@ -195,11 +195,108 @@ class MtController extends PublicController
         return Json::encode(['data' => 'ok']);
     }
 
+    //修改商品 ssh 20220914
     public function actionGoodsUpdate()
     {
         $post = Yii::$app->request->post();
-        Yii::info('confirmOrderData: ' . json_encode($post));
-        return Json::encode(['data' => 'ok']);
+
+        $connection = Yii::$app->db;//事务处理
+        $transaction = $connection->beginTransaction();
+        try {
+            if (isset($post['retail_data']) == false) {
+                noticeUtil::push("美团修改商品,通知我们时出错了,没有获取到相应信息", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+            $retailData = $post['retail_data'];
+            $retail = urldecode(urldecode($retailData));
+            $info = json_decode($retail);
+            $obj = $info[0] ?? null;
+            $shopId = $obj->app_poi_code ?? 0;
+            $shop = ShopClass::getById($shopId, true);
+            if (empty($shop)) {
+                noticeUtil::push("美团修改商品,通知我们时出错了,没有找到门店", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+            $sjId = $shop->sjId ?? 0;
+            $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
+            if (empty($ext)) {
+                noticeUtil::push("美团修改商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+            $mainId = $shop->mainId ?? 0;
+            if (empty($mainId)) {
+                noticeUtil::push("美团修改商品,通知我们时出错了,没有main信息", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+            $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
+            if (empty($mtCategory)) {
+                $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
+            }
+            $catId = $mtCategory->id ?? 0;
+            if (empty($catId)) {
+                noticeUtil::push("美团修改商品,通知我们时出错了,没有美团的分类id", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+
+            $config = dict::getDict('mtConfig');
+            $app = new Application($config);
+            $accessToken = mtUtil::getAccessToken($ext);
+
+            $appSpuCode = $obj->app_spu_code ?? '';
+            $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
+            if (empty($hasGoods)) {
+                noticeUtil::push("美团修改商品,通知我们时,没有发现这个商品,mainId:{$mainId} spu:{$appSpuCode}", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+
+            $params = ['app_poi_code' => $shopId, 'app_spu_code' => $appSpuCode, 'access_token' => $accessToken];
+            $goodsInfo = $app->retail->getDetail($params);
+
+            Yii::info('获取商品详情:' . $goodsInfo . ' ' . json_encode($params));
+
+            $goodsInfo = json_decode($goodsInfo, true);
+            if (isset($goodsInfo['error'])) {
+                noticeUtil::push("美团修改商品,通知我们时出错了,没有找到商品图片", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+            $imgString = $goodsInfo['data']['picture'] ?? '';
+            if (empty($imgString)) {
+                noticeUtil::push("美团修改商品,通知我们时出错了,没有商品图片...", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+
+            noticeUtil::push("美团修改商品,通知我们,商品图片:" . $imgString, '15280215347');
+
+            $goodsName = $goodsInfo['data']['name'] ?? '';
+            $goodsPrice = $goodsInfo['data']['price'] ?? '';
+
+            $shopImg = [];
+            $cover = '';
+            $imgArr = explode(',', $imgString);
+            if (!empty($imgArr)) {
+                foreach ($imgArr as $imgUrl) {
+                    $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
+                    $shopImg[] = $shortUrl;
+                    if ($cover == '') {
+                        $cover = $shortUrl;
+                    }
+                }
+            }
+            $hasGoods->price = $goodsPrice;
+            $hasGoods->name = $goodsName;
+            $hasGoods->cover = $cover;
+            $hasGoods->shopImg = $shopImg;
+            $hasGoods->save();
+
+            $transaction->commit();
+            return Json::encode(['data' => 'ok']);
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            $msg = $e->getMessage();
+            noticeUtil::push("美团修改商品,通知我们时出错了,错误信息:{$msg}", '15280215347');
+            return Json::encode(['data' => 'ok']);
+        }
+
     }
 
     //已确认订单通知