MtController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $mtOrder = MtOrderClass::add($data);
  38. $picArr = [];
  39. $app = new Application($this->config);
  40. $detail = $data['detail'];
  41. $items = json_decode($detail, true);
  42. foreach($items as $item){
  43. $appSpuCode = $item['app_spu_code'];
  44. $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $appSpuCode];
  45. $retailDetail = $app->retail->getDetail($params);
  46. $retailDetail = json_decode($retailDetail, true);
  47. if (is_set($retailDetail['error'])) {
  48. Yii::error('get retail fail:' . json_encode($params));
  49. } else {
  50. $picArr[$appSpuCode] = $retailDetail['data']['picture'];
  51. }
  52. }
  53. Yii::info('picture: ' . json_encode($picArr));
  54. return Json::encode(['data' => 'ok']);
  55. }
  56. public function actionCancelOrder()
  57. {
  58. $post = Yii::$app->request->post();
  59. if(!empty($post)) {
  60. Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
  61. }
  62. return Json::encode(['data' => 'ok']);
  63. }
  64. public function demo() {
  65. $app = new Application($this->config);
  66. $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
  67. $orderDetail = $app->order->getOrderDetail($params);
  68. print_r(json_decode($orderDetail, true));
  69. // 商品类、活动类接口支持异步队列
  70. $app->goods->async()->create([]);
  71. }
  72. }