shish 4 éve
szülő
commit
f2e1505439

+ 139 - 11
app-hd/controllers/MtController.php

@@ -2,8 +2,17 @@
 
 namespace hd\controllers;
 
+use bizHd\custom\classes\CustomClass;
+use bizHd\goods\classes\CategoryClass;
+use bizHd\goods\classes\GoodsClass;
 use bizHd\meituan\classes\MtOrderClass;
+use bizHd\order\services\OrderService;
+use bizHd\shop\classes\ShopClass;
 use common\components\arrayUtil;
+use common\components\dict;
+use common\components\httpUtil;
+use common\components\noticeUtil;
+use common\components\util;
 use Yii;
 use Shishaoqi\MeituanFlashPurchase\Application;
 use yii\helpers\Json;
@@ -48,13 +57,132 @@ class MtController extends PublicController
         if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
             return Json::encode(['data' => 'ok']);
         }
-        foreach ($post as $k => $val) {
-            $post[$k] = urldecode($val);
-        }
 
-        MtOrderClass::add($post);
+        Yii::info('newOrder: ' . $post);
+        exit();
 
-        return Json::encode(['data' => 'ok']);
+        $connection = Yii::$app->db;//事务处理
+        $transaction = $connection->beginTransaction();
+        $orderId = $post['order_id'] ?? 0;
+        try {
+
+            $shopId = $post['app_poi_code'] ?? '';
+            $shop = ShopClass::getById($shopId, true);
+            if (empty($shop)) {
+                noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$orderId}", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+            $mainId = $shop->mainId ?? 0;
+            $sjId = $shop->sjId ?? 0;
+            $customId = $shop->defaultCustomId ?? 0;
+            if (empty($customId)) {
+                noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$orderId}", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+            $custom = CustomClass::getById($customId);
+            if (empty($custom)) {
+                noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$orderId}", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+
+            foreach ($post as $k => $val) {
+                $post[$k] = urldecode($val);
+            }
+            MtOrderClass::add($post);
+
+            $detail = $post['detail'] ?? '';
+            if (empty($detail)) {
+                noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$orderId}", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+            $mtGoodsData = json_decode($detail, true);
+            if (empty($mtGoodsData)) {
+                noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$orderId}", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+            $app = new Application($this->config);
+            $productList = [];
+
+            $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:{$orderId}", '15280215347');
+                return Json::encode(['data' => 'ok']);
+            }
+
+            foreach ($mtGoodsData as $mtKey => $mtGoods) {
+
+                $mtGoodsSpu = $mtGoods['app_spu_code'];
+                $params = ['app_poi_code' => $shopId, 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
+                $mgGoodsDetail = $app->retail->getDetail($params);
+                $mgGoodsDetail = json_decode($mgGoodsDetail, true);
+                if (isset($mgGoodsDetail['error'])) {
+                    noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$orderId}", '15280215347');
+                    util::stop();
+                }
+                $imgString = $mgGoodsDetail['data']['picture'];
+                $goodsName = $mtGoods['food_name'] ?? '';
+                $goodsPrice = $mtGoods['price'] ?? 0;
+                $num = $mtGoods['quantity'] ?? 0;
+
+                $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;
+                    }
+                    $goodsData = [
+                        'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
+                        'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => 0, '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 = [];
+                        foreach ($imgArr as $imgUrl) {
+                            $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
+                            $shopImg[] = $shortUrl;
+                        }
+                        $hasGoods->shopImg = $shopImg;
+                    }
+                    $hasGoods->name = $goodsName;
+                    $hasGoods->price = $goodsPrice;
+                    $hasGoods->save();
+                }
+                $productList = array_merge($goods, $productList);
+            }
+            $sendCost = $data['shipping_fee'] ?? 0;
+            $modifyPrice = $data[''];
+            $orderData = [
+                'product' => $productList,
+                'fromType' => dict::getDict('fromType', 'mt'),
+            ];
+
+            $hasPay = dict::getDict('hasPay', 'payed');
+            OrderService::createFinishOrder($orderData, $custom, $hasPay);
+            $transaction->commit();
+            return Json::encode(['data' => 'ok']);
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            $msg = $e->getMessage();
+            noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$orderId}", '15280215347');
+        }
     }
 
     public function actionNewOrder()
@@ -90,16 +218,16 @@ class MtController extends PublicController
         $detail = $data['detail'];
         $items = json_decode($detail, true);
         foreach ($items as $item) {
-            $appSpuCode = $item['app_spu_code'];
+            $mtGoodsSpu = $item['app_spu_code'];
 
             // access_token 是商家授权后,获取到的
-            $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $appSpuCode, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
-            $retailDetail = $app->retail->getDetail($params);
-            $retailDetail = json_decode($retailDetail, true);
-            if (isset($retailDetail['error'])) {
+            $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
+            $mgGoodsDetail = $app->retail->getDetail($params);
+            $mgGoodsDetail = json_decode($mgGoodsDetail, true);
+            if (isset($mgGoodsDetail['error'])) {
                 Yii::error('get retail fail:' . json_encode($params));
             } else {
-                $picArr[$appSpuCode] = $retailDetail['data']['picture'];
+                $picArr[$mtGoodsSpu] = $mgGoodsDetail['data']['picture'];
             }
         }
 

+ 1 - 1
app-hd/controllers/OrderController.php

@@ -396,7 +396,7 @@ class OrderController extends BaseController
                 if (is_numeric($unitGoodsPrice) == false) {
                     util::fail('请输入正确的单价');
                 }
-                $productList = GoodsClass::kdWorkGoods($post);
+                $productList = GoodsClass::orderCreateGoods($post);
             }
         }
 

