MtController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\meituan\classes\MtOrderClass;
  4. use common\components\arrayUtil;
  5. use Yii;
  6. use Shishaoqi\MeituanFlashPurchase\Application;
  7. use yii\helpers\Json;
  8. class MtController extends PublicController
  9. {
  10. public $guestAccess = [];
  11. private $config = [
  12. 'app_id' => '119094',
  13. 'app_secret' => 'b7e86d768391180f805621b40ec31e33',
  14. 'request_url' => '', // 默认 `https://waimaiopen.meituan.com/api/v1/`
  15. ];
  16. public function actionApi()
  17. {
  18. echo '开发中...';
  19. }
  20. //已确认订单通知
  21. public function actionConfirmOrder()
  22. {
  23. $post = Yii::$app->request->post();
  24. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  25. return Json::encode(['data' => 'ok']);
  26. }
  27. $data = arrayUtil::humpUnderlineConversion($post, 1);
  28. Yii::warning('confirmOrder: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
  29. }
  30. public function actionNewOrder()
  31. {
  32. $post = Yii::$app->request->post();
  33. if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  34. return Json::encode(['data' => 'ok']);
  35. }
  36. // 美团传入的订单数据,中文是 URL_encode 的,执行下解密
  37. foreach ($post as $k => $val) {
  38. $post[$k] = urldecode($val);
  39. }
  40. $data = arrayUtil::humpUnderlineConversion($post, 1);
  41. Yii::warning('newOrder: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
  42. $orderId = $data['orderId'];
  43. $mtOrder = MtOrderClass::getByCondition(['orderId' => $orderId]);
  44. if (!empty($mtOrder)) {
  45. Yii::warning('订单号已存在:' . $orderId);
  46. return Json::encode(['data' => 'ok']);
  47. }
  48. // TODO 通过 app_poi_code 查询批发商的美团商家帐号,获取 access_token 与 refresh_token
  49. // TODO 做到上一步,要实现好批发商把他的美团商家账号授权给花掌柜
  50. $mtOrder = MtOrderClass::add($data);
  51. $picArr = [];
  52. $app = new Application($this->config);
  53. $detail = $data['detail'];
  54. $items = json_decode($detail, true);
  55. foreach ($items as $item) {
  56. $appSpuCode = $item['app_spu_code'];
  57. // access_token 是商家授权后,获取到的
  58. $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $appSpuCode, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  59. $retailDetail = $app->retail->getDetail($params);
  60. $retailDetail = json_decode($retailDetail, true);
  61. if (isset($retailDetail['error'])) {
  62. Yii::error('get retail fail:' . json_encode($params));
  63. } else {
  64. $picArr[$appSpuCode] = $retailDetail['data']['picture'];
  65. }
  66. }
  67. Yii::info('picture: ' . json_encode($picArr)); // 订单商品图片
  68. return Json::encode(['data' => 'ok']);
  69. }
  70. public function actionCancelOrder()
  71. {
  72. $post = Yii::$app->request->post();
  73. if (empty($post)) {
  74. return Json::encode(['data' => 'ok']);
  75. }
  76. Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
  77. return Json::encode(['data' => 'ok']);
  78. }
  79. public function demo()
  80. {
  81. $app = new Application($this->config);
  82. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  83. $orderDetail = $app->order->getOrderDetail($params);
  84. print_r(json_decode($orderDetail, true));
  85. // 商品类、活动类接口支持异步队列
  86. $app->goods->async()->create([]);
  87. }
  88. }