MtController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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('newOrder: ' . $post);
  53. Yii::info('newOrder1: ' . json_encode($post));
  54. exit();
  55. $connection = Yii::$app->db;//事务处理
  56. $transaction = $connection->beginTransaction();
  57. $orderId = $post['order_id'] ?? 0;
  58. try {
  59. $shopId = $post['app_poi_code'] ?? '';
  60. $shop = ShopClass::getById($shopId, true);
  61. if (empty($shop)) {
  62. noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$orderId}", '15280215347');
  63. return Json::encode(['data' => 'ok']);
  64. }
  65. $mainId = $shop->mainId ?? 0;
  66. $sjId = $shop->sjId ?? 0;
  67. $customId = $shop->defaultCustomId ?? 0;
  68. if (empty($customId)) {
  69. noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$orderId}", '15280215347');
  70. return Json::encode(['data' => 'ok']);
  71. }
  72. $custom = CustomClass::getById($customId);
  73. if (empty($custom)) {
  74. noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$orderId}", '15280215347');
  75. return Json::encode(['data' => 'ok']);
  76. }
  77. foreach ($post as $k => $val) {
  78. $post[$k] = urldecode($val);
  79. }
  80. MtOrderClass::add($post);
  81. $detail = $post['detail'] ?? '';
  82. if (empty($detail)) {
  83. noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$orderId}", '15280215347');
  84. return Json::encode(['data' => 'ok']);
  85. }
  86. $mtGoodsData = json_decode($detail, true);
  87. if (empty($mtGoodsData)) {
  88. noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$orderId}", '15280215347');
  89. return Json::encode(['data' => 'ok']);
  90. }
  91. $app = new Application($this->config);
  92. $productList = [];
  93. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  94. if (empty($mtCategory)) {
  95. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  96. }
  97. $catId = $mtCategory->id ?? 0;
  98. if (empty($catId)) {
  99. noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$orderId}", '15280215347');
  100. return Json::encode(['data' => 'ok']);
  101. }
  102. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  103. $mtGoodsSpu = $mtGoods['app_spu_code'];
  104. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  105. $mgGoodsDetail = $app->retail->getDetail($params);
  106. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  107. if (isset($mgGoodsDetail['error'])) {
  108. noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$orderId}", '15280215347');
  109. util::stop();
  110. }
  111. $imgString = $mgGoodsDetail['data']['picture'];
  112. $goodsName = $mtGoods['food_name'] ?? '';
  113. $goodsPrice = $mtGoods['price'] ?? 0;
  114. $num = $mtGoods['quantity'] ?? 0;
  115. $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  116. if (empty($hasGoods)) {
  117. $imgArr = explode(',', $imgString);
  118. $shopImg = [];
  119. foreach ($imgArr as $imgUrl) {
  120. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  121. $shopImg[] = $shortUrl;
  122. }
  123. $goodsData = [
  124. 'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
  125. 'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => 0, 'unitGoodsPrice' => $goodsPrice, 'num' => $num,
  126. ];
  127. $goods = GoodsClass::orderCreateGoods($goodsData);
  128. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  129. } else {
  130. $id = $hasGoods->id ?? 0;
  131. $unitType = 0;
  132. $property = dict::getDict('property', 'goods');
  133. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  134. $imgMd5 = Yii::$app->redis->executeCommand('GET', ['mt_goods_spu_' . $mtGoodsSpu]);
  135. if (empty($imgMd5) || $imgMd5 != md5($imgString)) {
  136. //图片需要新更
  137. $imgArr = explode(',', $imgString);
  138. $shopImg = [];
  139. foreach ($imgArr as $imgUrl) {
  140. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  141. $shopImg[] = $shortUrl;
  142. }
  143. $hasGoods->shopImg = $shopImg;
  144. }
  145. $hasGoods->name = $goodsName;
  146. $hasGoods->price = $goodsPrice;
  147. $hasGoods->save();
  148. }
  149. $productList = array_merge($goods, $productList);
  150. }
  151. $sendCost = $data['shipping_fee'] ?? 0;
  152. $modifyPrice = $data[''];
  153. $orderData = [
  154. 'product' => $productList,
  155. 'fromType' => dict::getDict('fromType', 'mt'),
  156. ];
  157. $hasPay = dict::getDict('hasPay', 'payed');
  158. OrderService::createFinishOrder($orderData, $custom, $hasPay);
  159. $transaction->commit();
  160. return Json::encode(['data' => 'ok']);
  161. } catch (\Exception $e) {
  162. $transaction->rollBack();
  163. $msg = $e->getMessage();
  164. noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$orderId}", '15280215347');
  165. }
  166. }
  167. public function actionNewOrder()
  168. {
  169. $post = Yii::$app->request->post();
  170. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  171. return Json::encode(['data' => 'ok']);
  172. }
  173. return Json::encode(['data' => 'ok']);
  174. Yii::warning('newOrder2: ' . json_encode($post));
  175. // 美团传入的订单数据,中文是 URL_encode 的,执行下解密
  176. foreach ($post as $k => $val) {
  177. $post[$k] = urldecode($val);
  178. }
  179. $data = arrayUtil::humpUnderlineConversion($post, 1);
  180. Yii::warning('newOrder: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
  181. $orderId = $data['orderId'];
  182. $mtOrder = MtOrderClass::getByCondition(['orderId' => $orderId]);
  183. if (!empty($mtOrder)) {
  184. Yii::warning('订单号已存在:' . $orderId);
  185. return Json::encode(['data' => 'ok']);
  186. }
  187. // TODO 通过 app_poi_code 查询批发商的美团商家帐号,获取 access_token 与 refresh_token
  188. // TODO 做到上一步,要实现好批发商把他的美团商家账号授权给花掌柜
  189. $mtOrder = MtOrderClass::add($data);
  190. $picArr = [];
  191. $app = new Application($this->config);
  192. $detail = $data['detail'];
  193. $items = json_decode($detail, true);
  194. foreach ($items as $item) {
  195. $mtGoodsSpu = $item['app_spu_code'];
  196. // access_token 是商家授权后,获取到的
  197. $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  198. $mgGoodsDetail = $app->retail->getDetail($params);
  199. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  200. if (isset($mgGoodsDetail['error'])) {
  201. Yii::error('get retail fail:' . json_encode($params));
  202. } else {
  203. $picArr[$mtGoodsSpu] = $mgGoodsDetail['data']['picture'];
  204. }
  205. }
  206. Yii::info('picture: ' . json_encode($picArr)); // 订单商品图片
  207. return Json::encode(['data' => 'ok']);
  208. }
  209. public function actionCancelOrder()
  210. {
  211. $post = Yii::$app->request->post();
  212. if (empty($post)) {
  213. return Json::encode(['data' => 'ok']);
  214. }
  215. Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
  216. return Json::encode(['data' => 'ok']);
  217. }
  218. public function demo()
  219. {
  220. $app = new Application($this->config);
  221. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  222. $orderDetail = $app->order->getOrderDetail($params);
  223. print_r(json_decode($orderDetail, true));
  224. // 商品类、活动类接口支持异步队列
  225. $app->goods->async()->create([]);
  226. }
  227. }