Browse Source

实现 app-hd(花掌柜)的跑腿授权等相关功能 -- 从 app-ghs 搬过来

shizhongqi 8 months ago
parent
commit
ffafe53537

+ 8 - 76
app-ghs/controllers/DeliveryController.php

@@ -63,21 +63,24 @@ class DeliveryController extends BaseController
     public function actionGetAuthUrl()
     {
         $platform = Yii::$app->request->get('platformId');
+
+        $Auth = new \common\components\delivery\services\AuthService();
+        $mainId = $this->mainId;
         switch($platform){
             case 'shansong':
-                return $this->actionShansongAuth();
+                $Auth->shansongAuth($mainId);
                 break;
             case 'huolala':
-                return $this->actionHuolalaAuth();
+                $Auth->huolalaAuth($mainId);
                 break;
             case 'fengniao':
-                return $this->actionFengniaoAuth();
+                $Auth->fengniaoAuth($mainId);
                 break;
             case 'dada':
-                return $this->actionDadaAuth();
+                $Auth->dadaAuth($mainId);
                 break;
             case 'shunfeng':
-                return $this->actionShunfengAuth();
+                $Auth->shunfengAuth($mainId);
                 break;
             case 'didi':
                 break;
@@ -87,77 +90,6 @@ class DeliveryController extends BaseController
         }
     }
 
