CredentialService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace App\Module\ThirdParty\Services;
  3. use App\Module\ThirdParty\Logics\CredentialLogic;
  4. use App\Module\ThirdParty\Models\ThirdPartyCredential;
  5. use Illuminate\Support\Collection;
  6. /**
  7. * 第三方服务认证凭证服务类
  8. */
  9. class CredentialService
  10. {
  11. /**
  12. * 创建新的认证凭证
  13. *
  14. * @param array $data
  15. * @return ThirdPartyCredential
  16. * @throws \Exception
  17. */
  18. public static function create(array $data): ThirdPartyCredential
  19. {
  20. return CredentialLogic::createCredential($data);
  21. }
  22. /**
  23. * 更新认证凭证
  24. *
  25. * @param int $credentialId
  26. * @param array $data
  27. * @return bool
  28. * @throws \Exception
  29. */
  30. public static function update(int $credentialId, array $data): bool
  31. {
  32. $credential = ThirdPartyCredential::findOrFail($credentialId);
  33. return CredentialLogic::updateCredential($credential, $data);
  34. }
  35. /**
  36. * 删除认证凭证
  37. *
  38. * @param int $credentialId
  39. * @return bool
  40. * @throws \Exception
  41. */
  42. public static function delete(int $credentialId): bool
  43. {
  44. $credential = ThirdPartyCredential::findOrFail($credentialId);
  45. return CredentialLogic::deleteCredential($credential);
  46. }
  47. /**
  48. * 激活/停用凭证
  49. *
  50. * @param int $credentialId
  51. * @param bool $active
  52. * @return bool
  53. * @throws \Exception
  54. */
  55. public static function toggleStatus(int $credentialId, bool $active): bool
  56. {
  57. $credential = ThirdPartyCredential::findOrFail($credentialId);
  58. return CredentialLogic::toggleCredentialStatus($credential, $active);
  59. }
  60. /**
  61. * 获取凭证详情
  62. *
  63. * @param int $credentialId
  64. * @return ThirdPartyCredential
  65. */
  66. public static function getById(int $credentialId): ThirdPartyCredential
  67. {
  68. return ThirdPartyCredential::with('service')->findOrFail($credentialId);
  69. }
  70. /**
  71. * 获取凭证列表
  72. *
  73. * @param array $filters
  74. * @param array $options
  75. * @return Collection
  76. */
  77. public static function getList(array $filters = [], array $options = []): Collection
  78. {
  79. return CredentialLogic::getCredentialList($filters, $options);
  80. }
  81. /**
  82. * 获取服务的活跃凭证
  83. *
  84. * @param int $serviceId
  85. * @param string $environment
  86. * @return ThirdPartyCredential|null
  87. */
  88. public static function getActiveCredential(int $serviceId, string $environment = 'production'): ?ThirdPartyCredential
  89. {
  90. return ThirdPartyCredential::where('service_id', $serviceId)
  91. ->where('environment', $environment)
  92. ->where('is_active', true)
  93. ->where(function ($query) {
  94. $query->whereNull('expires_at')
  95. ->orWhere('expires_at', '>', now());
  96. })
  97. ->first();
  98. }
  99. /**
  100. * 获取即将过期的凭证
  101. *
  102. * @param int $days
  103. * @return Collection
  104. */
  105. public static function getExpiringCredentials(int $days = 7): Collection
  106. {
  107. return CredentialLogic::getExpiringCredentials($days);
  108. }
  109. /**
  110. * 获取未使用的凭证
  111. *
  112. * @param int $days
  113. * @return Collection
  114. */
  115. public static function getUnusedCredentials(int $days = 30): Collection
  116. {
  117. return CredentialLogic::getUnusedCredentials($days);
  118. }
  119. /**
  120. * 测试凭证连接
  121. *
  122. * @param int $credentialId
  123. * @return array
  124. */
  125. public static function testCredential(int $credentialId): array
  126. {
  127. $credential = ThirdPartyCredential::findOrFail($credentialId);
  128. return CredentialLogic::testCredential($credential);
  129. }
  130. /**
  131. * 轮换凭证
  132. *
  133. * @param int $credentialId
  134. * @param array $newCredentials
  135. * @return ThirdPartyCredential
  136. * @throws \Exception
  137. */
  138. public static function rotateCredential(int $credentialId, array $newCredentials): ThirdPartyCredential
  139. {
  140. $credential = ThirdPartyCredential::findOrFail($credentialId);
  141. return CredentialLogic::rotateCredential($credential, $newCredentials);
  142. }
  143. /**
  144. * 批量更新凭证状态
  145. *
  146. * @param array $credentialIds
  147. * @param bool $active
  148. * @return array
  149. */
  150. public static function batchToggleStatus(array $credentialIds, bool $active): array
  151. {
  152. $results = [
  153. 'success' => [],
  154. 'failed' => [],
  155. ];
  156. foreach ($credentialIds as $credentialId) {
  157. try {
  158. static::toggleStatus($credentialId, $active);
  159. $results['success'][] = $credentialId;
  160. } catch (\Exception $e) {
  161. $results['failed'][] = [
  162. 'id' => $credentialId,
  163. 'error' => $e->getMessage(),
  164. ];
  165. }
  166. }
  167. return $results;
  168. }
  169. /**
  170. * 批量删除凭证
  171. *
  172. * @param array $credentialIds
  173. * @return array
  174. */
  175. public static function batchDelete(array $credentialIds): array
  176. {
  177. $results = [
  178. 'success' => [],
  179. 'failed' => [],
  180. ];
  181. foreach ($credentialIds as $credentialId) {
  182. try {
  183. static::delete($credentialId);
  184. $results['success'][] = $credentialId;
  185. } catch (\Exception $e) {
  186. $results['failed'][] = [
  187. 'id' => $credentialId,
  188. 'error' => $e->getMessage(),
  189. ];
  190. }
  191. }
  192. return $results;
  193. }
  194. /**
  195. * 获取凭证统计信息
  196. *
  197. * @return array
  198. */
  199. public static function getStats(): array
  200. {
  201. return CredentialLogic::getCredentialStats();
  202. }
  203. /**
  204. * 验证凭证数据
  205. *
  206. * @param string $authType
  207. * @param array $credentials
  208. * @return array
  209. */
  210. public static function validateCredentialData(string $authType, array $credentials): array
  211. {
  212. $authTypeEnum = \App\Module\ThirdParty\Enums\AUTH_TYPE::from($authType);
  213. return CredentialLogic::validateCredentialData($authTypeEnum, $credentials);
  214. }
  215. /**
  216. * 获取认证类型的必需字段
  217. *
  218. * @param string $authType
  219. * @return array
  220. */
  221. public static function getRequiredFields(string $authType): array
  222. {
  223. $authTypeEnum = \App\Module\ThirdParty\Enums\AUTH_TYPE::from($authType);
  224. return $authTypeEnum->getRequiredFields();
  225. }
  226. /**
  227. * 清理过期凭证
  228. *
  229. * @param int $days 过期天数
  230. * @return int 清理数量
  231. */
  232. public static function cleanupExpiredCredentials(int $days = 30): int
  233. {
  234. $expiredCredentials = ThirdPartyCredential::where('is_active', false)
  235. ->where('expires_at', '<', now()->subDays($days))
  236. ->get();
  237. $count = 0;
  238. foreach ($expiredCredentials as $credential) {
  239. try {
  240. $credential->delete();
  241. $count++;
  242. } catch (\Exception $e) {
  243. // 记录错误但继续处理其他凭证
  244. \Log::warning("清理过期凭证失败: {$credential->id}, 错误: {$e->getMessage()}");
  245. }
  246. }
  247. return $count;
  248. }
  249. /**
  250. * 导出凭证配置(不包含敏感信息)
  251. *
  252. * @param array $credentialIds
  253. * @return array
  254. */
  255. public static function exportCredentials(array $credentialIds = []): array
  256. {
  257. $query = ThirdPartyCredential::with('service');
  258. if (!empty($credentialIds)) {
  259. $query->whereIn('id', $credentialIds);
  260. }
  261. $credentials = $query->get();
  262. $exported = [];
  263. foreach ($credentials as $credential) {
  264. $exported[] = [
  265. 'id' => $credential->id,
  266. 'service_name' => $credential->service->name,
  267. 'service_code' => $credential->service->code,
  268. 'name' => $credential->name,
  269. 'type' => $credential->type,
  270. 'environment' => $credential->environment,
  271. 'is_active' => $credential->is_active,
  272. 'expires_at' => $credential->expires_at?->toDateTimeString(),
  273. 'usage_count' => $credential->usage_count,
  274. 'last_used_at' => $credential->last_used_at?->toDateTimeString(),
  275. 'created_at' => $credential->created_at->toDateTimeString(),
  276. // 不导出敏感的凭证信息
  277. ];
  278. }
  279. return $exported;
  280. }
  281. }