MtController.php 12 KB

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