| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- namespace App\Module\ThirdParty\Services;
- use App\Module\ThirdParty\Logics\CredentialLogic;
- use App\Module\ThirdParty\Models\ThirdPartyCredential;
- use Illuminate\Support\Collection;
- /**
- * 第三方服务认证凭证服务类
- */
- class CredentialService
- {
- /**
- * 创建新的认证凭证
- *
- * @param array $data
- * @return ThirdPartyCredential
- * @throws \Exception
- */
- public static function create(array $data): ThirdPartyCredential
- {
- return CredentialLogic::createCredential($data);
- }
- /**
- * 更新认证凭证
- *
- * @param int $credentialId
- * @param array $data
- * @return bool
- * @throws \Exception
- */
- public static function update(int $credentialId, array $data): bool
- {
- $credential = ThirdPartyCredential::findOrFail($credentialId);
- return CredentialLogic::updateCredential($credential, $data);
- }
- /**
- * 删除认证凭证
- *
- * @param int $credentialId
- * @return bool
- * @throws \Exception
- */
- public static function delete(int $credentialId): bool
- {
- $credential = ThirdPartyCredential::findOrFail($credentialId);
- return CredentialLogic::deleteCredential($credential);
- }
- /**
- * 激活/停用凭证
- *
- * @param int $credentialId
- * @param bool $active
- * @return bool
- * @throws \Exception
- */
- public static function toggleStatus(int $credentialId, bool $active): bool
- {
- $credential = ThirdPartyCredential::findOrFail($credentialId);
- return CredentialLogic::toggleCredentialStatus($credential, $active);
- }
- /**
- * 获取凭证详情
- *
- * @param int $credentialId
- * @return ThirdPartyCredential
- */
- public static function getById(int $credentialId): ThirdPartyCredential
- {
- return ThirdPartyCredential::with('service')->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;
- }
- }
|