TencentRequestService.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * Created by KLXQ
  4. * user: youyi
  5. * Date:2024/7/02 10:36.
  6. */
  7. namespace App\Services;
  8. use App\Models\TencentRequest;
  9. use Illuminate\Support\Facades\Log;
  10. class TencentRequestService
  11. {
  12. public static function create($server, $action, $request, $resultArr): void
  13. {
  14. $request = \is_array($request) ? json_encode($request) : $request;
  15. $statusInfo = 'success';
  16. Log::channel('tencent')->info($action.': '.$request);
  17. if (200 === $resultArr['code']) {
  18. Log::channel('tencent')->info($action.': '.$resultArr['msg']);
  19. } else {
  20. $statusInfo = 'error';
  21. Log::channel('tencent')->error($action.': '.$resultArr['msg']);
  22. }
  23. if (!config('tencent.tencent_log')) {
  24. return;
  25. }
  26. // 回调会使用,此时没有用户信息
  27. $userId = 0;
  28. $username = 'system';
  29. if (auth('api')->user()) {
  30. $userId = auth('api')->user()->id;
  31. $username = auth('api')->user()->name;
  32. }
  33. TencentRequest::query()->create([
  34. 'server' => $server,
  35. 'action' => $action,
  36. 'u_id' => $userId,
  37. 'request' => $request,
  38. 'username' => $username,
  39. 'msg' => $resultArr['msg'],
  40. 'status_info' => $statusInfo,
  41. 'response' => json_encode($resultArr['data']),
  42. ]);
  43. }
  44. }