liugc пре 9 месеци
родитељ
комит
bcdc1b8e75
3 измењених фајлова са 35 додато и 4 уклоњено
  1. 0 1
      app/api/controller/DouYinController.php
  2. 3 2
      app/api/service/DouYinService.php
  3. 32 1
      app/common.php

+ 0 - 1
app/api/controller/DouYinController.php

@@ -111,7 +111,6 @@ class DouYinController extends BaseApiController
             'code' => $params['code'],
             'anonymous_code' => $params['anonymous_code']
         ];
-
         $res = DouYinService::toDyRequestUrl('https://developer.toutiao.com/api/apps/v2/jscode2session',$toData,['Content-Type' => 'application/json'],'errNoReturn',1);
         Log::info(json_encode($res));
         return $this->success('',$res);

+ 3 - 2
app/api/service/DouYinService.php

@@ -19,6 +19,7 @@ use app\common\model\user\UserAuth;
 use app\common\model\works\ServiceWork;
 use app\common\service\ConfigService;
 use app\common\service\FileService;
+use GuzzleHttp\Client;
 use think\facade\Db;
 use think\facade\Log;
 
@@ -760,9 +761,9 @@ class DouYinService
             'resFunction' => $resFunction,
             'isHost' => $isHost
         ];
-        $res = http_request(env('internal_api.api_url_host').'platf/dou_yin/toDyRequestUrl',$toData,['Content-Type' => 'application/json;charset=utf-8']);
+        $res = commonHttpClient(env('internal_api.api_url_host').'platf/dou_yin/toDyRequestUrl', $toData, 'post', 'json', ['Content-Type' => 'application/json']);
         Log::info(json_encode($res));
-        if($res['code'] === 0){
+        if(isset($res['code']) && $res['code'] === 0){
             Log::info("toDyRequestUrl:".json_encode($res));
             return $res['data'];
         }else{

+ 32 - 1
app/common.php

@@ -4,6 +4,9 @@ use app\common\logic\TableDataLogic;
 use app\common\model\goods_category\GoodsCategory;
 use app\common\model\setting\PostageRegion;
 use app\common\service\FileService;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\GuzzleException;
+use think\facade\Log;
 use think\helper\Str;
 
 /**
@@ -764,4 +767,32 @@ function formatLogData($data){
     if(is_object($data)) return json_encode((array)$data);
     if(is_bool($data)) return $data?'true':'false';
     if(is_string($data)) return $data;
-}
+}
+function commonHttpClient(string $url, array $params, string $requestType='post', string $dataContentType = '',array $headers = [], int $timeout = 5) {
+    try {
+        $client = new Client(['verify' => false,'timeout' => $timeout]);
+        Log::info(request()->secureKey().':http_rqs:'.formatLogData(['url' => $url,'params' => $params,'requestType' => $requestType,'dataContentType' => $dataContentType,'headers' => $headers,'timeout' => $timeout]));
+        if($requestType === 'get'){
+            $response = $client->request('GET', $url.'?'.http_build_query($params));
+        }elseif ($requestType === 'post'){
+            $response = $client->request('POST', $url, [
+                $dataContentType => $params,
+                'headers' => $headers
+            ]);
+        }else{
+            return [];
+        }
+        if($response->getStatusCode() === 200){
+            $body = $response->getBody()->getContents();
+            $result = json_decode($body,true);
+        }else{
+            Log::info(request()->secureKey().':http_response:'.formatLogData($response));
+            $result = [];
+        }
+        //Log::info(request()->secureKey().':http_result:'.formatLogData($result));
+        return $result;
+    } catch (GuzzleException $e) {
+        Log::info(request()->secureKey().':http_error:'.formatLogData($e->getMessage()));
+        return [];
+    }
+}