-    // ------------------ 授权(账号绑定) ---------------------
-    //授权
-    public function actionShansongAuth()
-    {
-        //闪送授权(商户授权:授权后能为商户下所有门店发单)
-        $auth = new \common\components\delivery\platform\shansong\Auth();
-        if(getenv('YII_ENV') == 'production') {
-            //$apiHost = Yii::$app->params['ghsHost'];
-            // TODO 暂时修改为测试环境
-            $apiHost = 'https://api.shop.hzghd.com';
-        } else {
-            $apiHost = Yii::$app->params['ghsHost'];
-        }
-        $redirectUrl = $apiHost . '/delivery/shansong-auth-callback';
-        $authUrl = $auth->generateMerchantAuthUrl($this->mainId, $redirectUrl);
-
-        // 重定向用户
-        //header('Location: ' . $authUrl);
-        util::success(['url'=>$authUrl]);
-    }
-
-    public function actionHuolalaAuth()
-    {
-        $huoLalaAuth = new \common\components\delivery\platform\huolala\Auth();
-        $mainId = $this->mainId;
-        // $apiHost = Yii::$app->params['ghsHost'];
-        // TODO 暂时修改为测试环境
-        $apiHost = 'https://api.shop.hzghd.com';
-        $redirectUrl = $apiHost . '/delivery/huolala-auth-callback' . '?mainId=' . $mainId;
-        $authUrl = $huoLalaAuth->generateAuthUrl($redirectUrl);
-
-        //header('Location: ' . $authUrl);
-        util::success(['url'=>$authUrl]);
-    }
-
-    public function actionFengniaoAuth()
-    {
-        $fengnaioAuth = new \common\components\delivery\platform\fengniao\Auth();
-        $mainId = $this->mainId;
-        // $apiHost = Yii::$app->params['ghsHost'];
-        // TODO 暂时修改为测试环境
-        $apiHost = 'https://api.shop.hzghd.com';
-        $redirectUrl = $apiHost . '/delivery/fengniao-auth-callback' . '?main_id=' . $mainId;
-        $authUrl = $fengnaioAuth->generateAuthUrl($redirectUrl);
-
-        util::success(['url'=>$authUrl]);
-    }
-
-    public function actionDadaAuth()
-    {
-        $dadaAuth = new \common\components\delivery\platform\dada\Auth();
-        $ticket = $dadaAuth->getTicket();
-
-        $mainId = $this->mainId;
-        $redirectUrl = Yii::$app->params['ghsHost'] . '/delivery/dada-auth-callback' . '?mainId=' . $mainId;
-        $shopNumber = '40d8391f9b05477b';
-        $state = 'huidiao_biaoshi';
-        $authUrl = $dadaAuth->generateAuthUrl($ticket, $state, $shopNumber);
-        //header('Location: ' . $authUrl);
-        util::success(['url'=>$authUrl]);
-    }
-
-    public function actionShunfengAuth()
-    {
-        $shunfengAuth = new \common\components\delivery\platform\shunfeng\Auth();
-
-        $mainId = $this->mainId;
-        $authUrl = $shunfengAuth->generateAuthUrl($mainId);
-        util::success(['url'=>$authUrl]);
-    }
-
     public function actionCancelAuth()
     {
         // 有平台有对应的取消授权接口,有的没有。

+ 92 - 1
app-hd/controllers/DeliveryController.php

@@ -3,15 +3,44 @@ namespace hd\controllers;
 
 use biz\ghs\classes\GhsClass;
 use bizGhs\custom\classes\CustomClass;
+use bizGhs\express\classes\DeliveryAuthTokenClass;
 use Yii;
 use biz\shop\classes\ShopClass;
 use common\components\delivery\services\DispatchService;
 use common\components\util;
-use ghs\controllers\BaseController;
 use yii\helpers\ArrayHelper;
 
 class DeliveryController extends BaseController
 {
+    public function actionList()
+    {
+        $platformList = [];
+        $platformLogos = [
+            'data' => ['name'=>'达达', 'logo'=>'📦'],
+            'shunfeng' => ['name'=>'顺丰同城', 'logo'=>'🚚'],
+            'meituan' => ['name'=>'美团', 'logo'=>'🍱'] ,
+            'fengniao' => ['name'=>'蜂鸟', 'logo'=>'🍜'],
+            'huolala' => ['name'=>'货拉拉跑腿', 'logo'=>'🚛'],
+            'shansong' => ['name'=>'闪送', 'logo'=>'⚡'],
+            'didi' => ['name'=>'滴滴', 'logo'=>'🚗'],
+            'uu' => ['name'=>'UU跑腿', 'logo'=>'🛵']
+        ];
+        $mainId = $this->mainId;
+        $allDeliveries = DeliveryAuthTokenClass::getAllByCondition(['mainId'=>$mainId]);
+        foreach($allDeliveries as $d){
+            $item = [
+                'id'=>$d['platform'],
+                'logo'=>$platformLogos[$d['platform']]['logo'],
+                'name'=>$platformLogos[$d['platform']]['name'],
+                'is_authorized' => true,
+                'balance' => $d['balance']/100.0,
+                'access_type' => 'oauth'
+            ];
+            $platformList[] = $item;
+        }
+        util::success(['platformList'=>$platformList]);
+    }
+
     // 获取多个平台报价
     public function actionAllDeliveryQuotes()
     {
@@ -72,4 +101,66 @@ class DeliveryController extends BaseController
         $ret['deliveryList'] = $deliveryList;
         util::success($ret, "success");
     }
+
+    public function actionCancelAuth()
+    {
+        // 有平台有对应的取消授权接口,有的没有。
+        // 有的就对接下,然后再删除数据。没有的直接删除数据就行。
+        $platform = Yii::$app->request->get('platform');
+        $delivery = DeliveryAuthTokenClass::getByCondition(['mainId'=>$this->mainId, 'platform'=>$platform]);
+        if($delivery) {
+            if($platform == 'shansong') {
+                $auth = new \common\components\delivery\platform\shansong\Auth();
+                $result = $auth->cancelAuthorization($delivery['accessToken']);
+                if($result['success']) {
+                    // 授权已取消
+                }else{
+                    if($result['message'] == '商户未授权,请授权后再次尝试'){
+                        Yii::info($this->mainId . '--商户未授权,请授权后再次尝试');
+                    }else{
+                        util::fail('取消授权失败:'.$result['message']);
+                    }
+                }
+            }
+        }else{
+            util::fail('取消授权失败,数据未找到');
+        }
+
+        $ret = DeliveryAuthTokenClass::deleteByCondition(['mainId'=>$this->mainId, 'platform'=>$platform]);
+        if($ret > 0){
+            util::success(['message'=>'取消授权成功']);
+        } else {
+            util::fail('取消授权失败');
+        }
+    }
+
+    public function actionGetAuthUrl()
+    {
+        $platform = Yii::$app->request->get('platformId');
+
+        $Auth = new \common\components\delivery\services\AuthService();
+        $mainId = $this->mainId;
+        switch($platform){
+            case 'shansong':
+                $Auth->shansongAuth($mainId);
+                break;
+            case 'huolala':
+                $Auth->huolalaAuth($mainId);
+                break;
+            case 'fengniao':
+                $Auth->fengniaoAuth($mainId);
+                break;
+            case 'dada':
+                $Auth->dadaAuth($mainId);
+                break;
+            case 'shunfeng':
+                $Auth->shunfengAuth($mainId);
+                break;
+            case 'didi':
+                break;
+            default:
+                util::error(-1, $platform . '不存在');
+                break;
+        }
+    }
 }

+ 68 - 0
app-hd/controllers/DeliveryShopController.php

@@ -0,0 +1,68 @@
+<?php
+namespace hd\controllers;
+
+use bizGhs\express\classes\DeliveryAuthTokenClass;
+use common\components\util;
+use common\components\delivery\services\ShopService;
+use Yii;
+
+/**
+ * 聚合配送 -- 门店相关接口
+ * Class DeliveryController
+ * @package ghs\controllers
+ */
+class DeliveryShopController extends BaseController
+{
+    public $guestAccess = [];
+
+    public function actionBalance()
+    {
+        $get = Yii::$app->request->get();
+        $platform = $get['platform'];
+
+        $ShopService = new ShopService($this->mainId, $platform);
+        $balance = $ShopService->getBalance($platform);
+
+        $deliveryAuthToken = DeliveryAuthTokenClass::getByCondition(['platform'=>$platform, 'mainId'=>$this->mainId], true);
+        if(!$deliveryAuthToken){
+            util::fail('配送平台未授权');
+        }
+        $deliveryAuthToken->balance = $balance;
+        $deliveryAuthToken->save();
+        util::success(['balance'=>$balance]);
+    }
+
+    public function actionStoreList()
+    {
+        $get = Yii::$app->request->get();
+        $platform = $get['platform'];
+
+        $ShopService = new ShopService($this->mainId, $platform);
+        $stores = $ShopService->getMerchantStores($platform);
+        util::success($stores);
+    }
+
+    public function actionBindStore(){
+        $post = Yii::$app->request->post();
+        $platform = $post['platform'];
+        $storeId = $post['shopId'];
+        $ShopService = new ShopService($this->mainId, $platform);
+        $re = $ShopService->bindStore($this->mainId, $platform, $storeId);
+        if(!$re){
+            util::fail('绑定门店失败');
+        }
+        util::success('绑定成功');
+    }
+
+    public function actionUnbindStore(){
+        $post = Yii::$app->request->post();
+        $platform = $post['platform'];
+        $storeId = $post['shopId'];
+        $ShopService = new ShopService($this->mainId, $platform);
+        $re = $ShopService->unbindStore($this->mainId, $platform, $storeId);
+        if(!$re){
+            util::fail('解绑门店失败');
+        }
+        util::success('解绑门店成功');
+    }
+}

+ 35 - 0
common/components/delivery/platform/shunfeng/Shop.php

@@ -52,4 +52,39 @@ class Shop extends Shunfeng
             'message' => $response['error_msg'] ?? '查询余额失败',
         ];
     }
