MtController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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\services\OrderService;
  8. use bizHd\shop\classes\ShopClass;
  9. use common\components\arrayUtil;
  10. use common\components\dict;
  11. use common\components\httpUtil;
  12. use common\components\noticeUtil;
  13. use common\components\util;
  14. use Yii;
  15. use Shishaoqi\MeituanFlashPurchase\Application;
  16. use yii\helpers\Json;
  17. class MtController extends PublicController
  18. {
  19. public $guestAccess = [];
  20. private $config = [
  21. 'app_id' => '119094',
  22. 'app_secret' => 'b7e86d768391180f805621b40ec31e33',
  23. 'request_url' => '', // 默认 `https://waimaiopen.meituan.com/api/v1/`
  24. ];
  25. //部分退款
  26. public function actionPartRefund()
  27. {
  28. $post = Yii::$app->request->post();
  29. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  30. return Json::encode(['data' => 'ok']);
  31. }
  32. $data = arrayUtil::humpUnderlineConversion($post, 1);
  33. Yii::warning('partRefund: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
  34. }
  35. //全部退款
  36. public function actionAllRefund()
  37. {
  38. $post = Yii::$app->request->post();
  39. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  40. return Json::encode(['data' => 'ok']);
  41. }
  42. $data = arrayUtil::humpUnderlineConversion($post, 1);
  43. Yii::warning('allRefund: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
  44. }
  45. //已确认订单通知
  46. public function actionConfirmOrder()
  47. {
  48. $post = Yii::$app->request->post();
  49. $orderId = $post['order_id'] ?? 0;
  50. //4秒内不允许第三方重复通知
  51. $cacheKey = 'mt_confirm_order_' . $orderId;
  52. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  53. if (!empty($has)) {
  54. Yii::info("美团已确认订单通知,出现重复通知,流水类型:orderId:{$orderId}");
  55. return Json::encode(['data' => 'ok']);
  56. }
  57. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  58. // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  59. if (empty($post)) {
  60. return Json::encode(['data' => 'ok']);
  61. }
  62. Yii::info('confirmOrderData: ' . json_encode($post));
  63. $connection = Yii::$app->db;//事务处理
  64. $transaction = $connection->beginTransaction();
  65. try {
  66. $shopId = $post['app_poi_code'] ?? '';
  67. $shop = ShopClass::getById($shopId, true);
  68. if (empty($shop)) {
  69. noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$orderId}", '15280215347');
  70. return Json::encode(['data' => 'ok']);
  71. }
  72. $mainId = $shop->mainId ?? 0;
  73. $sjId = $shop->sjId ?? 0;
  74. $customId = $shop->defaultCustomId ?? 0;
  75. if (empty($customId)) {
  76. noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$orderId}", '15280215347');
  77. return Json::encode(['data' => 'ok']);
  78. }
  79. $custom = CustomClass::getById($customId);
  80. if (empty($custom)) {
  81. noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$orderId}", '15280215347');
  82. return Json::encode(['data' => 'ok']);
  83. }
  84. foreach ($post as $k => $val) {
  85. $post[$k] = urldecode($val);
  86. }
  87. MtOrderClass::add($post);
  88. $detail = $post['detail'] ?? '';
  89. if (empty($detail)) {
  90. noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$orderId}", '15280215347');
  91. return Json::encode(['data' => 'ok']);
  92. }
  93. $mtGoodsData = json_decode($detail, true);
  94. if (empty($mtGoodsData)) {
  95. noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$orderId}", '15280215347');
  96. return Json::encode(['data' => 'ok']);
  97. }
  98. $app = new Application($this->config);
  99. $productList = [];
  100. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  101. if (empty($mtCategory)) {
  102. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  103. }
  104. $catId = $mtCategory->id ?? 0;
  105. if (empty($catId)) {
  106. noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$orderId}", '15280215347');
  107. return Json::encode(['data' => 'ok']);
  108. }
  109. $caution = $post['caution'] ?? '';
  110. $remark = $caution;
  111. $bookMobile = $post['order_phone_number'] ?? '';
  112. $receiveMobile = $post['recipient_phone'] ?? '';
  113. $receiveUserName = $post['recipient_name'] ?? '';
  114. $fullAddress = $post['recipient_address'] ?? '';
  115. $orderCover = '';
  116. $goodsNum = 0;
  117. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  118. $mtGoodsSpu = $mtGoods['app_spu_code'];
  119. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  120. $mgGoodsDetail = $app->retail->getDetail($params);
  121. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  122. if (isset($mgGoodsDetail['error'])) {
  123. noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$orderId}", '15280215347');
  124. util::stop();
  125. }
  126. $imgString = $mgGoodsDetail['data']['picture'];
  127. $goodsName = $mtGoods['food_name'] ?? '';
  128. $goodsPrice = $mtGoods['price'] ?? 0;
  129. $num = $mtGoods['quantity'] ?? 0;
  130. $goodsNum = bcadd($num, $goodsNum);
  131. $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  132. if (empty($hasGoods)) {
  133. $imgArr = explode(',', $imgString);
  134. $shopImg = [];
  135. foreach ($imgArr as $imgUrl) {
  136. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  137. $shopImg[] = $shortUrl;
  138. if ($orderCover == '') {
  139. $orderCover = $shortUrl;
  140. }
  141. }
  142. $cover = $shopImg[0];
  143. $goodsData = [
  144. 'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
  145. 'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => $cover, 'unitGoodsPrice' => $goodsPrice, 'num' => $num,
  146. ];
  147. $goods = GoodsClass::orderCreateGoods($goodsData);
  148. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  149. } else {
  150. $id = $hasGoods->id ?? 0;
  151. $unitType = 0;
  152. $property = dict::getDict('property', 'goods');
  153. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  154. $imgMd5 = Yii::$app->redis->executeCommand('GET', ['mt_goods_spu_' . $mtGoodsSpu]);
  155. if (empty($imgMd5) || $imgMd5 != md5($imgString)) {
  156. //图片需要更新
  157. $imgArr = explode(',', $imgString);
  158. $shopImg = [];
  159. $currentCover = '';
  160. foreach ($imgArr as $imgUrl) {
  161. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  162. $shopImg[] = $shortUrl;
  163. if ($currentCover == '') {
  164. $currentCover = $shortUrl;
  165. }
  166. }
  167. $hasGoods->cover = $currentCover;
  168. $hasGoods->shopImg = json_encode($shopImg);
  169. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  170. }
  171. $hasGoods->name = $goodsName;
  172. $hasGoods->price = $goodsPrice;
  173. $hasGoods->save();
  174. if ($orderCover == '') {
  175. $orderCover = $hasGoods->cover ?? '';
  176. }
  177. }
  178. $productList = array_merge($goods, $productList);
  179. }
  180. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  181. if (empty($poi_receive_detail_yuan)) {
  182. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$orderId}", '15280215347');
  183. util::stop();
  184. }
  185. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  186. if (is_array($poi_receive_detail_yuan_data) == false) {
  187. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$orderId}", '15280215347');
  188. util::stop();
  189. }
  190. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  191. if ($onlinePayment == 0) {
  192. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$orderId}", '15280215347');
  193. util::stop();
  194. }
  195. $modifyPrice = $onlinePayment;
  196. //技术服务费
  197. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  198. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  199. //履约服务费
  200. $performanceServiceFee = 0;
  201. if (!empty($reconciliationExtras)) {
  202. $extArr = json_decode($reconciliationExtras, true);
  203. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  204. }
  205. //运费
  206. $sendCost = $post['shipping_fee'] ?? 0;
  207. //打包费
  208. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  209. //服务费和手续费
  210. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  211. $thirdSn = $post['day_seq'] ?? 0;
  212. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  213. $reachPeriod = date("H:i", $estimate_arrival_time);
  214. $reachDate = date("Y-m-d", $estimate_arrival_time);
  215. $reachTime = $estimate_arrival_time;
  216. $orderData = [
  217. 'product' => $productList,
  218. 'fromType' => dict::getDict('fromType', 'mt'),
  219. 'labourCost' => $labourCost,
  220. 'sendCost' => $sendCost,
  221. 'shopId' => $shopId,
  222. 'mainId' => $mainId,
  223. 'sjId' => $sjId,
  224. 'modifyPrice' => $modifyPrice,
  225. 'serviceFee' => $serviceFee,
  226. 'thirdSn' => $thirdSn,
  227. 'cover' => $orderCover,
  228. 'reachTime' => $reachTime,
  229. 'reachDate' => $reachDate,
  230. 'reachPeriod' => $reachPeriod,
  231. 'remark' => $remark,
  232. 'goodsNum' => $goodsNum,
  233. 'bookMobile' => $bookMobile,
  234. 'receiveMobile' => $receiveMobile,
  235. 'receiveUserName' => $receiveUserName,
  236. 'fullAddress' => $fullAddress,
  237. ];
  238. $hasPay = dict::getDict('hasPay', 'payed');
  239. OrderService::createFinishOrder($orderData, $custom, $hasPay);
  240. $transaction->commit();
  241. return Json::encode(['data' => 'ok']);
  242. } catch (\Exception $e) {
  243. $transaction->rollBack();
  244. $msg = $e->getMessage();
  245. noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$orderId}", '15280215347');
  246. }
  247. }
  248. public function actionNewOrder()
  249. {
  250. $post = Yii::$app->request->post();
  251. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  252. return Json::encode(['data' => 'ok']);
  253. }
  254. return Json::encode(['data' => 'ok']);
  255. }
  256. public function actionCancelOrder()
  257. {
  258. $post = Yii::$app->request->post();
  259. if (empty($post)) {
  260. return Json::encode(['data' => 'ok']);
  261. }
  262. Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
  263. return Json::encode(['data' => 'ok']);
  264. }
  265. public function demo()
  266. {
  267. $app = new Application($this->config);
  268. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  269. $orderDetail = $app->order->getOrderDetail($params);
  270. print_r(json_decode($orderDetail, true));
  271. // 商品类、活动类接口支持异步队列
  272. $app->goods->async()->create([]);
  273. }
  274. }