MtController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. $sendCost = $data['shipping_fee'] ?? 0;
  151. $modifyPrice = $data[''];
  152. $orderData = [
  153. 'product' => $productList,
  154. 'fromType' => dict::getDict('fromType', 'mt'),
  155. ];
  156. $hasPay = dict::getDict('hasPay', 'payed');
  157. OrderService::createFinishOrder($orderData, $custom, $hasPay);
  158. $transaction->commit();
  159. return Json::encode(['data' => 'ok']);
  160. } catch (\Exception $e) {
  161. $transaction->rollBack();
  162. $msg = $e->getMessage();
  163. noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$orderId}", '15280215347');
  164. }
  165. }
  166. public function actionNewOrder()
  167. {
  168. $post = Yii::$app->request->post();
  169. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  170. return Json::encode(['data' => 'ok']);
  171. }
  172. return Json::encode(['data' => 'ok']);
  173. Yii::warning('newOrder2: ' . json_encode($post));
  174. // 美团传入的订单数据,中文是 URL_encode 的,执行下解密
  175. foreach ($post as $k => $val) {
  176. $post[$k] = urldecode($val);
  177. }
  178. $data = arrayUtil::humpUnderlineConversion($post, 1);
  179. Yii::warning('newOrder: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
  180. $orderId = $data['orderId'];
  181. $mtOrder = MtOrderClass::getByCondition(['orderId' => $orderId]);
  182. if (!empty($mtOrder)) {
  183. Yii::warning('订单号已存在:' . $orderId);
  184. return Json::encode(['data' => 'ok']);
  185. }
  186. // TODO 通过 app_poi_code 查询批发商的美团商家帐号,获取 access_token 与 refresh_token
  187. // TODO 做到上一步,要实现好批发商把他的美团商家账号授权给花掌柜
  188. $mtOrder = MtOrderClass::add($data);
  189. $picArr = [];
  190. $app = new Application($this->config);
  191. $detail = $data['detail'];
  192. $items = json_decode($detail, true);
  193. foreach ($items as $item) {
  194. $mtGoodsSpu = $item['app_spu_code'];
  195. // access_token 是商家授权后,获取到的
  196. $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  197. $mgGoodsDetail = $app->retail->getDetail($params);
  198. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  199. if (isset($mgGoodsDetail['error'])) {
  200. Yii::error('get retail fail:' . json_encode($params));
  201. } else {
  202. $picArr[$mtGoodsSpu] = $mgGoodsDetail['data']['picture'];
  203. }
  204. }
  205. Yii::info('picture: ' . json_encode($picArr)); // 订单商品图片
  206. return Json::encode(['data' => 'ok']);
  207. }
  208. public function actionCancelOrder()
  209. {
  210. $post = Yii::$app->request->post();
  211. if (empty($post)) {
  212. return Json::encode(['data' => 'ok']);
  213. }
  214. Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
  215. return Json::encode(['data' => 'ok']);
  216. }
  217. public function demo()
  218. {
  219. $app = new Application($this->config);
  220. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  221. $orderDetail = $app->order->getOrderDetail($params);
  222. print_r(json_decode($orderDetail, true));
  223. // 商品类、活动类接口支持异步队列
  224. $app->goods->async()->create([]);
  225. }
  226. }