MtController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\custom\classes\CustomClass;
  4. use bizHd\goods\classes\CategoryClass;
  5. use bizHd\goods\classes\GoodsClass;
  6. use bizHd\meituan\classes\MtOrderClass;
  7. use bizHd\order\classes\OrderClass;
  8. use bizHd\order\services\OrderService;
  9. use bizHd\shop\classes\ShopClass;
  10. use bizHd\work\classes\WorkClass;
  11. use common\components\arrayUtil;
  12. use common\components\dict;
  13. use common\components\httpUtil;
  14. use common\components\noticeUtil;
  15. use common\components\util;
  16. use Yii;
  17. use Shishaoqi\MeituanFlashPurchase\Application;
  18. use yii\helpers\Json;
  19. class MtController extends PublicController
  20. {
  21. public $guestAccess = [];
  22. //已确认订单通知
  23. public function actionConfirmOrder()
  24. {
  25. $post = Yii::$app->request->post();
  26. $mtOrderId = $post['order_id'] ?? 0;
  27. //4秒内不允许第三方重复通知
  28. $cacheKey = 'mt_confirm_order_' . $mtOrderId;
  29. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  30. if (!empty($has)) {
  31. Yii::info("美团已确认订单通知,出现重复通知,orderId:{$mtOrderId}");
  32. return Json::encode(['data' => 'ok']);
  33. }
  34. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  35. // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  36. if (empty($post)) {
  37. return Json::encode(['data' => 'ok']);
  38. }
  39. Yii::info('confirmOrderData: ' . json_encode($post));
  40. $connection = Yii::$app->db;//事务处理
  41. $transaction = $connection->beginTransaction();
  42. try {
  43. $shopId = $post['app_poi_code'] ?? '';
  44. $shop = ShopClass::getById($shopId, true);
  45. if (empty($shop)) {
  46. noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$mtOrderId}", '15280215347');
  47. return Json::encode(['data' => 'ok']);
  48. }
  49. $mainId = $shop->mainId ?? 0;
  50. $sjId = $shop->sjId ?? 0;
  51. $customId = $shop->defaultCustomId ?? 0;
  52. if (empty($customId)) {
  53. noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$mtOrderId}", '15280215347');
  54. return Json::encode(['data' => 'ok']);
  55. }
  56. $custom = CustomClass::getById($customId);
  57. if (empty($custom)) {
  58. noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$mtOrderId}", '15280215347');
  59. return Json::encode(['data' => 'ok']);
  60. }
  61. foreach ($post as $k => $val) {
  62. $post[$k] = urldecode($val);
  63. }
  64. MtOrderClass::add($post);
  65. $detail = $post['detail'] ?? '';
  66. if (empty($detail)) {
  67. noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$mtOrderId}", '15280215347');
  68. return Json::encode(['data' => 'ok']);
  69. }
  70. $mtGoodsData = json_decode($detail, true);
  71. if (empty($mtGoodsData)) {
  72. noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$mtOrderId}", '15280215347');
  73. return Json::encode(['data' => 'ok']);
  74. }
  75. Yii::info('商品信息:' . json_encode($mtGoodsData));
  76. $config = dict::getDict('mtConfig');
  77. $app = new Application($config);
  78. $productList = [];
  79. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  80. if (empty($mtCategory)) {
  81. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  82. }
  83. $catId = $mtCategory->id ?? 0;
  84. if (empty($catId)) {
  85. noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$mtOrderId}", '15280215347');
  86. return Json::encode(['data' => 'ok']);
  87. }
  88. $caution = $post['caution'] ?? '';
  89. $remark = $caution;
  90. $bookMobile = $post['order_phone_number'] ?? '';
  91. $receiveMobile = $post['recipient_phone'] ?? '';
  92. $receiveUserName = $post['recipient_name'] ?? '';
  93. $fullAddress = $post['recipient_address'] ?? '';
  94. $orderCover = '';
  95. $goodsNum = 0;
  96. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  97. $mtGoodsSpu = $mtGoods['app_spu_code'] ?? '';
  98. if (empty($mtGoodsSpu)) {
  99. if (isset($mtGoods['app_food_code']) && !empty($mtGoods['app_food_code'])) {
  100. $mtGoodsSpu = $mtGoods['app_food_code'];
  101. }
  102. }
  103. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  104. $mgGoodsDetail = $app->retail->getDetail($params);
  105. Yii::info('获取商品报错了:' . $mgGoodsDetail . ' ' . json_encode($params));
  106. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  107. if (isset($mgGoodsDetail['error'])) {
  108. noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$mtOrderId}", '15280215347');
  109. util::stop();
  110. }
  111. $imgString = $mgGoodsDetail['data']['picture'];
  112. $goodsName = $mtGoods['food_name'] ?? '';
  113. $goodsPrice = $mtGoods['price'] ?? 0;
  114. $num = $mtGoods['quantity'] ?? 0;
  115. $goodsNum = bcadd($num, $goodsNum);
  116. $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  117. if (empty($hasGoods)) {
  118. $imgArr = explode(',', $imgString);
  119. $shopImg = [];
  120. foreach ($imgArr as $imgUrl) {
  121. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  122. $shopImg[] = $shortUrl;
  123. if ($orderCover == '') {
  124. $orderCover = $shortUrl;
  125. }
  126. }
  127. $cover = $shopImg[0];
  128. $goodsData = [
  129. 'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
  130. 'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => $cover, 'unitGoodsPrice' => $goodsPrice, 'num' => $num,
  131. ];
  132. $goods = GoodsClass::orderCreateGoods($goodsData);
  133. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  134. } else {
  135. $id = $hasGoods->id ?? 0;
  136. $unitType = 0;
  137. $property = dict::getDict('property', 'goods');
  138. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  139. $imgMd5 = Yii::$app->redis->executeCommand('GET', ['mt_goods_spu_' . $mtGoodsSpu]);
  140. if (empty($imgMd5) || $imgMd5 != md5($imgString)) {
  141. //图片需要更新
  142. $imgArr = explode(',', $imgString);
  143. $shopImg = [];
  144. $currentCover = '';
  145. foreach ($imgArr as $imgUrl) {
  146. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  147. $shopImg[] = $shortUrl;
  148. if ($currentCover == '') {
  149. $currentCover = $shortUrl;
  150. }
  151. }
  152. $hasGoods->cover = $currentCover;
  153. $hasGoods->shopImg = json_encode($shopImg);
  154. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  155. }
  156. $hasGoods->name = $goodsName;
  157. $hasGoods->price = $goodsPrice;
  158. $hasGoods->save();
  159. if ($orderCover == '') {
  160. $orderCover = $hasGoods->cover ?? '';
  161. }
  162. }
  163. $productList = array_merge($goods, $productList);
  164. }
  165. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  166. if (empty($poi_receive_detail_yuan)) {
  167. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId}", '15280215347');
  168. util::stop();
  169. }
  170. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  171. if (is_array($poi_receive_detail_yuan_data) == false) {
  172. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId}", '15280215347');
  173. util::stop();
  174. }
  175. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  176. if ($onlinePayment == 0) {
  177. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId}", '15280215347');
  178. util::stop();
  179. }
  180. $modifyPrice = $onlinePayment;
  181. //技术服务费
  182. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  183. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  184. //履约服务费
  185. $performanceServiceFee = 0;
  186. if (!empty($reconciliationExtras)) {
  187. $extArr = json_decode($reconciliationExtras, true);
  188. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  189. }
  190. //运费
  191. $sendCost = $post['shipping_fee'] ?? 0;
  192. //打包费
  193. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  194. //服务费和手续费
  195. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  196. $thirdSn = $post['day_seq'] ?? 0;
  197. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  198. $reachPeriod = date("H:i", $estimate_arrival_time);
  199. $reachDate = date("Y-m-d", $estimate_arrival_time);
  200. $reachTime = $estimate_arrival_time;
  201. $orderData = [
  202. 'product' => $productList,
  203. 'fromType' => dict::getDict('fromType', 'mt'),
  204. 'labourCost' => $labourCost,
  205. 'sendCost' => $sendCost,
  206. 'shopId' => $shopId,
  207. 'mainId' => $mainId,
  208. 'sjId' => $sjId,
  209. 'modifyPrice' => $modifyPrice,
  210. 'serviceFee' => $serviceFee,
  211. 'thirdSn' => $thirdSn,
  212. 'cover' => $orderCover,
  213. 'reachTime' => $reachTime,
  214. 'reachDate' => $reachDate,
  215. 'reachPeriod' => $reachPeriod,
  216. 'remark' => $remark,
  217. 'goodsNum' => $goodsNum,
  218. 'bookMobile' => $bookMobile,
  219. 'receiveMobile' => $receiveMobile,
  220. 'receiveUserName' => $receiveUserName,
  221. 'fullAddress' => $fullAddress,
  222. 'thirdOrderId' => $mtOrderId,
  223. ];
  224. $hasPay = dict::getDict('hasPay', 'payed');
  225. OrderService::createFinishOrder($orderData, $custom, $hasPay);
  226. $transaction->commit();
  227. return Json::encode(['data' => 'ok']);
  228. } catch (\Exception $e) {
  229. $transaction->rollBack();
  230. $msg = $e->getMessage();
  231. noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$mtOrderId}", '15280215347');
  232. }
  233. }
  234. //取消订单
  235. public function actionCancelOrder()
  236. {
  237. $post = Yii::$app->request->post();
  238. $mtOrderId = $post['order_id'] ?? 0;
  239. if (empty($post)) {
  240. return Json::encode(['data' => 'ok']);
  241. }
  242. Yii::info('cancelOrderData: ' . json_encode($post));
  243. //4秒内不允许第三方重复通知
  244. $cacheKey = 'mt_cancel_order_' . $mtOrderId;
  245. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  246. if (!empty($has)) {
  247. Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}");
  248. return Json::encode(['data' => 'ok']);
  249. }
  250. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  251. $mt = dict::getDict('fromType', 'mt');
  252. $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true);
  253. if (empty($order)) {
  254. //没有接单时取消订单
  255. return Json::encode(['data' => 'ok']);
  256. }
  257. //只要制作单没有完成,则走取消订单流程。如果有制作单完成,则订单状态和制作单暂时先不管,后面要兼容。
  258. $orderId = $order->id ?? 0;
  259. $workList = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true);
  260. $hasCompleteWork = false;
  261. if (!empty($workList)) {
  262. foreach ($workList as $work) {
  263. if ($work->status == 1) {
  264. $hasCompleteWork = true;
  265. }
  266. }
  267. }
  268. //取消订单和制作单
  269. if ($hasCompleteWork == false) {
  270. $order->status = 5;
  271. $order->save();
  272. if (!empty($workList)) {
  273. foreach ($workList as $work) {
  274. $work->status = 2;
  275. $work->save();
  276. }
  277. }
  278. }
  279. //如果制作单完成了还会出现取消订单情况,这个等于全部退款,这个后面再考虑兼容!!!!!
  280. return Json::encode(['data' => 'ok']);
  281. }
  282. public function demo()
  283. {
  284. $config = dict::getDict('mtConfig');
  285. $app = new Application($config);
  286. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  287. $orderDetail = $app->order->getOrderDetail($params);
  288. print_r(json_decode($orderDetail, true));
  289. // 商品类、活动类接口支持异步队列
  290. $app->goods->async()->create([]);
  291. }
  292. }