Browse Source

授权回调接口

shizhongqi 9 months ago
parent
commit
84af5f1a18

+ 62 - 0
app-ghs/controllers/DeliveryController.php

@@ -136,4 +136,66 @@ class DeliveryController extends BaseController
         //return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
         return $this->redirect(['success']);
     }
+
+    /**
+     * 授权回调接口
+     */
+    public function actionAuthCallback()
+    {
+        $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');
+
+        $code = $callbackData['code'];
+        $state = $callbackData['state'];
+        $shopId = $callbackData['shopId'];
+        $isAllStoreAuth = $callbackData['isAllStoreAuth'];
+        $thirdStoreId = $callbackData['thirdStoreId'];
+        $storeId = $callbackData['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;
+        $data['shop_id'] = $shopId;
+        $data['access_token'] = $result['access_token'];
+        $data['refresh_token'] = $result['refresh_token'];
+        $data['expires_at'] = time() + $result['expires_in'];
+        $data['auth_type'] = $isAllStoreAuth ? 'all_store' : 'single_store';
+
+        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']);
+        return $this->redirect(['success']);
+    }
 }

+ 4 - 8
common/components/delivery/services/adapter/ShansongAdapter.php

@@ -8,7 +8,6 @@ use common\components\delivery\helpers\SignHelper;
 class ShansongAdapter implements Adapter
 {
     protected $baseUrl;
-    protected $shopId;
     protected $appSecret;
     protected $clientId;
 
@@ -17,20 +16,17 @@ class ShansongAdapter implements Adapter
         if ( getenv('YII_ENV') == 'production') {
             $cfg = [
                 'base_url' => 'https://open.ishansong.com',
-                'shop_id' => '20000000000070108',
-                'secret' => '5ivRvyBXLrprGPssYUUvpKXwoZAojwDg',
-                'client_id' => 'ss9QcKVXwvcMTkHzw', // App-key
+                'secret' => 'mUa1uLQkybm6cGaUa4AW4krn4i5QROpD',
+                'client_id' => 'sswoXlqJvk7Be9GN3', // App-key
             ];
         } else {
             $cfg = [
                 'base_url' => 'http://open.s.bingex.com',
-                'shop_id' => '20000000000000969',
-                'secret' => '5ivRvyBXLrprGPssYUUvpKXwoZAojwDg',
-                'client_id' => 'ss9QcKVXwvcMTkHzw', // App-key
+                'secret' => 'mUa1uLQkybm6cGaUa4AW4krn4i5QROpD',
+                'client_id' => 'sswoXlqJvk7Be9GN3', // App-key
             ];
         }
         $this->baseUrl = $cfg['base_url'];
-        $this->shopId = $cfg['shop_id'];
         $this->appSecret = $cfg['secret'];
         $this->clientId = $cfg['client_id'];
     }