| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace hd\controllers;
- use bizHd\meituan\classes\MtOrderClass;
- use common\components\arrayUtil;
- use Yii;
- use Shishaoqi\MeituanFlashPurchase\Application;
- use yii\helpers\Json;
- class MtController extends PublicController
- {
- public $guestAccess = [];
- private $config = [
- 'app_id' => '119094',
- 'app_secret' => 'b7e86d768391180f805621b40ec31e33',
- 'request_url' => '', // 默认 `https://waimaiopen.meituan.com/api/v1/`
- ];
- public function actionApi()
- {
- echo '开发中...';
- }
- public function actionNewOrder()
- {
- $post = Yii::$app->request->post();
- if(empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
- return Json::encode(['data' => 'ok']);
- }
- // 美团传入的订单数据,中文是 URL_encode 的,执行下解密
- foreach($post as $k => $val) {
- $post[$k] = urldecode($val);
- }
- $data = arrayUtil::humpUnderlineConversion($post, 1);
- Yii::warning('newOrder: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
- $orderId = $data['orderId'];
- $mtOrder = MtOrderClass::getByCondition(['orderId' => $orderId]);
- if (!empty($mtOrder)) {
- Yii::warning('订单号已存在:' . $orderId);
- return Json::encode(['data' => 'ok']);
- }
- // TODO 通过 app_poi_code 查询批发商的美团商家帐号,获取 access_token 与 refresh_token
- // TODO 做到上一步,要实现好批发商把他的美团商家账号授权给花掌柜
- $mtOrder = MtOrderClass::add($data);
- $picArr = [];
- $app = new Application($this->config);
- $detail = $data['detail'];
- $items = json_decode($detail, true);
- foreach($items as $item){
- $appSpuCode = $item['app_spu_code'];
- // access_token 是商家授权后,获取到的
- $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $appSpuCode, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
- $retailDetail = $app->retail->getDetail($params);
- $retailDetail = json_decode($retailDetail, true);
- if (isset($retailDetail['error'])) {
- Yii::error('get retail fail:' . json_encode($params));
- } else {
- $picArr[$appSpuCode] = $retailDetail['data']['picture'];
- }
- }
- Yii::info('picture: ' . json_encode($picArr)); // 订单商品图片
- return Json::encode(['data' => 'ok']);
- }
- public function actionCancelOrder()
- {
- $post = Yii::$app->request->post();
- if(!empty($post)) {
- Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
- }
- return Json::encode(['data' => 'ok']);
- }
- public function demo() {
- $app = new Application($this->config);
- $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
- $orderDetail = $app->order->getOrderDetail($params);
- print_r(json_decode($orderDetail, true));
- // 商品类、活动类接口支持异步队列
- $app->goods->async()->create([]);
- }
- }
|