+
+    /**
+     * 获取店铺信息
+     *
+     * 2025/2/17更新,返回参数更新,支持返回物流产品,详见logistic_type_list
+     * 开发者可通过该接⼝获取到店铺的基础信息(非连锁店铺可以额外获得等级信息和补贴信息)
+     *
+     * @param string $shopId 店铺ID
+     * @param int $shopType 店铺ID类型 1:顺丰店铺ID 2:接⼊⽅店铺ID
+     * @return mixed
+     */
+    public function getShopInfo($shopId, $shopType = 1)
+    {
+        $params = [
+            'shop_id' => $shopId,
+            'shop_type' => $shopType,
+            'push_time' => time()
+        ];
+        $response = $this->httpRequest('getshopinfo', $params);
+
+        // 检查API响应
+        if (isset($response['error_code']) && $response['error_code'] == 0) {
+            return [
+                'success' => true,
+                'shop_info' => $response['result']['shop_info'] ?? [],
+                'message' => '获取店铺信息成功',
+            ];
+        }
+
+        return [
+            'success' => false,
+            'shop_info' => [],
+            'message' => $response['error_msg'] ?? '获取店铺信息失败',
+        ];
+    }
 }

+ 73 - 0
common/components/delivery/services/AuthService.php

@@ -0,0 +1,73 @@
+<?php
+namespace common\components\delivery\services;
+
+use common\components\util;
+
+Class AuthService{
+    // ------------------ 授权(账号绑定) ---------------------
+    //授权
+    public function shansongAuth($mainId)
+    {
+        //闪送授权(商户授权:授权后能为商户下所有门店发单)
+        $auth = new \common\components\delivery\platform\shansong\Auth();
+        if(getenv('YII_ENV') == 'production') {
+            //$apiHost = Yii::$app->params['ghsHost'];
+            // TODO 暂时修改为测试环境
+            $apiHost = 'https://api.shop.hzghd.com';
+        } else {
+            $apiHost = Yii::$app->params['ghsHost'];
+        }
+        $redirectUrl = $apiHost . '/delivery/shansong-auth-callback';
+        $authUrl = $auth->generateMerchantAuthUrl($mainId, $redirectUrl);
+
+        // 重定向用户
+        //header('Location: ' . $authUrl);
+        util::success(['url'=>$authUrl]);
+    }
+
+    public function huolalaAuth($mainId)
+    {
+        $huoLalaAuth = new \common\components\delivery\platform\huolala\Auth();
+        // $apiHost = Yii::$app->params['ghsHost'];
+        // TODO 暂时修改为测试环境
+        $apiHost = 'https://api.shop.hzghd.com';
+        $redirectUrl = $apiHost . '/delivery/huolala-auth-callback' . '?mainId=' . $mainId;
+        $authUrl = $huoLalaAuth->generateAuthUrl($redirectUrl);
+
+        //header('Location: ' . $authUrl);
+        util::success(['url'=>$authUrl]);
+    }
+
+    public function fengniaoAuth($mainId)
+    {
+        $fengnaioAuth = new \common\components\delivery\platform\fengniao\Auth();
+        // $apiHost = Yii::$app->params['ghsHost'];
+        // TODO 暂时修改为测试环境
+        $apiHost = 'https://api.shop.hzghd.com';
+        $redirectUrl = $apiHost . '/delivery/fengniao-auth-callback' . '?main_id=' . $mainId;
+        $authUrl = $fengnaioAuth->generateAuthUrl($redirectUrl);
+
+        util::success(['url'=>$authUrl]);
+    }
+
+    public function dadaAuth($mainId)
+    {
+        $dadaAuth = new \common\components\delivery\platform\dada\Auth();
+        $ticket = $dadaAuth->getTicket();
+
+        $redirectUrl = Yii::$app->params['ghsHost'] . '/delivery/dada-auth-callback' . '?mainId=' . $mainId;
+        $shopNumber = '40d8391f9b05477b';
+        $state = 'huidiao_biaoshi';
+        $authUrl = $dadaAuth->generateAuthUrl($ticket, $state, $shopNumber);
+        //header('Location: ' . $authUrl);
+        util::success(['url'=>$authUrl]);
+    }
+
+    public function shunfengAuth($mainId)
+    {
+        $shunfengAuth = new \common\components\delivery\platform\shunfeng\Auth();
+
+        $authUrl = $shunfengAuth->generateAuthUrl($mainId);
+        util::success(['url'=>$authUrl]);
+    }
+}

+ 0 - 1
common/components/delivery/services/DispatchService.php

@@ -1,5 +1,4 @@
 <?php
-
 namespace common\components\delivery\services;
 
 use biz\shop\classes\ShopClass;

+ 0 - 2
common/components/delivery/services/SettlementService.php

@@ -1,2 +0,0 @@
-<?php
-// 运价结算逻辑