| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- <?php
- namespace ghs\controllers;
- use biz\shop\classes\ShopClass;
- use bizGhs\express\classes\ShansAuthTokenClass;
- use bizGhs\order\classes\OrderClass;
- use common\components\delivery\services\adapter\HuolalaAdapter;
- use common\components\util;
- use common\components\delivery\services\DispatchService;
- use Yii;
- /**
- * 聚合配送(自配 + 聚合动力)
- * Class DeliveryController
- * @package ghs\controllers
- */
- class DeliveryController extends BaseController
- {
- public $guestAccess = ['callback', 'shansong-auth-callback', 'huolala-auth-callback'];
- // ------------------ 授权(账号绑定) ---------------------
- //授权
- public function actionMerchantAuth()
- {
- //闪送授权(商户授权:授权后能为商户下所有门店发单)
- $auth = new \common\components\delivery\platform\shansong\Auth();
- $redirectUrl = 'https://api.shop.hzghd.com/delivery/shansong-auth-callback';
- $authUrl = $auth->generateMerchantAuthUrl($this->mainId, $redirectUrl);
- // 重定向用户
- header('Location: ' . $authUrl);
- }
- public function actionHuolalaAuth()
- {
- $huoLalaAuth = new \common\components\delivery\platform\huolala\Auth();
- $mainId = $this->mainId;
- $redirectUrl = 'https://api.shop.hzghd.com/delivery/huolala-auth-callback' . '?mainId=' . $mainId;
- $authUrl = $huoLalaAuth->generateAuthUrl($redirectUrl);
- header('Location: ' . $authUrl);
- }
- public function actionFengniaoAuth()
- {
- $fengnaioAuth = new \common\components\delivery\platform\fengniao\Auth();
- $mainId = $this->mainId;
- $redirectUrl = 'https://api.shop.hzghd.com/delivery/fengniao-auth-callback' . '?mainId=' . $mainId;
- $authUrl = $fengnaioAuth->generateAuthUrl($redirectUrl);
- header('Location: ' . $authUrl);
- }
- public function actionDadaAuth()
- {
- $dadaAuth = new \common\components\delivery\platform\dada\Auth();
- $ticket = $dadaAuth->getTicket();
- $mainId = $this->mainId;
- $redirectUrl = 'https://api.shop.hzghd.com/delivery/dada-auth-callback' . '?mainId=' . $mainId;
- $shopNumber = '40d8391f9b05477b';
- $state = 'huidiao_biaoshi';
- $authUrl = $dadaAuth->generateAuthUrl($ticket, $state, $shopNumber);
- header('Location: ' . $authUrl);
- }
- public function actionHuolalaVehicle()
- {
- $mainId = $this->mainId;
- $authPlatforms = ShansAuthTokenClass::getByCondition(['user_id'=>$mainId, 'platform'=>'huolala']);
- $huolalaAdapter = new HuolalaAdapter($authPlatforms['access_token']);
- $cities = $huolalaAdapter->getCityVehicleList(1006);
- return $this->asJson($cities);
- }
- // 查询开通城市
- public function actionOpenCitiesLists()
- {
- $platform = Yii::$app->request->get('platform');
- $ds = new DispatchService($this->mainId);
- $cities = $ds->openCitiesLists($platform);
- if ($platform == 'shansong') {
- $newCitiesArr = [];
- $citiesArr = $cities['data'];
- foreach($citiesArr as $block) {
- foreach ($block['cities'] as $city) {
- $newCitiesArr[$city['name']] = $city;
- }
- }
- // 把newCitiesArr保存到 platform/huolala/cities.php 文件中
- $citiesFile = Yii::getAlias('@common/components/delivery/platform/shansong/cities.php');
- // 如果文件不存在,则创建文件
- if (!file_exists($citiesFile)) {
- file_put_contents($citiesFile, '<?php return []; ?>');
- }
- // 文件内容要包含命名空间
- $content = '<?php return ' . var_export($newCitiesArr, true) . '; ?>';
- file_put_contents($citiesFile, $content);
- return $newCitiesArr;
- } else if ($platform == 'huolala') {
- $citiesArr = $cities['data']['city_list'];
- $newCitiesArr = [
- 'expire' => $cities['data']['expire'],
- ];
- foreach($citiesArr as $city) {
- $newCitiesArr[$city['name']] = [
- 'city_id' => $city['city_id'],
- 'city_name' => $city['name'],
- ];
- }
- // 把newCitiesArr保存到 platform/huolala/cities.php 文件中
- $citiesFile = Yii::getAlias('@common/components/delivery/platform/huolala/cities.php');
- // 如果文件不存在,则创建文件
- if (!file_exists($citiesFile)) {
- file_put_contents($citiesFile, '<?php namespace common\components\delivery\platform\huolala; return []; ?>');
- }
- // 文件内容要包含命名空间
- $content = '<?php namespace common\components\delivery\platform\huolala; return ' . var_export($newCitiesArr, true) . '; ?>';
- file_put_contents($citiesFile, $content);
- return $newCitiesArr;
- } else {
- return $cities;
- }
- return $cities;
- }
- public function actionAddress()
- {
- $post = Yii::$app->request->post();
- $adminId = $this->adminId;
- util::checkRepeatCommit($adminId, 4);
- $orderId = intval($post['orderId']);
- $order = OrderClass::getById($orderId, false, 'fullAddress');
- $shop = ShopClass::getById($this->shopId, false, 'fullAddress');
- $ret = [
- "send_address" => $shop['fullAddress'],
- "receive_address" => $order['fullAddress']
- ];
- util::success($ret, "success");
- }
- // 获取多个平台报价
- public function actionAllDeliveryQuotes()
- {
- $post = Yii::$app->request->post();
- $adminId = $this->adminId;
- util::checkRepeatCommit($adminId, 4);
- $orderId = intval($post['orderId']);
- $order = OrderClass::getById($orderId);
- $shop = ShopClass::getById($this->shopId);
- $ds = new DispatchService($this->mainId);
- $platforms = $ds->getBestPlatformByPrice($order, $shop);
- $deliveryList = [];
- foreach ($platforms['quotes'] as $item) {
- switch ($item['platform']) {
- case 'shansong':
- $deliveryList[] = [
- 'name' => '闪送',
- 'en_name' => 'shansong',
- 'price' => $item['total_amount'],
- 'distance' => $item['total_distance'],
- 'type' => ''
- ];
- break;
- case 'fengniao':
- // 循环遍历 goods_infos 数组,提取有效项的信息
- $validGoods = [];
- if (!empty($item['goods_infos']) && is_array($item['goods_infos'])) {
- foreach ($item['goods_infos'] as $good) {
- if (!empty($good['is_valid']) && $good['is_valid'] == 1) {
- $validGoods[] = [
- 'actual_delivery_amount_cent' => $good['actual_delivery_amount_cent'] ?? 0,
- 'service_goods_id' => $good['service_goods_id'] ?? ''
- ];
- }
- }
- }
- $deliveryList[] = [
- 'name' => '蜂鸟',
- 'en_name' => 'fengniao',
- 'price' => !empty($validGoods) ? $validGoods[0]['actual_delivery_amount_cent'] : 0,
- 'distance' => $item['distance'],
- 'valid_goods' => $validGoods, // 返回所有有效的商品信息
- 'type' => ''
- ];
- break;
- case 'huolala':
- $deliveryList[] = [
- 'name' => '货拉拉',
- 'en_name' => 'huolala',
- 'price' => isset($item['price_list']) ? $item['price_list'][0]['price_conditions'][0]['total_price'] : 0,
- 'distance' => isset($item['price_list']) ? $item['price_list'][0]['distance_info']['distance_total'] : 0,
- 'type' => $item['vehicle_type'],
- 'city_id' => $item['city_id'],
- 'city_info_revision' => $item['city_info_revision'],
- ];
- break;
- }
- }
- $ret['deliveryList'] = $deliveryList;
- util::success($ret, "success");
- }
- // 创建订单(真实下单)
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- $platform = $post['platform'];
- // huolala 需要有以下参数
- // 'city_id' => 下单城市ID (必需),
- // 'city_info_revision' => 城市版本号 (必需) -- 下单城市版本号, 示例:296,从u-city-info接口获取
- // 'order_vehicle_id' => 车型ID (必需),
- // 'vehicle_std' => ['双排座'] 车型附加要求 (可选),
- // 'order_time' => 用车时间戳(秒) (必需),
- $ds = new DispatchService($this->mainId, $platform);
- $order = [];
- $platforms = $ds->createOrder($order);
- }
- // 查询订单(查单)
- // 第三方回调入口
- /**
- * 回调接口
- */
- public function actionCallback()
- {
- $callbackData = Yii::$app->request->post();
- if (empty($callbackData)) {
- $postStr = file_get_contents('php://input');
- $callbackData = json_decode($postStr, true);
- if (empty($callbackData)) {
- util::fail('回调请求的数据为空');
- }
- }
- // 从配置中获取安全token
- // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
- // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
- // 记录回调日志
- Yii::info('聚合配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'delivery');
- }
- /**
- * 闪送授权回调接口
- */
- public function actionShansongAuthCallback()
- {
- $code = Yii::$app->request->get('code');
- $state = Yii::$app->request->get('state');
- $shopId = Yii::$app->request->get('shopId');
- $isAllStoreAuth = Yii::$app->request->get('isAllStoreAuth');
- $thirdStoreId = Yii::$app->request->get('thirdStoreId');
- $storeId = Yii::$app->request->get('storeId');
- // 验证state参数(防止CSRF)
- //if ($state != $yourUserId) {
- //throw new \yii\web\BadRequestHttpException('Invalid state parameter');
- //}
- // 获取AccessToken
- $auth = new \common\components\delivery\platform\shansong\Auth();
- $result = $auth->getAccessToken($code);
- if (!$result['success']) {
- throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
- }
- // 保存授权信息到数据库
- $data = [
- 'user_id' => $state,
- 'shop_id' => $shopId,
- 'access_token' => $result['access_token'],
- 'refresh_token' => $result['refresh_token'],
- 'expires_at' => time() + $result['expires_in'],
- 'auth_type' => $isAllStoreAuth ? 'all_store' : 'single_store',
- 'platform_type' => 'shansong'
- ];
- if (!$isAllStoreAuth) {
- $data['third_store_id'] = $thirdStoreId;
- $data['shans_store_id'] = $storeId;
- }
- ShansAuthTokenClass::add($data);
- if (false) {
- throw new \yii\web\HttpException(500, '保存授权信息失败');
- }
- return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
- }
- /**
- * 货拉拉授权回调接口
- */
- public function actionHuolalaAuthCallback()
- {
- $get = Yii::$app->request->get();
- $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
- Yii::info($getData);
- $callbackData = Yii::$app->request->post();
- if (empty($callbackData)) {
- $postStr = file_get_contents('php://input');
- $callbackData = json_decode($postStr, true);
- if (empty($callbackData)) {
- Yii::info('回调请求的数据为空');
- }
- }
- Yii::info('货拉拉授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'delivery');
- $code = $get['code'];
- $mainId = $get['mainId'];
- // 获取AccessToken
- $auth = new \common\components\delivery\platform\huolala\Auth();
- $result = $auth->getAccessToken($code);
- if (!$result['success']) {
- throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
- }
- // 保存授权信息到数据库
- $data = [
- 'user_id' => $mainId,
- 'shop_id' => $result['auth_mobile'],
- 'access_token' => $result['access_token'],
- 'refresh_token' => $result['refresh_token'],
- 'expires_at' => strtotime($result['auth_end_time']),
- 'auth_type' => 'all_store',
- 'platform' => 'huolala'
- ];
- ShansAuthTokenClass::add($data);
- return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
- }
- /**
- * 蜂鸟授权回调接口
- */
- public function actionFengniaoAuthCallback()
- {
- $get = Yii::$app->request->get();
- $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
- Yii::info($getData);
- $callbackData = Yii::$app->request->post();
- if (empty($callbackData)) {
- $postStr = file_get_contents('php://input');
- $callbackData = json_decode($postStr, true);
- if (empty($callbackData)) {
- Yii::info('回调请求的数据为空');
- }
- }
- Yii::info('蜂鸟授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'fengniao');
- $mainId = $get['mainId'];
- $code = $get['code'];
- $merchantId = $get['merchant_id'];
- $scope = $get['scope'];
- $state = $get['state'];
- // 获取AccessToken
- $auth = new \common\components\delivery\platform\fengniao\Auth();
- $result = $auth->getAccessToken($code, $merchantId);
- if (!$result['success']) {
- throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
- }
- $result = $result['data'];
- // 保存授权信息到数据库
- $data = [
- 'user_id' => $mainId,
- 'shop_id' => '',
- 'access_token' => $result['access_token'],
- 'refresh_token' => $result['refresh_token'],
- 'expires_at' => $result['expire_in'],
- 'auth_type' => 'all_store',
- 'platform' => 'fengniao'
- ];
- ShansAuthTokenClass::add($data);
- return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
- }
- /**
- * 达达授权回调接口
- */
- public function actionDadaAuthCallback()
- {
- $get = Yii::$app->request->get();
- $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
- Yii::info($getData);
- $callbackData = Yii::$app->request->post();
- if (empty($callbackData)) {
- $postStr = file_get_contents('php://input');
- $callbackData = json_decode($postStr, true);
- if (empty($callbackData)) {
- Yii::info('回调请求的数据为空');
- }
- }
- Yii::info('达达授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
- }
- }
|