findOrFail($credentialId); } /** * 获取凭证列表 * * @param array $filters * @param array $options * @return Collection */ public static function getList(array $filters = [], array $options = []): Collection { return CredentialLogic::getCredentialList($filters, $options); } /** * 获取服务的活跃凭证 * * @param int $serviceId * @param string $environment * @return ThirdPartyCredential|null */ public static function getActiveCredential(int $serviceId, string $environment = 'production'): ?ThirdPartyCredential { return ThirdPartyCredential::where('service_id', $serviceId) ->where('environment', $environment) ->where('is_active', true) ->where(function ($query) { $query->whereNull('expires_at') ->orWhere('expires_at', '>', now()); }) ->first(); } /** * 获取即将过期的凭证 * * @param int $days * @return Collection */ public static function getExpiringCredentials(int $days = 7): Collection { return CredentialLogic::getExpiringCredentials($days); } /** * 获取未使用的凭证 * * @param int $days * @return Collection */ public static function getUnusedCredentials(int $days = 30): Collection { return CredentialLogic::getUnusedCredentials($days); } /** * 测试凭证连接 * * @param int $credentialId * @return array */ public static function testCredential(int $credentialId): array { $credential = ThirdPartyCredential::findOrFail($credentialId); return CredentialLogic::testCredential($credential); } /** * 轮换凭证 * * @param int $credentialId * @param array $newCredentials * @return ThirdPartyCredential * @throws \Exception */ public static function rotateCredential(int $credentialId, array $newCredentials): ThirdPartyCredential { $credential = ThirdPartyCredential::findOrFail($credentialId); return CredentialLogic::rotateCredential($credential, $newCredentials); } /** * 批量更新凭证状态 * * @param array $credentialIds * @param bool $active * @return array */ public static function batchToggleStatus(array $credentialIds, bool $active): array { $results = [ 'success' => [], 'failed' => [], ]; foreach ($credentialIds as $credentialId) { try { static::toggleStatus($credentialId, $active); $results['success'][] = $credentialId; } catch (\Exception $e) { $results['failed'][] = [ 'id' => $credentialId, 'error' => $e->getMessage(), ]; } } return $results; } /** * 批量删除凭证 * * @param array $credentialIds * @return array */ public static function batchDelete(array $credentialIds): array { $results = [ 'success' => [], 'failed' => [], ]; foreach ($credentialIds as $credentialId) { try { static::delete($credentialId); $results['success'][] = $credentialId; } catch (\Exception $e) { $results['failed'][] = [ 'id' => $credentialId, 'error' => $e->getMessage(), ]; } } return $results; } /** * 获取凭证统计信息 * * @return array */ public static function getStats(): array { return CredentialLogic::getCredentialStats(); } /** * 验证凭证数据 * * @param string $authType * @param array $credentials * @return array */ public static function validateCredentialData(string $authType, array $credentials): array { $authTypeEnum = \App\Module\ThirdParty\Enums\AUTH_TYPE::from($authType); return CredentialLogic::validateCredentialData($authTypeEnum, $credentials); } /** * 获取认证类型的必需字段 * * @param string $authType * @return array */ public static function getRequiredFields(string $authType): array { $authTypeEnum = \App\Module\ThirdParty\Enums\AUTH_TYPE::from($authType); return $authTypeEnum->getRequiredFields(); } /** * 清理过期凭证 * * @param int $days 过期天数 * @return int 清理数量 */ public static function cleanupExpiredCredentials(int $days = 30): int { $expiredCredentials = ThirdPartyCredential::where('is_active', false) ->where('expires_at', '<', now()->subDays($days)) ->get(); $count = 0; foreach ($expiredCredentials as $credential) { try { $credential->delete(); $count++; } catch (\Exception $e) { // 记录错误但继续处理其他凭证 \Log::warning("清理过期凭证失败: {$credential->id}, 错误: {$e->getMessage()}"); } } return $count; } /** * 导出凭证配置(不包含敏感信息) * * @param array $credentialIds * @return array */ public static function exportCredentials(array $credentialIds = []): array { $query = ThirdPartyCredential::with('service'); if (!empty($credentialIds)) { $query->whereIn('id', $credentialIds); } $credentials = $query->get(); $exported = []; foreach ($credentials as $credential) { $exported[] = [ 'id' => $credential->id, 'service_name' => $credential->service->name, 'service_code' => $credential->service->code, 'name' => $credential->name, 'type' => $credential->type, 'environment' => $credential->environment, 'is_active' => $credential->is_active, 'expires_at' => $credential->expires_at?->toDateTimeString(), 'usage_count' => $credential->usage_count, 'last_used_at' => $credential->last_used_at?->toDateTimeString(), 'created_at' => $credential->created_at->toDateTimeString(), // 不导出敏感的凭证信息 ]; } return $exported; } }