|
|
@@ -95,155 +95,5 @@ class UserLogService
|
|
|
return UserLogLogic::getUserLogStats($userId);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 检查用户是否有日志记录
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- public static function hasUserLogs(int $userId): bool
|
|
|
- {
|
|
|
- return UserLogLogic::hasUserLogs($userId);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取最新的用户日志
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param int $limit 数量限制
|
|
|
- * @return \Illuminate\Database\Eloquent\Collection
|
|
|
- */
|
|
|
- public static function getLatestUserLogs(int $userId, int $limit = 10)
|
|
|
- {
|
|
|
- return UserLogLogic::getLatestUserLogs($userId, $limit);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 记录资金变更日志
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param string $fundName 资金名称
|
|
|
- * @param int $amount 变更数量
|
|
|
- * @param bool $isGain 是否为获得(true为获得,false为消耗)
|
|
|
- * @param int|null $sourceId 来源记录ID
|
|
|
- * @return UserLog|null
|
|
|
- */
|
|
|
- public static function logFundChange(
|
|
|
- int $userId,
|
|
|
- string $fundName,
|
|
|
- int $amount,
|
|
|
- bool $isGain = true,
|
|
|
- ?int $sourceId = null
|
|
|
- ): ?UserLog {
|
|
|
- $action = $isGain ? '获得' : '消耗';
|
|
|
- $message = "{$action}{$fundName} {$amount}";
|
|
|
-
|
|
|
- return self::log($userId, $message, 'fund', $sourceId, 'fund_logs');
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 记录物品变更日志
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param string $itemName 物品名称
|
|
|
- * @param int $quantity 变更数量
|
|
|
- * @param bool $isGain 是否为获得(true为获得,false为消耗)
|
|
|
- * @param int|null $sourceId 来源记录ID
|
|
|
- * @return UserLog|null
|
|
|
- */
|
|
|
- public static function logItemChange(
|
|
|
- int $userId,
|
|
|
- string $itemName,
|
|
|
- int $quantity,
|
|
|
- bool $isGain = true,
|
|
|
- ?int $sourceId = null
|
|
|
- ): ?UserLog {
|
|
|
- $action = $isGain ? '获得' : '消耗';
|
|
|
- $message = "{$action}{$itemName} {$quantity}";
|
|
|
-
|
|
|
- return self::log($userId, $message, 'item', $sourceId, 'item_transaction_logs');
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 记录农场操作日志
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param string $action 操作类型
|
|
|
- * @param string $details 操作详情
|
|
|
- * @param int|null $sourceId 来源记录ID
|
|
|
- * @return UserLog|null
|
|
|
- */
|
|
|
- public static function logFarmAction(
|
|
|
- int $userId,
|
|
|
- string $action,
|
|
|
- string $details,
|
|
|
- ?int $sourceId = null
|
|
|
- ): ?UserLog {
|
|
|
- return self::log($userId, $details, 'farm', $sourceId, 'farm_harvest_logs');
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 记录宠物相关日志
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param string $action 操作类型
|
|
|
- * @param string $details 操作详情
|
|
|
- * @param int|null $sourceId 来源记录ID
|
|
|
- * @return UserLog|null
|
|
|
- */
|
|
|
- public static function logPetAction(
|
|
|
- int $userId,
|
|
|
- string $action,
|
|
|
- string $details,
|
|
|
- ?int $sourceId = null
|
|
|
- ): ?UserLog {
|
|
|
- return self::log($userId, $details, 'pet', $sourceId, 'pet_data');
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 记录系统操作日志
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param string $action 操作类型
|
|
|
- * @param string $details 操作详情
|
|
|
- * @param int|null $sourceId 来源记录ID
|
|
|
- * @return UserLog|null
|
|
|
- */
|
|
|
- public static function logSystemAction(
|
|
|
- int $userId,
|
|
|
- string $action,
|
|
|
- string $details,
|
|
|
- ?int $sourceId = null
|
|
|
- ): ?UserLog {
|
|
|
- return self::log($userId, $details, 'system', $sourceId);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 格式化日志数据为前端需要的格式
|
|
|
- *
|
|
|
- * @param LengthAwarePaginator $logs 日志分页数据
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public static function formatLogsForFrontend(LengthAwarePaginator $logs): array
|
|
|
- {
|
|
|
- $formattedLogs = [];
|
|
|
-
|
|
|
- foreach ($logs->items() as $log) {
|
|
|
- $formattedLogs[] = [
|
|
|
- 'msg' => $log->message,
|
|
|
- 'time' => $log->time, // 使用模型中定义的time访问器
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- return [
|
|
|
- 'logs' => $formattedLogs,
|
|
|
- 'pagination' => [
|
|
|
- 'current_page' => $logs->currentPage(),
|
|
|
- 'last_page' => $logs->lastPage(),
|
|
|
- 'per_page' => $logs->perPage(),
|
|
|
- 'total' => $logs->total(),
|
|
|
- 'has_more' => $logs->hasMorePages(),
|
|
|
- ],
|
|
|
- ];
|
|
|
- }
|
|
|
}
|