MtController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\meituan\classes\MtOrderClass;
  4. use common\components\arrayUtil;
  5. use Yii;
  6. use Abbotton\MeituanTakeaway\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. $orderId = $data['orderId'];
  32. $mtOrder = MtOrderClass::getByCondition(['orderId' => $orderId]);
  33. if (!empty($mtOrder)) {
  34. Yii::warning('订单号已存在:' . $orderId);
  35. return Json::encode(['data' => 'ok']);
  36. }
  37. // TODO 通过 app_poi_code 查询批发商的美团商家帐号,获取 access_token 与 refresh_token
  38. // TODO 做到上一步,要实现好批发商把他的美团商家账号授权给花掌柜
  39. $mtOrder = MtOrderClass::add($data);
  40. $picArr = [];
  41. $app = new Application($this->config);
  42. $detail = $data['detail'];
  43. $items = json_decode($detail, true);
  44. foreach($items as $item){
  45. $appSpuCode = $item['app_spu_code'];
  46. $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $appSpuCode, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
  47. $retailDetail = $app->retail->getDetail($params);
  48. Yii::info('retailDetail:' . $retailDetail);
  49. $retailDetail = json_decode($retailDetail, true);
  50. if (isset($retailDetail['error'])) {
  51. Yii::error('get retail fail:' . json_encode($params));
  52. } else {
  53. $picArr[$appSpuCode] = $retailDetail['data']['picture'];
  54. }
  55. }
  56. Yii::info('picture: ' . json_encode($picArr));
  57. return Json::encode(['data' => 'ok']);
  58. }
  59. public function actionCancelOrder()
  60. {
  61. $post = Yii::$app->request->post();
  62. if(!empty($post)) {
  63. Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
  64. }
  65. return Json::encode(['data' => 'ok']);
  66. }
  67. public function demo() {
  68. $app = new Application($this->config);
  69. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  70. $orderDetail = $app->order->getOrderDetail($params);
  71. print_r(json_decode($orderDetail, true));
  72. // 商品类、活动类接口支持异步队列
  73. $app->goods->async()->create([]);
  74. }
  75. }