MtController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  50. return Json::encode(['data' => 'ok']);
  51. }
  52. Yii::info('newOrder1: ' . json_encode($post));
  53. exit();
  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. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  102. $mtGoodsSpu = $mtGoods['app_spu_code'];
  103. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  104. $mgGoodsDetail = $app->retail->getDetail($params);
  105. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  106. if (isset($mgGoodsDetail['error'])) {
  107. noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$orderId}", '15280215347');
  108. util::stop();
  109. }
  110. $imgString = $mgGoodsDetail['data']['picture'];
  111. $goodsName = $mtGoods['food_name'] ?? '';
  112. $goodsPrice = $mtGoods['price'] ?? 0;
  113. $num = $mtGoods['quantity'] ?? 0;
  114. $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  115. if (empty($hasGoods)) {
  116. $imgArr = explode(',', $imgString);
  117. $shopImg = [];
  118. foreach ($imgArr as $imgUrl) {
  119. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  120. $shopImg[] = $shortUrl;
  121. }
  122. $goodsData = [
  123. 'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
  124. 'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => 0, '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. foreach ($imgArr as $imgUrl) {
  139. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  140. $shopImg[] = $shortUrl;
  141. }
  142. $hasGoods->shopImg = $shopImg;
  143. }
  144. $hasGoods->name = $goodsName;
  145. $hasGoods->price = $goodsPrice;
  146. $hasGoods->save();
  147. }
  148. $productList = array_merge($goods, $productList);
  149. }
  150. $poi_receive_detail_yuan = $data['poi_receive_detail_yuan'] ?? '';
  151. if (empty($poi_receive_detail_yuan)) {
  152. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$orderId}", '15280215347');
  153. util::stop();
  154. }
  155. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  156. if (is_array($poi_receive_detail_yuan_data) == false) {
  157. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$orderId}", '15280215347');
  158. util::stop();
  159. }
  160. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  161. if ($onlinePayment == 0) {
  162. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$orderId}", '15280215347');
  163. util::stop();
  164. }
  165. //运费
  166. $sendCost = $data['shipping_fee'] ?? 0;
  167. //打包费
  168. $labourCost = $data['package_bag_money_yuan'] ?? 0;
  169. $modifyPrice = $onlinePayment;
  170. $orderData = [
  171. 'product' => $productList,
  172. 'fromType' => dict::getDict('fromType', 'mt'),
  173. 'labourCost' => $labourCost,
  174. 'sendCost' => $sendCost,
  175. 'shopId' => $shopId,
  176. 'mainId' => $mainId,
  177. 'sjId' => $sjId,
  178. ];
  179. $hasPay = dict::getDict('hasPay', 'payed');
  180. OrderService::createFinishOrder($orderData, $custom, $hasPay);
  181. $transaction->commit();
  182. return Json::encode(['data' => 'ok']);
  183. } catch (\Exception $e) {
  184. $transaction->rollBack();
  185. $msg = $e->getMessage();
  186. noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$orderId}", '15280215347');
  187. }
  188. }
  189. public function actionNewOrder()
  190. {
  191. $post = Yii::$app->request->post();
  192. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  193. return Json::encode(['data' => 'ok']);
  194. }
  195. return Json::encode(['data' => 'ok']);
  196. Yii::warning('newOrder2: ' . json_encode($post));
  197. // 美团传入的订单数据,中文是 URL_encode 的,执行下解密
  198. foreach ($post as $k => $val) {
  199. $post[$k] = urldecode($val);
  200. }
  201. $data = arrayUtil::humpUnderlineConversion($post, 1);
  202. Yii::warning('newOrder: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
  203. $orderId = $data['orderId'];
  204. $mtOrder = MtOrderClass::getByCondition(['orderId' => $orderId]);
  205. if (!empty($mtOrder)) {
  206. Yii::warning('订单号已存在:' . $orderId);
  207. return Json::encode(['data' => 'ok']);
  208. }
  209. // TODO 通过 app_poi_code 查询批发商的美团商家帐号,获取 access_token 与 refresh_token
  210. // TODO 做到上一步,要实现好批发商把他的美团商家账号授权给花掌柜
  211. $mtOrder = MtOrderClass::add($data);
  212. $picArr = [];
  213. $app = new Application($this->config);
  214. $detail = $data['detail'];
  215. $items = json_decode($detail, true);
  216. foreach ($items as $item) {
  217. $mtGoodsSpu = $item['app_spu_code'];
  218. // access_token 是商家授权后,获取到的
  219. $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  220. $mgGoodsDetail = $app->retail->getDetail($params);
  221. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  222. if (isset($mgGoodsDetail['error'])) {
  223. Yii::error('get retail fail:' . json_encode($params));
  224. } else {
  225. $picArr[$mtGoodsSpu] = $mgGoodsDetail['data']['picture'];
  226. }
  227. }
  228. Yii::info('picture: ' . json_encode($picArr)); // 订单商品图片
  229. return Json::encode(['data' => 'ok']);
  230. }
  231. public function actionCancelOrder()
  232. {
  233. $post = Yii::$app->request->post();
  234. if (empty($post)) {
  235. return Json::encode(['data' => 'ok']);
  236. }
  237. Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
  238. return Json::encode(['data' => 'ok']);
  239. }
  240. public function demo()
  241. {
  242. $app = new Application($this->config);
  243. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  244. $orderDetail = $app->order->getOrderDetail($params);
  245. print_r(json_decode($orderDetail, true));
  246. // 商品类、活动类接口支持异步队列
  247. $app->goods->async()->create([]);
  248. }
  249. }