MtController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. public function actionNewOrder()
  21. {
  22. $post = Yii::$app->request->post();
  23. if(empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  24. return Json::encode(['data' => 'ok']);
  25. }
  26. // 美团传入的订单数据,中文是 URL_encode 的,执行下解密
  27. foreach($post as $k => $val) {
  28. $post[$k] = urldecode($val);
  29. }
  30. $data = arrayUtil::humpUnderlineConversion($post, 1);
  31. Yii::warning('newOrder: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
  32. $orderId = $data['orderId'];
  33. $mtOrder = MtOrderClass::getByCondition(['orderId' => $orderId]);
  34. if (!empty($mtOrder)) {
  35. Yii::warning('订单号已存在:' . $orderId);
  36. return Json::encode(['data' => 'ok']);
  37. }
  38. // TODO 通过 app_poi_code 查询批发商的美团商家帐号,获取 access_token 与 refresh_token
  39. // TODO 做到上一步,要实现好批发商把他的美团商家账号授权给花掌柜
  40. $mtOrder = MtOrderClass::add($data);
  41. $picArr = [];
  42. $app = new Application($this->config);
  43. $detail = $data['detail'];
  44. $items = json_decode($detail, true);
  45. foreach($items as $item){
  46. $appSpuCode = $item['app_spu_code'];
  47. // access_token 是商家授权后,获取到的
  48. $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $appSpuCode, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  49. $retailDetail = $app->retail->getDetail($params);
  50. $retailDetail = json_decode($retailDetail, true);
  51. if (isset($retailDetail['error'])) {
  52. Yii::error('get retail fail:' . json_encode($params));
  53. } else {
  54. $picArr[$appSpuCode] = $retailDetail['data']['picture'];
  55. }
  56. }
  57. Yii::info('picture: ' . json_encode($picArr)); // 订单商品图片
  58. return Json::encode(['data' => 'ok']);
  59. }
  60. public function actionCancelOrder()
  61. {
  62. $post = Yii::$app->request->post();
  63. if(!empty($post)) {
  64. Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
  65. }
  66. return Json::encode(['data' => 'ok']);
  67. }
  68. public function demo() {
  69. $app = new Application($this->config);
  70. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  71. $orderDetail = $app->order->getOrderDetail($params);
  72. print_r(json_decode($orderDetail, true));
  73. // 商品类、活动类接口支持异步队列
  74. $app->goods->async()->create([]);
  75. }
  76. }