'119094', 'app_secret' => 'b7e86d768391180f805621b40ec31e33', 'request_url' => '', // 默认 `https://waimaiopen.meituan.com/api/v1/` ]; //部分退款 public function actionPartRefund() { $post = Yii::$app->request->post(); if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回 return Json::encode(['data' => 'ok']); } $data = arrayUtil::humpUnderlineConversion($post, 1); Yii::warning('partRefund: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出 } //全部退款 public function actionAllRefund() { $post = Yii::$app->request->post(); if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回 return Json::encode(['data' => 'ok']); } $data = arrayUtil::humpUnderlineConversion($post, 1); Yii::warning('allRefund: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出 } //已确认订单通知 public function actionConfirmOrder() { $post = Yii::$app->request->post(); if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回 return Json::encode(['data' => 'ok']); } Yii::info('newOrder1: ' . json_encode($post)); exit(); $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() { $post = Yii::$app->request->post(); if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回 return Json::encode(['data' => 'ok']); } return Json::encode(['data' => 'ok']); Yii::warning('newOrder2: ' . json_encode($post)); // 美团传入的订单数据,中文是 URL_encode 的,执行下解密 foreach ($post as $k => $val) { $post[$k] = urldecode($val); } $data = arrayUtil::humpUnderlineConversion($post, 1); Yii::warning('newOrder: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出 $orderId = $data['orderId']; $mtOrder = MtOrderClass::getByCondition(['orderId' => $orderId]); if (!empty($mtOrder)) { Yii::warning('订单号已存在:' . $orderId); return Json::encode(['data' => 'ok']); } // TODO 通过 app_poi_code 查询批发商的美团商家帐号,获取 access_token 与 refresh_token // TODO 做到上一步,要实现好批发商把他的美团商家账号授权给花掌柜 $mtOrder = MtOrderClass::add($data); $picArr = []; $app = new Application($this->config); $detail = $data['detail']; $items = json_decode($detail, true); foreach ($items as $item) { $mtGoodsSpu = $item['app_spu_code']; // access_token 是商家授权后,获取到的 $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[$mtGoodsSpu] = $mgGoodsDetail['data']['picture']; } } Yii::info('picture: ' . json_encode($picArr)); // 订单商品图片 return Json::encode(['data' => 'ok']); } public function actionCancelOrder() { $post = Yii::$app->request->post(); if (empty($post)) { return Json::encode(['data' => 'ok']); } Yii::warning('cancelOrder: ' . urldecode(json_encode($post))); return Json::encode(['data' => 'ok']); } public function demo() { $app = new Application($this->config); $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1]; $orderDetail = $app->order->getOrderDetail($params); print_r(json_decode($orderDetail, true)); // 商品类、活动类接口支持异步队列 $app->goods->async()->create([]); } }