MtController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  50. if (empty($post)) {
  51. return Json::encode(['data' => 'ok']);
  52. }
  53. Yii::info('confirmOrderData: ' . json_encode($post));
  54. $connection = Yii::$app->db;//事务处理
  55. $transaction = $connection->beginTransaction();
  56. $orderId = $post['order_id'] ?? 0;
  57. try {
  58. $shopId = $post['app_poi_code'] ?? '';
  59. $shop = ShopClass::getById($shopId, true);
  60. if (empty($shop)) {
  61. noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$orderId}", '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:{$orderId}", '15280215347');
  69. return Json::encode(['data' => 'ok']);
  70. }
  71. $custom = CustomClass::getById($customId);
  72. if (empty($custom)) {
  73. noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$orderId}", '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:{$orderId}", '15280215347');
  83. return Json::encode(['data' => 'ok']);
  84. }
  85. $mtGoodsData = json_decode($detail, true);
  86. if (empty($mtGoodsData)) {
  87. noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$orderId}", '15280215347');
  88. return Json::encode(['data' => 'ok']);
  89. }
  90. $app = new Application($this->config);
  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:{$orderId}", '15280215347');
  99. return Json::encode(['data' => 'ok']);
  100. }
  101. $caution = $post['caution'] ?? '';
  102. $remark = $caution;
  103. $orderCover = '';
  104. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  105. $mtGoodsSpu = $mtGoods['app_spu_code'];
  106. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  107. $mgGoodsDetail = $app->retail->getDetail($params);
  108. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  109. if (isset($mgGoodsDetail['error'])) {
  110. noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$orderId}", '15280215347');
  111. util::stop();
  112. }
  113. $imgString = $mgGoodsDetail['data']['picture'];
  114. $goodsName = $mtGoods['food_name'] ?? '';
  115. $goodsPrice = $mtGoods['price'] ?? 0;
  116. $num = $mtGoods['quantity'] ?? 0;
  117. $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  118. if (empty($hasGoods)) {
  119. $imgArr = explode(',', $imgString);
  120. $shopImg = [];
  121. foreach ($imgArr as $imgUrl) {
  122. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  123. $shopImg[] = $shortUrl;
  124. if ($orderCover == '') {
  125. $orderCover = $shortUrl;
  126. }
  127. }
  128. $cover = $shopImg[0];
  129. $goodsData = [
  130. 'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
  131. 'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => $cover, 'unitGoodsPrice' => $goodsPrice, 'num' => $num,
  132. ];
  133. $goods = GoodsClass::orderCreateGoods($goodsData);
  134. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  135. } else {
  136. $id = $hasGoods->id ?? 0;
  137. $unitType = 0;
  138. $property = dict::getDict('property', 'goods');
  139. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  140. $imgMd5 = Yii::$app->redis->executeCommand('GET', ['mt_goods_spu_' . $mtGoodsSpu]);
  141. if (empty($imgMd5) || $imgMd5 != md5($imgString)) {
  142. //图片需要更新
  143. $imgArr = explode(',', $imgString);
  144. $shopImg = [];
  145. $currentCover = '';
  146. foreach ($imgArr as $imgUrl) {
  147. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  148. $shopImg[] = $shortUrl;
  149. if ($currentCover == '') {
  150. $currentCover = $shortUrl;
  151. }
  152. }
  153. $hasGoods->cover = $currentCover;
  154. $hasGoods->shopImg = $shopImg;
  155. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  156. }
  157. $hasGoods->name = $goodsName;
  158. $hasGoods->price = $goodsPrice;
  159. $hasGoods->save();
  160. if ($orderCover == '') {
  161. $orderCover = $hasGoods->cover ?? '';
  162. }
  163. }
  164. $productList = array_merge($goods, $productList);
  165. }
  166. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  167. if (empty($poi_receive_detail_yuan)) {
  168. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$orderId}", '15280215347');
  169. util::stop();
  170. }
  171. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  172. if (is_array($poi_receive_detail_yuan_data) == false) {
  173. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$orderId}", '15280215347');
  174. util::stop();
  175. }
  176. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  177. if ($onlinePayment == 0) {
  178. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$orderId}", '15280215347');
  179. util::stop();
  180. }
  181. $modifyPrice = $onlinePayment;
  182. //技术服务费
  183. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  184. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  185. //履约服务费
  186. $performanceServiceFee = 0;
  187. if (!empty($reconciliationExtras)) {
  188. $extArr = json_decode($reconciliationExtras, true);
  189. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  190. }
  191. //运费
  192. $sendCost = $post['shipping_fee'] ?? 0;
  193. //打包费
  194. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  195. //服务费和手续费
  196. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  197. $thirdSn = $post['day_seq'] ?? 0;
  198. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  199. $reachPeriod = date("H:i", $estimate_arrival_time);
  200. $reachDate = date("Y-m-d", $estimate_arrival_time);
  201. $reachTime = $estimate_arrival_time;
  202. $orderData = [
  203. 'product' => $productList,
  204. 'fromType' => dict::getDict('fromType', 'mt'),
  205. 'labourCost' => $labourCost,
  206. 'sendCost' => $sendCost,
  207. 'shopId' => $shopId,
  208. 'mainId' => $mainId,
  209. 'sjId' => $sjId,
  210. 'modifyPrice' => $modifyPrice,
  211. 'serviceFee' => $serviceFee,
  212. 'thirdSn' => $thirdSn,
  213. 'cover' => $orderCover,
  214. 'reachTime' => $reachTime,
  215. 'reachDate' => $reachDate,
  216. 'reachPeriod' => $reachPeriod,
  217. 'remark' => $remark,
  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:{$orderId}", '15280215347');
  227. }
  228. }
  229. public function actionNewOrder()
  230. {
  231. $post = Yii::$app->request->post();
  232. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  233. return Json::encode(['data' => 'ok']);
  234. }
  235. return Json::encode(['data' => 'ok']);
  236. }
  237. public function actionCancelOrder()
  238. {
  239. $post = Yii::$app->request->post();
  240. if (empty($post)) {
  241. return Json::encode(['data' => 'ok']);
  242. }
  243. Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
  244. return Json::encode(['data' => 'ok']);
  245. }
  246. public function demo()
  247. {
  248. $app = new Application($this->config);
  249. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  250. $orderDetail = $app->order->getOrderDetail($params);
  251. print_r(json_decode($orderDetail, true));
  252. // 商品类、活动类接口支持异步队列
  253. $app->goods->async()->create([]);
  254. }
  255. }