Răsfoiți Sursa

商品信息

shish 4 ani în urmă
părinte
comite
1685eaa424

+ 8 - 66
app-hd/controllers/MtController.php

@@ -61,7 +61,6 @@ class MtController extends PublicController
                 noticeUtil::push("美团确认订单报错,没有找到门店拓展信息,美团订单id:{$mtOrderId}", '15280215347');
                 return Json::encode(['data' => 'ok']);
             }
-            $accessToken = mtUtil::getAccessToken($shopExt);
             $mainId = $shop->mainId ?? 0;
             $sjId = $shop->sjId ?? 0;
             $customId = $shop->defaultCustomId ?? 0;
@@ -93,8 +92,6 @@ class MtController extends PublicController
 
             Yii::info('商品信息:' . json_encode($mtGoodsData));
 
-            $config = dict::getDict('mtConfig');
-            $app = new Application($config);
             $productList = [];
 
             $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
@@ -120,79 +117,24 @@ class MtController extends PublicController
 
             $goodsNum = 0;
             foreach ($mtGoodsData as $mtKey => $mtGoods) {
-
                 $mtGoodsSpu = $mtGoods['app_spu_code'] ?? '';
                 if (empty($mtGoodsSpu)) {
                     if (isset($mtGoods['app_food_code']) && !empty($mtGoods['app_food_code'])) {
                         $mtGoodsSpu = $mtGoods['app_food_code'];
                     }
                 }
-                $params = ['app_poi_code' => $shopId, 'app_spu_code' => $mtGoodsSpu, 'access_token' => $accessToken];
-                $mgGoodsDetail = $app->retail->getDetail($params);
-                Yii::info('获取商品详情:' . $mgGoodsDetail . ' ' . json_encode($params));
-                $mgGoodsDetail = json_decode($mgGoodsDetail, true);
-                if (isset($mgGoodsDetail['error'])) {
-                    noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$mtOrderId}", '15280215347');
-                    util::stop();
-                }
-                $imgString = $mgGoodsDetail['data']['picture'] ?? '';
-                if (empty($imgString)) {
-                    noticeUtil::push("美团确认订单报错,没有商品图片,美团订单id:{$mtOrderId}", '15280215347');
-                    util::stop();
-                }
-                noticeUtil::push("美团确认订单,美团订单id:{$mtOrderId},商品图片:" . $imgString, '15280215347');
-                $goodsName = $mtGoods['food_name'] ?? '';
                 $goodsPrice = $mtGoods['price'] ?? 0;
                 $num = $mtGoods['quantity'] ?? 0;
                 $goodsNum = bcadd($num, $goodsNum);
-                $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
-                if (empty($hasGoods)) {
-                    $imgArr = explode(',', $imgString);
-                    $shopImg = [];
-                    foreach ($imgArr as $imgUrl) {
-                        $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
-                        $shopImg[] = $shortUrl;
-                        if ($orderCover == '') {
-                            $orderCover = $shortUrl;
-                        }
-                    }
-                    $cover = $shopImg[0];
-                    $goodsData = [
-                        'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
-                        'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => $cover, 'unitGoodsPrice' => $goodsPrice, 'num' => $num,
-                    ];
-                    $goods = GoodsClass::orderCreateGoods($goodsData);
-                    Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
-                } else {
-                    $id = $hasGoods->id ?? 0;
-                    $unitType = 0;
-                    $property = dict::getDict('property', 'goods');
-                    $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
-
-                    $imgMd5 = Yii::$app->redis->executeCommand('GET', ['mt_goods_spu_' . $mtGoodsSpu]);
-                    if (empty($imgMd5) || $imgMd5 != md5($imgString)) {
-                        //图片需要更新
-                        $imgArr = explode(',', $imgString);
-                        $shopImg = [];
-                        $currentCover = '';
-                        foreach ($imgArr as $imgUrl) {
-                            $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
-                            $shopImg[] = $shortUrl;
-                            if ($currentCover == '') {
-                                $currentCover = $shortUrl;
-                            }
-                        }
-                        $hasGoods->cover = $currentCover;
-                        $hasGoods->shopImg = json_encode($shopImg);
-                        Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
-                    }
-                    $hasGoods->name = $goodsName;
-                    $hasGoods->price = $goodsPrice;
-                    $hasGoods->save();
-                    if ($orderCover == '') {
-                        $orderCover = $hasGoods->cover ?? '';
-                    }
+                $currentGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
+                if (empty($currentGoods)) {
+                    noticeUtil::push("美团确认订单报错,数据库没有相应商品,美团订单id:{$mtOrderId}", '15280215347');
+                    return Json::encode(['data' => 'ok']);
                 }
+                $id = $currentGoods->id ?? 0;
+                $unitType = 0;
+                $property = dict::getDict('property', 'goods');
+                $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
                 $productList = array_merge($goods, $productList);
             }
             $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';

