MtController.php 13 KB

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