request->post(); //$str = '{"sig":"3595534244c625331cb9e8b35cc63b13","app_id":"119094","retail_data":"%5B%257B%2522app_food_code%2522%253A%2522mtcode_1568954379552215085%2522%252C%2522app_id%2522%253A119094%252C%2522app_poi_code%2522%253A%252236524%2522%252C%2522app_spu_code%2522%253A%2522mtcode_1568954379552215085%2522%252C%2522categoryList%2522%253A%255B%257B%2522categoryCode%2522%253A%2522%2522%252C%2522categoryName%2522%253A%2522%25E6%259C%25AA%25E5%2588%2586%25E7%25B1%25BB%2522%257D%255D%252C%2522categoryName%2522%253A%2522%25E6%259C%25AA%25E5%2588%2586%25E7%25B1%25BB%2522%252C%2522createAppKey%2522%253A%2522%25E5%2595%2586%25E5%25AE%25B6%25E7%25AB%25AF_%25E7%25A7%25BB%25E5%258A%25A8%25E7%25AB%25AF%2522%252C%2522ctime%2522%253A1662902846%252C%2522name%2522%253A%25229%25E6%259C%25B5%25E7%25BA%25A2%25E7%258E%25AB%25E7%2591%25B0%25E6%2590%25AD%25E9%2585%258D%25E6%25BB%25A1%25E5%25A4%25A9%25E6%2598%259F%25E9%25B2%259C%25E8%258A%25B1%25E8%258A%25B1%25E6%259D%259F%2522%252C%2522opName%2522%253A%2522%25E5%2595%2586%25E5%25AE%25B6%25E8%25B4%25A6%25E5%258F%25B7112823666%2522%252C%2522timestamp%2522%253A1662902848%252C%2522utime%2522%253A1662902846%257D%5D","timestamp":"1662902848"}'; //$post = json_decode($str, true); $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; } } } $catIds = [$catId]; $addData = [ 'name' => $goodsName, 'flower' => 1, 'property' => 0, 'price' => $goodsPrice, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'status' => 0, 'delStatus' => 0, 'shopImg' => $shopImg, 'catIds' => $catIds, 'cover' => $cover, 'spu' => $appSpuCode, ]; GoodsClass::addGoods($addData); $transaction->commit(); return Json::encode(['data' => 'ok']); } catch (\Exception $e) { $transaction->rollBack(); $msg = $e->getMessage(); noticeUtil::push("美团添加商品,通知我们时出错了,错误信息:{$msg}", '15280215347'); return Json::encode(['data' => 'ok']); } } //删除商品 ssh 20220914 public function actionGoodsDel() { $post = Yii::$app->request->post(); 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']); } $appSpuCode = $obj->app_spu_code ?? ''; $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true); if (empty($hasGoods)) { return Json::encode(['data' => 'ok']); } $hasGoods->delStatus = 1; $hasGoods->save(); $goodsId = $hasGoods->id ?? 0; GoodsCategoryClass::updateByCondition(['gId' => $goodsId], ['delStatus' => 1]); return Json::encode(['data' => 'ok']); } //修改商品 ssh 20220914 public function actionGoodsUpdate() { $post = Yii::$app->request->post(); $post['retail_data'] = '%5B%257B%2522app_food_code%2522%253A%2522A004%2522%252C%2522app_id%2522%253A119094%252C%2522app_poi_code%2522%253A%252236524%2522%252C%2522app_spu_code%2522%253A%2522A004%2522%252C%2522categoryName%2522%253A%2522%25E6%25B5%258B%25E8%25AF%2595%25E5%2588%2586%25E7%25B1%25BB%2522%252C%2522ctime%2522%253A1661325122%252C%2522diffContents%2522%253A%257B%257D%252C%2522name%2522%253A%2522A004%2522%252C%2522opAppKey%2522%253A%2522%25E5%2595%2586%25E5%25AE%25B6%25E7%25AB%25AF_%25E7%25A7%25BB%25E5%258A%25A8%25E7%25AB%25AF%2522%252C%2522opName%2522%253A%2522%25E5%2595%2586%25E5%25AE%25B6%25E8%25B4%25A6%25E5%258F%25B7112823854%2522%252C%2522opReason%2522%253A%2522%2522%252C%2522timestamp%2522%253A1663751862%252C%2522utime%2522%253A1663751860%257D%5D'; $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); print_r($imgArr); die; 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 = json_encode($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']); } } //已确认订单通知 public function actionConfirmOrder() { //error_reporting(E_ALL); $post = Yii::$app->request->post(); $mtOrderId = $post['order_id'] ?? 0; $thirdSn = $post['day_seq'] ?? 0; //4秒内不允许第三方重复通知 $cacheKey = 'mt_confirm_order_' . $mtOrderId; $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]); if (!empty($has)) { Yii::info("美团已确认订单通知,出现重复通知,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}"); return Json::encode(['data' => 'ok']); } Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']); // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回 if (empty($post)) { return Json::encode(['data' => 'ok']); } Yii::info('confirmOrderData: ' . json_encode($post)); $connection = Yii::$app->db;//事务处理 $transaction = $connection->beginTransaction(); try { $shopId = $post['app_poi_code'] ?? ''; $shop = ShopClass::getById($shopId, true); if (empty($shop)) { noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true); if (empty($shopExt)) { noticeUtil::push("美团确认订单报错,没有找到门店拓展信息,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } $mainId = $shop->mainId ?? 0; $sjId = $shop->sjId ?? 0; $customId = $shop->defaultCustomId ?? 0; if (empty($customId)) { noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } $custom = CustomClass::getById($customId); if (empty($custom)) { noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '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:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } $mtGoodsData = json_decode($detail, true); if (empty($mtGoodsData)) { noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } Yii::info('商品信息:' . json_encode($mtGoodsData)); $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:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } $caution = isset($post['caution']) && !empty($post['caution']) ? $post['caution'] : ''; $remark = $caution; $bookMobile = $post['order_phone_number'] ?? ''; $receiveMobile = $post['recipient_phone'] ?? ''; $receiveUserName = $post['recipient_name'] ?? ''; $fullAddress = $post['recipient_address'] ?? ''; $cover = ''; $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']; } } $goodsPrice = $mtGoods['price'] ?? 0; $num = $mtGoods['quantity'] ?? 0; $goodsNum = bcadd($num, $goodsNum); $currentGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true); if (empty($currentGoods)) { noticeUtil::push("美团确认订单报错,数据库没有相应商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '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'] ?? ''; if (empty($poi_receive_detail_yuan)) { noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); util::stop(); } $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true); if (is_array($poi_receive_detail_yuan_data) == false) { noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); util::stop(); } $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0; if ($onlinePayment == 0) { noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); util::stop(); } $modifyPrice = $onlinePayment; //技术服务费 $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0; $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? ''; //履约服务费 $performanceServiceFee = 0; if (!empty($reconciliationExtras)) { $extArr = json_decode($reconciliationExtras, true); $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0; } //运费 $sendCost = $post['shipping_fee'] ?? 0; //打包费 $labourCost = $post['package_bag_money_yuan'] ?? 0; //服务费和手续费 $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2); $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0; $reachPeriod = date("H:i", $estimate_arrival_time); $reachDate = date("Y-m-d", $estimate_arrival_time); $reachTime = $estimate_arrival_time; $orderData = [ 'product' => $productList, 'fromType' => dict::getDict('fromType', 'mt'), 'labourCost' => $labourCost, 'sendCost' => $sendCost, 'shopId' => $shopId, 'mainId' => $mainId, 'sjId' => $sjId, 'modifyPrice' => $modifyPrice, 'serviceFee' => $serviceFee, 'thirdSn' => $thirdSn, 'cover' => $cover, 'reachTime' => $reachTime, 'reachDate' => $reachDate, 'reachPeriod' => $reachPeriod, 'remark' => $remark, 'goodsNum' => $goodsNum, 'bookMobile' => $bookMobile, 'receiveMobile' => $receiveMobile, 'receiveUserName' => $receiveUserName, 'fullAddress' => $fullAddress, 'thirdOrderId' => $mtOrderId, ]; $hasPay = dict::getDict('hasPay', 'payed'); $order = OrderService::createFinishOrder($orderData, $custom, $hasPay); $transaction->commit(); //新制作单提示 $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true); ShopExtClass::newWorkRemind($shopExt, $order); return Json::encode(['data' => 'ok']); } catch (\Exception $e) { $transaction->rollBack(); $msg = $e->getMessage(); noticeUtil::push("确认订单报错了,美团订单id:{$mtOrderId} 美团编号:{$thirdSn},错误信息:{$msg}", '15280215347'); return Json::encode(['data' => 'ok']); } } //取消订单 public function actionCancelOrder() { $post = Yii::$app->request->post(); $mtOrderId = $post['order_id'] ?? 0; $shopId = $post['app_poi_code'] ?? ''; if (empty($post)) { return Json::encode(['data' => 'ok']); } Yii::info('cancelOrderData: ' . json_encode($post)); //4秒内不允许第三方重复通知 $cacheKey = 'mt_cancel_order_' . $mtOrderId; $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]); if (!empty($has)) { Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}"); return Json::encode(['data' => 'ok']); } Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']); $mt = dict::getDict('fromType', 'mt'); $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true); if (empty($order)) { //没有接单时取消订单 return Json::encode(['data' => 'ok']); } //只要制作单没有完成,则走取消订单流程。如果有制作单完成,则订单状态和制作单暂时先不管,后面要兼容。 $orderId = $order->id ?? 0; $workList = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true); $hasCompleteWork = false; if (!empty($workList)) { foreach ($workList as $work) { if ($work->status == 1) { $hasCompleteWork = true; } } } //取消订单和制作单 if ($hasCompleteWork == false) { $order->status = 5; $order->save(); if (!empty($workList)) { foreach ($workList as $work) { $work->status = 2; $work->save(); } } } //如果制作单完成了还会出现取消订单情况,这个等于全部退款,这个后面再考虑兼容!!!!! //制作单取消通知 $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true); ShopExtClass::orderCancelRemind($shopExt, $order); return Json::encode(['data' => 'ok']); } //推送已支付订单 public function actionNewOrder() { return Json::encode(['data' => 'ok']); } //部分退款 public function actionPartRefund() { return Json::encode(['data' => 'ok']); } //全部退款 public function actionAllRefund() { return Json::encode(['data' => 'ok']); } }