MtController.php 13 KB

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