MtController.php 14 KB

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