Controller.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Http\Controllers\api\thirdPart\syrius\producer;
  3. use Illuminate\Http\Client\ConnectionException;
  4. use Illuminate\Http\Client\Response;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Support\Facades\Http;
  7. use Illuminate\Support\Facades\Log;
  8. class Controller
  9. {
  10. protected string $method;
  11. protected string $url;
  12. protected array $data = [];
  13. public function response(): ?Response
  14. {
  15. $url = config("api.syrius.base_url").$this->url;
  16. $logInfo = [
  17. "param" => $this->data,
  18. "user" => Auth::id(),
  19. "ip" => request()->ip(),
  20. ];
  21. $token = "";
  22. try {
  23. $method = $this->method;
  24. $headers = ['Authorization' => 'Bearer '.$token];
  25. if ($method=='post')$headers["Content-Type"] = 'application/json';
  26. return Http::withHeaders($headers)->$method($url,$this->data);
  27. }catch (ConnectionException $e){
  28. Log::warning("syrius:连接异常",$logInfo);
  29. }catch (\Exception $e){
  30. Log::warning("syrius:请求异常",$logInfo);
  31. }
  32. return null;
  33. }
  34. }