MtController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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'] ?? 0;
  98. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  99. $mgGoodsDetail = $app->retail->getDetail($params);
  100. Yii::info('获取商品报错了:' . $mgGoodsDetail . ' ' . json_encode($params));
  101. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  102. if (isset($mgGoodsDetail['error'])) {
  103. noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$mtOrderId}", '15280215347');
  104. util::stop();
  105. }
  106. $imgString = $mgGoodsDetail['data']['picture'];
  107. $goodsName = $mtGoods['food_name'] ?? '';
  108. $goodsPrice = $mtGoods['price'] ?? 0;
  109. $num = $mtGoods['quantity'] ?? 0;
  110. $goodsNum = bcadd($num, $goodsNum);
  111. $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  112. if (empty($hasGoods)) {
  113. $imgArr = explode(',', $imgString);
  114. $shopImg = [];
  115. foreach ($imgArr as $imgUrl) {
  116. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  117. $shopImg[] = $shortUrl;
  118. if ($orderCover == '') {
  119. $orderCover = $shortUrl;
  120. }
  121. }
  122. $cover = $shopImg[0];
  123. $goodsData = [
  124. 'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
  125. 'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => $cover, 'unitGoodsPrice' => $goodsPrice, 'num' => $num,
  126. ];
  127. $goods = GoodsClass::orderCreateGoods($goodsData);
  128. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  129. } else {
  130. $id = $hasGoods->id ?? 0;
  131. $unitType = 0;
  132. $property = dict::getDict('property', 'goods');
  133. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  134. $imgMd5 = Yii::$app->redis->executeCommand('GET', ['mt_goods_spu_' . $mtGoodsSpu]);
  135. if (empty($imgMd5) || $imgMd5 != md5($imgString)) {
  136. //图片需要更新
  137. $imgArr = explode(',', $imgString);
  138. $shopImg = [];
  139. $currentCover = '';
  140. foreach ($imgArr as $imgUrl) {
  141. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  142. $shopImg[] = $shortUrl;
  143. if ($currentCover == '') {
  144. $currentCover = $shortUrl;
  145. }
  146. }
  147. $hasGoods->cover = $currentCover;
  148. $hasGoods->shopImg = json_encode($shopImg);
  149. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  150. }
  151. $hasGoods->name = $goodsName;
  152. $hasGoods->price = $goodsPrice;
  153. $hasGoods->save();
  154. if ($orderCover == '') {
  155. $orderCover = $hasGoods->cover ?? '';
  156. }
  157. }
  158. $productList = array_merge($goods, $productList);
  159. }
  160. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  161. if (empty($poi_receive_detail_yuan)) {
  162. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId}", '15280215347');
  163. util::stop();
  164. }
  165. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  166. if (is_array($poi_receive_detail_yuan_data) == false) {
  167. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId}", '15280215347');
  168. util::stop();
  169. }
  170. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  171. if ($onlinePayment == 0) {
  172. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId}", '15280215347');
  173. util::stop();
  174. }
  175. $modifyPrice = $onlinePayment;
  176. //技术服务费
  177. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  178. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  179. //履约服务费
  180. $performanceServiceFee = 0;
  181. if (!empty($reconciliationExtras)) {
  182. $extArr = json_decode($reconciliationExtras, true);
  183. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  184. }
  185. //运费
  186. $sendCost = $post['shipping_fee'] ?? 0;
  187. //打包费
  188. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  189. //服务费和手续费
  190. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  191. $thirdSn = $post['day_seq'] ?? 0;
  192. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  193. $reachPeriod = date("H:i", $estimate_arrival_time);
  194. $reachDate = date("Y-m-d", $estimate_arrival_time);
  195. $reachTime = $estimate_arrival_time;
  196. $orderData = [
  197. 'product' => $productList,
  198. 'fromType' => dict::getDict('fromType', 'mt'),
  199. 'labourCost' => $labourCost,
  200. 'sendCost' => $sendCost,
  201. 'shopId' => $shopId,
  202. 'mainId' => $mainId,
  203. 'sjId' => $sjId,
  204. 'modifyPrice' => $modifyPrice,
  205. 'serviceFee' => $serviceFee,
  206. 'thirdSn' => $thirdSn,
  207. 'cover' => $orderCover,
  208. 'reachTime' => $reachTime,
  209. 'reachDate' => $reachDate,
  210. 'reachPeriod' => $reachPeriod,
  211. 'remark' => $remark,
  212. 'goodsNum' => $goodsNum,
  213. 'bookMobile' => $bookMobile,
  214. 'receiveMobile' => $receiveMobile,
  215. 'receiveUserName' => $receiveUserName,
  216. 'fullAddress' => $fullAddress,
  217. 'thirdOrderId' => $mtOrderId,
  218. ];
  219. $hasPay = dict::getDict('hasPay', 'payed');
  220. OrderService::createFinishOrder($orderData, $custom, $hasPay);
  221. $transaction->commit();
  222. return Json::encode(['data' => 'ok']);
  223. } catch (\Exception $e) {
  224. $transaction->rollBack();
  225. $msg = $e->getMessage();
  226. noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$mtOrderId}", '15280215347');
  227. }
  228. }
  229. //取消订单
  230. public function actionCancelOrder()
  231. {
  232. $post = Yii::$app->request->post();
  233. $mtOrderId = $post['order_id'] ?? 0;
  234. if (empty($post)) {
  235. return Json::encode(['data' => 'ok']);
  236. }
  237. Yii::info('cancelOrderData: ' . json_encode($post));
  238. //4秒内不允许第三方重复通知
  239. $cacheKey = 'mt_cancel_order_' . $mtOrderId;
  240. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  241. if (!empty($has)) {
  242. Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}");
  243. return Json::encode(['data' => 'ok']);
  244. }
  245. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  246. $mt = dict::getDict('fromType', 'mt');
  247. $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true);
  248. if (empty($order)) {
  249. //没有接单时取消订单
  250. return Json::encode(['data' => 'ok']);
  251. }
  252. //只要制作单没有完成,则走取消订单流程。如果有制作单完成,则订单状态和制作单暂时先不管,后面要兼容。
  253. $orderId = $order->id ?? 0;
  254. $workList = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true);
  255. $hasCompleteWork = false;
  256. if (!empty($workList)) {
  257. foreach ($workList as $work) {
  258. if ($work->status == 1) {
  259. $hasCompleteWork = true;
  260. }
  261. }
  262. }
  263. //取消订单和制作单
  264. if ($hasCompleteWork == false) {
  265. $order->status = 5;
  266. $order->save();
  267. if (!empty($workList)) {
  268. foreach ($workList as $work) {
  269. $work->status == 2;
  270. $work->save();
  271. }
  272. }
  273. }
  274. //如果制作单完成了还会出现取消订单情况,这个等于全部退款,这个后面再考虑兼容!!!!!
  275. return Json::encode(['data' => 'ok']);
  276. }
  277. public function demo()
  278. {
  279. $config = dict::getDict('mtConfig');
  280. $app = new Application($config);
  281. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  282. $orderDetail = $app->order->getOrderDetail($params);
  283. print_r(json_decode($orderDetail, true));
  284. // 商品类、活动类接口支持异步队列
  285. $app->goods->async()->create([]);
  286. }
  287. }