+ 4 - 1
biz-hd/goods/classes/GoodsClass.php

@@ -26,7 +26,7 @@ class GoodsClass extends BaseClass
 
     const LOCK_STOCK_GOODS = 'lock_stock_goods_';//修改库存锁
 
-    public static function kdWorkGoods($data)
+    public static function orderCreateGoods($data)
     {
         $name = $data['name'] ?? '';
         $salePrice = 0;
@@ -42,6 +42,9 @@ class GoodsClass extends BaseClass
         $catIds = [$catId];
         $cover = isset($data['cover']) && !empty($data['cover']) ? $data['cover'] : 'default-img.png';
         $shopImg = [$cover];
+        if (isset($data['shopImg']) && is_array($data['shopImg'])) {
+            $shopImg = $data['shopImg'];
+        }
         $addGoodsData = [
             'name' => $name,
             'flower' => 1,

+ 158 - 125
common/components/httpUtil.php

@@ -12,129 +12,162 @@ use Yii;
 
 class httpUtil
 {
-	//专用方法,请勿修改 ssh 2019.11.21
-	public static function getHttpHost()
-	{
-		$http = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] . '://' : 'https://';
-		$suffix = 'huahb.com';
-		if (isset($_SERVER['HTTP_HOST'])) {
-			$hostInfo = $_SERVER['HTTP_HOST'];
-			$parse = parse_url($hostInfo);
-			$host = $parse['path'];
-			$pos = strpos($host, '.') + 1;
-			$suffix = substr($host, $pos);
-		}
-		return ['http' => $http, 'suffix' => $suffix];
-	}
-	
-	//获得类似这样 http://m.hhb.com
-	public static function getHost()
-	{
-		return Yii::$app->request->getHostInfo();
-	}
-	
-	//获取header里的数据 ssh 2019.11.22
-	public static function getHeader()
-	{
-		$ignore = ['host', 'accept', 'content-length', 'content-type'];
-		$headers = [];
-		foreach ($_SERVER as $key => $value) {
-			if (substr($key, 0, 5) === 'HTTP_') {
-				$key = substr($key, 5);
-				$key = str_replace('_', ' ', $key);
-				$key = str_replace(' ', '-', $key);
-				$key = strtolower($key);
-				if (!in_array($key, $ignore)) {
-					$headers[$key] = $value;
-				}
-			}
-		}
-		//$token = isset($headers['token']) ? $headers['token'] : '';
-		return $headers;
-	}
-	
-	public static function getToken()
-	{
-		$header = self::getHeader();
-		$token = isset($header['token']) ? $header['token'] : '';
-		return $token;
-	}
-	
-	//获取地址里的account ssh 2019.11.24
-	public static function getAccount($url)
-	{
-		//前端链接,需要去掉链接#号前面部分进行解析
-		$pos = strpos($url, '#');
-		if (isset($pos)) {
-			$url = substr($url, $pos + 1);
-		}
-		$parse = parse_url($url);
-		$account = 0;
-		if (isset($parse['query'])) {
-			parse_str($parse['query'], $queryArray);
-			if (isset($queryArray['account']) && !empty($queryArray['account'])) {
-				$account = $queryArray['account'];
-			}
-		}
-		return $account;
-	}
-	
-	//获取平台id ssh 2019.11.28
-	public static function getSourceId()
-	{
-		$ptSource = Yii::$app->params['ptSource'];
-		$host = self::getHost();
-		if (empty($host)) {
-			return 0;
-		}
-		$arr = parse_url($host);
-		if (isset($arr['host']) == false || empty($arr['host'])) {
-			return 0;
-		}
-		$pos = strpos($arr['host'], '.');
-		$ptName = substr($arr['host'], 0, $pos);
-		return isset($ptSource[$ptName]) ? $ptSource[$ptName] : 0;
-	}
-	
-	//判断是否微信浏览器
-	public static function isWx()
-	{
-		if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true) {
-			return true;
-		}
-		return false;
-	}
-	
-	//判断小程序 ssh 2019.12.13
-	public static function isMiniProgram()
-	{
-		//根据头信息判断
-		$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
-		if (strpos($userAgent, 'miniprogram') !== false) {
-			return true;
-		}
-		//根据来源判断
-		$referrer = Yii::$app->request->referrer;
-		if (strpos($referrer, 'servicewechat.com') !== false) {
-			return true;
-		}
-		return false;
-	}
-	
-	//获取ip ssh 2019.12.18
-	public static function ip()
-	{
-		return Yii::$app->request->userIP;
-	}
-	
-	//变成https ssh 2020.2.20
-	public static function becomeHttps($url)
-	{
-		$has = strpos($url, 'https');
-		if ($has !== false) {
-			return $url;
-		}
-		return str_replace('http', 'https', $url);
-	}
-	
+
+    /**
+     * 下载远程图片到本地 ssh 20220712
+     */
+    public static function downloadRemoteImg($sjId, $shopId, $url)
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_HEADER, 0);
+        curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        $body = curl_exec($ch);
+        curl_close($ch);
+
+        $month = date("Ym");
+        $date = date("d");
+        $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$shopId}/{$month}/{$date}/";
+        if (!file_exists($path)) {
+            //检查是否有该文件夹,如果没有就创建,并给予最高权限
+            mkdir($path, 0700, true);
+        }
+        $preFileName = stringUtil::uniqueFileName();
+        $newFile = $preFileName . ".jpg";
+        $fullPathFile = $path . $newFile;
+        $fp2 = @fopen($fullPathFile, 'a');
+        fwrite($fp2, $body);
+        fclose($fp2);
+        $img = "uploads/{$sjId}/{$shopId}/{$month}/{$date}/" . $newFile;
+        oss::uploadImage($img, $fullPathFile);
+        return $img;
+    }
+
+
+    //专用方法,请勿修改 ssh 2019.11.21
+    public static function getHttpHost()
+    {
+        $http = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] . '://' : 'https://';
+        $suffix = 'huahb.com';
+        if (isset($_SERVER['HTTP_HOST'])) {
+            $hostInfo = $_SERVER['HTTP_HOST'];
+            $parse = parse_url($hostInfo);
+            $host = $parse['path'];
+            $pos = strpos($host, '.') + 1;
+            $suffix = substr($host, $pos);
+        }
+        return ['http' => $http, 'suffix' => $suffix];
+    }
+
+    //获得类似这样 http://m.hhb.com
+    public static function getHost()
+    {
+        return Yii::$app->request->getHostInfo();
+    }
+
+    //获取header里的数据 ssh 2019.11.22
+    public static function getHeader()
+    {
+        $ignore = ['host', 'accept', 'content-length', 'content-type'];
+        $headers = [];
+        foreach ($_SERVER as $key => $value) {
+            if (substr($key, 0, 5) === 'HTTP_') {
+                $key = substr($key, 5);
+                $key = str_replace('_', ' ', $key);
+                $key = str_replace(' ', '-', $key);
+                $key = strtolower($key);
+                if (!in_array($key, $ignore)) {
+                    $headers[$key] = $value;
+                }
+            }
+        }
+        //$token = isset($headers['token']) ? $headers['token'] : '';
+        return $headers;
+    }
+
+    public static function getToken()
+    {
+        $header = self::getHeader();
+        $token = isset($header['token']) ? $header['token'] : '';
+        return $token;
+    }
+
+    //获取地址里的account ssh 2019.11.24
+    public static function getAccount($url)
+    {
+        //前端链接,需要去掉链接#号前面部分进行解析
+        $pos = strpos($url, '#');
+        if (isset($pos)) {
+            $url = substr($url, $pos + 1);
+        }
+        $parse = parse_url($url);
+        $account = 0;
+        if (isset($parse['query'])) {
+            parse_str($parse['query'], $queryArray);
+            if (isset($queryArray['account']) && !empty($queryArray['account'])) {
+                $account = $queryArray['account'];
+            }
+        }
+        return $account;
+    }
+
+    //获取平台id ssh 2019.11.28
+    public static function getSourceId()
+    {
+        $ptSource = Yii::$app->params['ptSource'];
+        $host = self::getHost();
+        if (empty($host)) {
+            return 0;
+        }
+        $arr = parse_url($host);
+        if (isset($arr['host']) == false || empty($arr['host'])) {
+            return 0;
+        }
+        $pos = strpos($arr['host'], '.');
+        $ptName = substr($arr['host'], 0, $pos);
+        return isset($ptSource[$ptName]) ? $ptSource[$ptName] : 0;
+    }
+
+    //判断是否微信浏览器
+    public static function isWx()
+    {
+        if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true) {
+            return true;
+        }
+        return false;
+    }
+
+    //判断小程序 ssh 2019.12.13
+    public static function isMiniProgram()
+    {
+        //根据头信息判断
+        $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
+        if (strpos($userAgent, 'miniprogram') !== false) {
+            return true;
+        }
+        //根据来源判断
+        $referrer = Yii::$app->request->referrer;
+        if (strpos($referrer, 'servicewechat.com') !== false) {
+            return true;
+        }
+        return false;
+    }
+
+    //获取ip ssh 2019.12.18
+    public static function ip()
+    {
+        return Yii::$app->request->userIP;
+    }
+
+    //变成https ssh 2020.2.20
+    public static function becomeHttps($url)
+    {
+        $has = strpos($url, 'https');
+        if ($has !== false) {
+            return $url;
+        }
+        return str_replace('http', 'https', $url);
+    }
+
 }

+ 13 - 0
console/controllers/MtController.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace console\controllers;
+
+use common\components\util;
+use Yii;
+use yii\console\Controller;
+
+
+class MtController extends Controller
+{
+
+}