MtController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. $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. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  112. $mgGoodsDetail = $app->retail->getDetail($params);
  113. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  114. if (isset($mgGoodsDetail['error'])) {
  115. noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$orderId}", '15280215347');
  116. util::stop();
  117. }
  118. $imgString = $mgGoodsDetail['data']['picture'];
  119. $goodsName = $mtGoods['food_name'] ?? '';
  120. $goodsPrice = $mtGoods['price'] ?? 0;
  121. $num = $mtGoods['quantity'] ?? 0;
  122. $goodsNum = bcadd($num, $goodsNum);
  123. $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  124. if (empty($hasGoods)) {
  125. $imgArr = explode(',', $imgString);
  126. $shopImg = [];
  127. foreach ($imgArr as $imgUrl) {
  128. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  129. $shopImg[] = $shortUrl;
  130. if ($orderCover == '') {
  131. $orderCover = $shortUrl;
  132. }
  133. }
  134. $cover = $shopImg[0];
  135. $goodsData = [
  136. 'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
  137. 'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => $cover, 'unitGoodsPrice' => $goodsPrice, 'num' => $num,
  138. ];
  139. $goods = GoodsClass::orderCreateGoods($goodsData);
  140. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  141. } else {
  142. $id = $hasGoods->id ?? 0;
  143. $unitType = 0;
  144. $property = dict::getDict('property', 'goods');
  145. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  146. $imgMd5 = Yii::$app->redis->executeCommand('GET', ['mt_goods_spu_' . $mtGoodsSpu]);
  147. if (empty($imgMd5) || $imgMd5 != md5($imgString)) {
  148. //图片需要更新
  149. $imgArr = explode(',', $imgString);
  150. $shopImg = [];
  151. $currentCover = '';
  152. foreach ($imgArr as $imgUrl) {
  153. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  154. $shopImg[] = $shortUrl;
  155. if ($currentCover == '') {
  156. $currentCover = $shortUrl;
  157. }
  158. }
  159. $hasGoods->cover = $currentCover;
  160. $hasGoods->shopImg = $shopImg;
  161. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  162. }
  163. $hasGoods->name = $goodsName;
  164. $hasGoods->price = $goodsPrice;
  165. $hasGoods->save();
  166. if ($orderCover == '') {
  167. $orderCover = $hasGoods->cover ?? '';
  168. }
  169. }
  170. $productList = array_merge($goods, $productList);
  171. }
  172. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  173. if (empty($poi_receive_detail_yuan)) {
  174. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$orderId}", '15280215347');
  175. util::stop();
  176. }
  177. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  178. if (is_array($poi_receive_detail_yuan_data) == false) {
  179. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$orderId}", '15280215347');
  180. util::stop();
  181. }
  182. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  183. if ($onlinePayment == 0) {
  184. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$orderId}", '15280215347');
  185. util::stop();
  186. }
  187. $modifyPrice = $onlinePayment;
  188. //技术服务费
  189. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  190. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  191. //履约服务费
  192. $performanceServiceFee = 0;
  193. if (!empty($reconciliationExtras)) {
  194. $extArr = json_decode($reconciliationExtras, true);
  195. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  196. }
  197. //运费
  198. $sendCost = $post['shipping_fee'] ?? 0;
  199. //打包费
  200. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  201. //服务费和手续费
  202. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  203. $thirdSn = $post['day_seq'] ?? 0;
  204. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  205. $reachPeriod = date("H:i", $estimate_arrival_time);
  206. $reachDate = date("Y-m-d", $estimate_arrival_time);
  207. $reachTime = $estimate_arrival_time;
  208. $orderData = [
  209. 'product' => $productList,
  210. 'fromType' => dict::getDict('fromType', 'mt'),
  211. 'labourCost' => $labourCost,
  212. 'sendCost' => $sendCost,
  213. 'shopId' => $shopId,
  214. 'mainId' => $mainId,
  215. 'sjId' => $sjId,
  216. 'modifyPrice' => $modifyPrice,
  217. 'serviceFee' => $serviceFee,
  218. 'thirdSn' => $thirdSn,
  219. 'cover' => $orderCover,
  220. 'reachTime' => $reachTime,
  221. 'reachDate' => $reachDate,
  222. 'reachPeriod' => $reachPeriod,
  223. 'remark' => $remark,
  224. 'goodsNum' => $goodsNum,
  225. 'bookMobile' => $bookMobile,
  226. 'receiveMobile' => $receiveMobile,
  227. 'receiveUserName' => $receiveUserName,
  228. 'fullAddress' => $fullAddress,
  229. ];
  230. $hasPay = dict::getDict('hasPay', 'payed');
  231. OrderService::createFinishOrder($orderData, $custom, $hasPay);
  232. $transaction->commit();
  233. return Json::encode(['data' => 'ok']);
  234. } catch (\Exception $e) {
  235. $transaction->rollBack();
  236. $msg = $e->getMessage();
  237. noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$orderId}", '15280215347');
  238. }
  239. }
  240. public function actionNewOrder()
  241. {
  242. $post = Yii::$app->request->post();
  243. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  244. return Json::encode(['data' => 'ok']);
  245. }
  246. return Json::encode(['data' => 'ok']);
  247. }
  248. public function actionCancelOrder()
  249. {
  250. $post = Yii::$app->request->post();
  251. if (empty($post)) {
  252. return Json::encode(['data' => 'ok']);
  253. }
  254. Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
  255. return Json::encode(['data' => 'ok']);
  256. }
  257. public function demo()
  258. {
  259. $app = new Application($this->config);
  260. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  261. $orderDetail = $app->order->getOrderDetail($params);
  262. print_r(json_decode($orderDetail, true));
  263. // 商品类、活动类接口支持异步队列
  264. $app->goods->async()->create([]);
  265. }
  266. }