+ 1 - 19
app-pt/controllers/MtController.php

@@ -12,25 +12,7 @@ use Yii;
 class MtController extends BaseController
 {
 
-    public $guestAccess = ['bind', 'refresh','sync-goods'];
-
-    //同步商品 ssh 20220801
-    public function actionSyncGoods()
-    {
-        $shopId = Yii::$app->request->get('id');
-        $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
-        if (empty($ext)) {
-            util::fail('没有找到门店相信息');
-        }
-        $config = dict::getDict('mtConfig');
-        $app = new Application($config);
-        $accessToken = mtUtil::getAccessToken($ext);
-        $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken];
-        $json = $app->retail->retailList($params);
-        $array = json_decode($json, true);
-        echo '<pre>';
-        print_r($array);
-    }
+    public $guestAccess = ['bind', 'refresh'];
 
     public function actionBind()
     {

+ 85 - 0
console/controllers/MtController.php

@@ -2,12 +2,97 @@
 
 namespace console\controllers;
 
+use common\components\noticeUtil;
 use common\components\util;
 use Yii;
 use yii\console\Controller;
+use biz\shop\classes\ShopClass;
+use biz\shop\classes\ShopExtClass;
+use bizHd\goods\classes\CategoryClass;
+use bizHd\goods\classes\GoodsClass;
+use common\components\dict;
+use common\components\httpUtil;
+use common\components\mtUtil;
+use Shishaoqi\MeituanFlashPurchase\Application;
 
 
 class MtController extends Controller
 {
 
+    //同步商品 ssh 20220801
+    public function actionSyncGoods()
+    {
+        global $argv;
+
+        ini_set('memory_limit', '2045M');
+        set_time_limit(0);
+
+        $shopId = $argv[2];
+        $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
+        if (empty($ext)) {
+            util::fail('没有找到门店相信息');
+        }
+        $shop = ShopClass::getById($shopId, true);
+        if (empty($shop)) {
+            util::fail('没有找到门店');
+        }
+        $mainId = $shop->mainId ?? 0;
+        $sjId = $shop->sjId ?? 0;
+        $config = dict::getDict('mtConfig');
+        $app = new Application($config);
+        $accessToken = mtUtil::getAccessToken($ext);
+        $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken];
+        $json = $app->retail->retailList($params);
+        $arr = json_decode($json, true);
+        if (isset($arr['data']) == false || empty($arr['data'])) {
+            util::fail('没有商品');
+        }
+
+        $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)) {
+            util::fail('没有找到美团的商品分类');
+        }
+
+        $goodsList = $arr['data'];
+        foreach ($goodsList as $key => $val) {
+
+            $picture = $val['picture'] ?? '';
+            $price = $val['price'] ?? 0;
+            $name = $val['name'] ?? '';
+            $mtGoodsSpu = $val['app_spu_code'] ?? '';
+            $imgArr = explode(',', $picture);
+            if (empty($mtGoodsSpu)) {
+                noticeUtil::push('商家有商品没有spuId,' . $name, '15280215347');
+                continue;
+            }
+            $current = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $mtGoodsSpu], true);
+            if (!empty($current)) {
+
+            } else {
+                $shopImg = [];
+                foreach ($imgArr as $imgUrl) {
+                    $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
+                    $shopImg[] = $shortUrl;
+                }
+                $cover = $shopImg[0];
+                $goodsData = [
+                    'spu' => $mtGoodsSpu,
+                    'shopImg' => $shopImg,
+                    'name' => $name,
+                    'sjId' => $sjId,
+                    'mainId' => $mainId,
+                    'shopId' => $shopId,
+                    'catId' => $catId,
+                    'cover' => $cover,
+                    'unitGoodsPrice' => $price,
+                ];
+                GoodsClass::orderCreateGoods($goodsData);
+            }
+        }
+    }
+
 }