ApiRequestLog.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2025-09-15
  7. *
  8. *
  9. */
  10. namespace app\common\model\system;
  11. use app\common\model\BaseModel;
  12. class ApiRequestLog extends BaseModel
  13. {
  14. /**
  15. * @return string
  16. * @author xaboy
  17. * @day 2025-09-15
  18. */
  19. public static function tablePk(): string
  20. {
  21. return 'log_id';
  22. }
  23. /**
  24. * @return string
  25. * @author xaboy
  26. * @day 2025-09-15
  27. */
  28. public static function tableName(): string
  29. {
  30. return 'api_request_log';
  31. }
  32. /**
  33. * 记录接口请求日志
  34. * @param array $data
  35. * @return bool
  36. */
  37. public static function record(array $data): bool
  38. {
  39. try {
  40. $logData = [
  41. 'request_id' => $data['request_id'] ?? self::generateRequestId(),
  42. 'app_type' => $data['app_type'] ?? '',
  43. 'request_method' => $data['request_method'] ?? '',
  44. 'request_url' => $data['request_url'] ?? '',
  45. 'request_params' => isset($data['request_params']) ? json_encode($data['request_params'], JSON_UNESCAPED_UNICODE) : null,
  46. 'request_headers' => isset($data['request_headers']) ? json_encode($data['request_headers'], JSON_UNESCAPED_UNICODE) : null,
  47. 'request_ip' => $data['request_ip'] ?? '',
  48. 'user_agent' => $data['user_agent'] ?? '',
  49. 'user_id' => $data['user_id'] ?? 0,
  50. 'merchant_id' => $data['merchant_id'] ?? 0,
  51. 'admin_id' => $data['admin_id'] ?? 0,
  52. 'response_code' => $data['response_code'] ?? 0,
  53. 'response_data' => isset($data['response_data']) ? json_encode($data['response_data'], JSON_UNESCAPED_UNICODE) : null,
  54. 'response_time' => $data['response_time'] ?? 0,
  55. 'error_message' => $data['error_message'] ?? '',
  56. ];
  57. return self::create($logData) !== false;
  58. } catch (\Exception $e) {
  59. // 记录日志失败时不中断主流程
  60. return false;
  61. }
  62. }
  63. /**
  64. * 生成请求唯一标识
  65. * @return string
  66. */
  67. private static function generateRequestId(): string
  68. {
  69. return md5(uniqid('api_request_', true) . mt_rand(1000, 9999));
  70. }
  71. /**
  72. * 根据请求ID查找日志
  73. * @param string $requestId
  74. * @return array|null
  75. */
  76. public static function findByRequestId(string $requestId): ?array
  77. {
  78. $log = self::getDB()->where('request_id', $requestId)->find();
  79. return $log ? $log->toArray() : null;
  80. }
  81. /**
  82. * 获取指定时间范围内的日志列表
  83. * @param string $startTime
  84. * @param string $endTime
  85. * @param int $page
  86. * @param int $limit
  87. * @return array
  88. */
  89. public static function getListByTimeRange(string $startTime, string $endTime, int $page = 1, int $limit = 20): array
  90. {
  91. $query = self::getDB()
  92. ->where('create_time', '>=', $startTime)
  93. ->where('create_time', '<=', $endTime)
  94. ->order('log_id', 'desc');
  95. $total = $query->count();
  96. $list = $query->page($page, $limit)->select()->toArray();
  97. return [
  98. 'list' => $list,
  99. 'total' => $total,
  100. 'page' => $page,
  101. 'limit' => $limit,
  102. 'total_page' => ceil($total / $limit)
  103. ];
  104. }
  105. /**
  106. * 清理过期日志
  107. * @param int $days 保留天数
  108. * @return int
  109. */
  110. public static function clearExpiredLogs(int $days = 30): int
  111. {
  112. $expireTime = date('Y-m-d H:i:s', strtotime("-$days days"));
  113. return self::getDB()->where('create_time', '<', $expireTime)->delete();
  114. }
  115. }