MtController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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('confirmOrderData: ' . json_encode($post));
  53. $str = '{"total":"20.5","delivery_time":"0","utime":"1657691753","wm_poi_name":"t_ZrECxUoNHg","taxpayer_id":"","detail":"%5B%7B%22actual_price%22%3A10.0%2C%22app_food_code%22%3A%22mtcode_1536299752939962428%22%2C%22app_spu_code%22%3A%22mtcode_1536299752939962428%22%2C%22box_num%22%3A0.0%2C%22box_price%22%3A0.0%2C%22detail_extra%22%3A%7B%22cate%22%3A%22%E8%8A%B1%E5%8D%89%E5%9B%AD%E8%89%BA%22%7D%2C%22food_discount%22%3A1.0%2C%22food_name%22%3A%22%E5%AE%B6%E5%B1%85%E9%B2%9C%E8%8A%B1-%E7%B2%89%E8%89%B2%E6%BB%A1%E5%A4%A9%E6%98%9F%E7%AE%80%E5%8D%95%E5%8C%85%E8%A3%85%22%2C%22item_id%22%3A4963320624333%2C%22mt_sku_id%22%3A3308253830%2C%22mt_spu_id%22%3A13964107149%2C%22mt_tag_id%22%3A10982511689%2C%22original_price%22%3A10.0%2C%22price%22%3A10.0%2C%22quantity%22%3A1%2C%22sku_id%22%3A%22%22%2C%22unit%22%3A%22%E4%BB%BD%22%2C%22upc%22%3A%22%22%2C%22weight%22%3A100%2C%22weight_for_unit%22%3A%22100.00%22%2C%22weight_unit%22%3A%22%E5%85%8B%28g%29%22%7D%2C%7B%22actual_price%22%3A10.0%2C%22app_food_code%22%3A%22mtcode_1536168818408202328%22%2C%22app_spu_code%22%3A%22mtcode_1536168818408202328%22%2C%22box_num%22%3A0.0%2C%22box_price%22%3A0.0%2C%22detail_extra%22%3A%7B%22cate%22%3A%22%E8%8A%B1%E5%8D%89%E5%9B%AD%E8%89%BA%22%7D%2C%22food_discount%22%3A1.0%2C%22food_name%22%3A%22%E3%80%90%E7%BA%AF%E7%9C%9F%E7%88%B1%E6%81%8B%E3%80%91+19%E6%9E%9D%E9%83%81%E9%87%91%E9%A6%99%E9%B2%9C%E8%8A%B1%E7%A4%BC%E7%A4%BC%E5%93%81%22%2C%22item_id%22%3A4963320624397%2C%22mt_sku_id%22%3A3298864862%2C%22mt_spu_id%22%3A14024093059%2C%22mt_tag_id%22%3A10982511689%2C%22original_price%22%3A10.0%2C%22price%22%3A10.0%2C%22quantity%22%3A1%2C%22sku_id%22%3A%22%22%2C%22unit%22%3A%22%E4%BB%BD%22%2C%22upc%22%3A%22%22%2C%22weight%22%3A500%2C%22weight_for_unit%22%3A%22500.00%22%2C%22weight_unit%22%3A%22%E5%85%8B%28g%29%22%7D%5D","poi_receive_detail":"%7B%22actOrderChargeByMt%22%3A%5B%7B%22comment%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeDesc%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeId%22%3A10019%2C%22moneyCent%22%3A0%7D%5D%2C%22actOrderChargeByPoi%22%3A%5B%7B%22comment%22%3A%22%E5%87%8F%E9%85%8D%E9%80%81%E8%B4%B920.0%E5%85%83%22%2C%22feeTypeDesc%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeId%22%3A10019%2C%22moneyCent%22%3A1200%7D%5D%2C%22foodShareFeeChargeByPoi%22%3A80%2C%22logisticsFee%22%3A1200%2C%22onlinePayment%22%3A2050%2C%22reconciliationExtras%22%3A%22%7B%5C%22performanceServiceFee%5C%22%3A5.0%2C%5C%22chargeMode%5C%22%3A2%7D%22%2C%22wmPoiReceiveCent%22%3A270%7D","caution":"","original_price":"32.5","recipient_name":"%E7%9F%B3%28%E5%85%88%E7%94%9F%29","order_id":"27052664056314941","timestamp":"1657691754","city_id":"999999","wm_poi_phone":"4009208801","backup_recipient_phone":"%5B%5D","pay_type":"2","wm_poi_id":"2705266","longitude":"95.369279","invMakeType":"0","estimate_arrival_time":"1657694297","avg_send_time":"2582.0","logistics_code":"1001","scan_deliver_flag":"false","order_tag_list":"%5B%5D","order_phone_number":"15280215347","poi_receive_detail_yuan":"%7B%22actOrderChargeByMt%22%3A%5B%7B%22comment%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeDesc%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeId%22%3A10019%2C%22money%22%3A%220.00%22%7D%5D%2C%22actOrderChargeByPoi%22%3A%5B%7B%22comment%22%3A%22%E5%87%8F%E9%85%8D%E9%80%81%E8%B4%B920.0%E5%85%83%22%2C%22feeTypeDesc%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeId%22%3A10019%2C%22money%22%3A%2212.00%22%7D%5D%2C%22foodShareFeeChargeByPoi%22%3A%220.80%22%2C%22logisticsFee%22%3A%2212.00%22%2C%22onlinePayment%22%3A%2220.50%22%2C%22poiReceive%22%3A%222.70%22%2C%22reconciliationExtras%22%3A%22%7B%5C%22performanceServiceFee%5C%22%3A%5C%225.00%5C%22%2C%5C%22chargeMode%5C%22%3A2%7D%22%7D","is_pre_order":"false","invoice_title":"","status":"4","app_poi_code":"36524","shipper_phone":"","is_third_shipping":"0","is_pre_sale_order":"false","ctime":"1657691715","shipping_fee":"12.0","has_invoiced":"0","extras":"%5B%7B%22mt_charge%22%3A0.0%2C%22poi_charge%22%3A12.0%2C%22reduce_fee%22%3A12.0%2C%22remark%22%3A%22%E5%87%8F%E9%85%8D%E9%80%81%E8%B4%B920.0%E5%85%83%22%2C%22type%22%3A25%7D%5D","package_bag_money_yuan":"0.50","incmp_modules":"%5B%5D","wm_poi_address":"%E5%8D%97%E6%9E%81%E6%B4%B204%E5%8F%B7%E7%AB%99","recipient_phone":"15280215347","wm_order_id_view":"27052664056314941","incmp_code":"0","app_id":"119094","latitude":"29.774499","recipient_address":"%E8%89%B2%E9%87%91%E6%8B%89","sig":"014b03d8f643d2ef5362d571a73603f6"}';
  54. $post = json_decode($str, true);
  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. //$imgString = 'https://awp-assets.meituan.net/set/online_b_open_developer_react/images/biz-retail-bb948ac2b3.png,https://awp-assets.meituan.net/set/online_b_open_developer_react/images/biz-medicine-b6d2fdf744.png';
  113. $goodsName = $mtGoods['food_name'] ?? '';
  114. $goodsPrice = $mtGoods['price'] ?? 0;
  115. $num = $mtGoods['quantity'] ?? 0;
  116. $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  117. if (empty($hasGoods)) {
  118. $imgArr = explode(',', $imgString);
  119. $shopImg = [];
  120. foreach ($imgArr as $imgUrl) {
  121. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  122. $shopImg[] = $shortUrl;
  123. }
  124. $cover = $shopImg[0];
  125. $goodsData = [
  126. 'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
  127. 'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => $cover, 'unitGoodsPrice' => $goodsPrice, 'num' => $num,
  128. ];
  129. print_r($goodsData);
  130. $goods = GoodsClass::orderCreateGoods($goodsData);
  131. Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
  132. } else {
  133. $id = $hasGoods->id ?? 0;
  134. $unitType = 0;
  135. $property = dict::getDict('property', 'goods');
  136. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  137. $imgMd5 = Yii::$app->redis->executeCommand('GET', ['mt_goods_spu_' . $mtGoodsSpu]);
  138. if (empty($imgMd5) || $imgMd5 != md5($imgString)) {
  139. //图片需要更新
  140. $imgArr = explode(',', $imgString);
  141. $shopImg = [];
  142. foreach ($imgArr as $imgUrl) {
  143. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  144. $shopImg[] = $shortUrl;
  145. }
  146. $hasGoods->shopImg = $shopImg;
  147. }
  148. $hasGoods->name = $goodsName;
  149. $hasGoods->price = $goodsPrice;
  150. $hasGoods->save();
  151. }
  152. $productList = array_merge($goods, $productList);
  153. }
  154. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  155. if (empty($poi_receive_detail_yuan)) {
  156. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$orderId}", '15280215347');
  157. util::stop();
  158. }
  159. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  160. if (is_array($poi_receive_detail_yuan_data) == false) {
  161. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$orderId}", '15280215347');
  162. util::stop();
  163. }
  164. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  165. if ($onlinePayment == 0) {
  166. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$orderId}", '15280215347');
  167. util::stop();
  168. }
  169. $modifyPrice = $onlinePayment;
  170. //技术服务费
  171. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  172. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  173. //履约服务费
  174. $performanceServiceFee = 0;
  175. if (!empty($reconciliationExtras)) {
  176. $extArr = json_decode($reconciliationExtras, true);
  177. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  178. }
  179. //运费
  180. $sendCost = $post['shipping_fee'] ?? 0;
  181. //打包费
  182. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  183. //服务费和手续费
  184. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  185. $orderData = [
  186. 'product' => $productList,
  187. 'fromType' => dict::getDict('fromType', 'mt'),
  188. 'labourCost' => $labourCost,
  189. 'sendCost' => $sendCost,
  190. 'shopId' => $shopId,
  191. 'mainId' => $mainId,
  192. 'sjId' => $sjId,
  193. 'modifyPrice' => $modifyPrice,
  194. 'serviceFee' => $serviceFee,
  195. ];
  196. $hasPay = dict::getDict('hasPay', 'payed');
  197. OrderService::createFinishOrder($orderData, $custom, $hasPay);
  198. $transaction->commit();
  199. return Json::encode(['data' => 'ok']);
  200. } catch (\Exception $e) {
  201. $transaction->rollBack();
  202. $msg = $e->getMessage();
  203. noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$orderId}", '15280215347');
  204. }
  205. }
  206. public function actionNewOrder()
  207. {
  208. $post = Yii::$app->request->post();
  209. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  210. return Json::encode(['data' => 'ok']);
  211. }
  212. return Json::encode(['data' => 'ok']);
  213. Yii::warning('newOrder2: ' . json_encode($post));
  214. // 美团传入的订单数据,中文是 URL_encode 的,执行下解密
  215. foreach ($post as $k => $val) {
  216. $post[$k] = urldecode($val);
  217. }
  218. $data = arrayUtil::humpUnderlineConversion($post, 1);
  219. Yii::warning('newOrder: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
  220. $orderId = $data['orderId'];
  221. $mtOrder = MtOrderClass::getByCondition(['orderId' => $orderId]);
  222. if (!empty($mtOrder)) {
  223. Yii::warning('订单号已存在:' . $orderId);
  224. return Json::encode(['data' => 'ok']);
  225. }
  226. // TODO 通过 app_poi_code 查询批发商的美团商家帐号,获取 access_token 与 refresh_token
  227. // TODO 做到上一步,要实现好批发商把他的美团商家账号授权给花掌柜
  228. $mtOrder = MtOrderClass::add($data);
  229. $picArr = [];
  230. $app = new Application($this->config);
  231. $detail = $data['detail'];
  232. $items = json_decode($detail, true);
  233. foreach ($items as $item) {
  234. $mtGoodsSpu = $item['app_spu_code'];
  235. // access_token 是商家授权后,获取到的
  236. $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  237. $mgGoodsDetail = $app->retail->getDetail($params);
  238. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  239. if (isset($mgGoodsDetail['error'])) {
  240. Yii::error('get retail fail:' . json_encode($params));
  241. } else {
  242. $picArr[$mtGoodsSpu] = $mgGoodsDetail['data']['picture'];
  243. }
  244. }
  245. Yii::info('picture: ' . json_encode($picArr)); // 订单商品图片
  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. }