'array', 'headers' => 'array', 'params' => 'array', 'priority' => 'integer', 'timeout' => 'integer', 'retry_times' => 'integer', 'retry_delay' => 'integer', 'health_check_interval' => 'integer', 'last_health_check' => 'datetime', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * 获取配置信息(用于表单回显) * * @param mixed $value * @return array */ public function getConfigAttribute($value): array { if (is_string($value)) { $decoded = json_decode($value, true); return is_array($decoded) ? $decoded : []; } return is_array($value) ? $value : []; } /** * 获取请求头信息(用于表单回显) * * @param mixed $value * @return array */ public function getHeadersAttribute($value): array { if (is_string($value)) { $decoded = json_decode($value, true); return is_array($decoded) ? $decoded : []; } return is_array($value) ? $value : []; } /** * 获取参数信息(用于表单回显) * * @param mixed $value * @return array */ public function getParamsAttribute($value): array { if (is_string($value)) { $decoded = json_decode($value, true); return is_array($decoded) ? $decoded : []; } return is_array($value) ? $value : []; } /** * 获取服务类型枚举 * * @return SERVICE_TYPE */ public function getServiceTypeEnum(): SERVICE_TYPE { return SERVICE_TYPE::from($this->type); } /** * 获取认证类型枚举 * * @return AUTH_TYPE */ public function getAuthTypeEnum(): AUTH_TYPE { return AUTH_TYPE::from($this->auth_type); } /** * 获取服务状态枚举 * * @return SERVICE_STATUS */ public function getServiceStatusEnum(): SERVICE_STATUS { return SERVICE_STATUS::from($this->status); } /** * 获取服务类型标签 * * @return string */ public function getTypeLabel(): string { return $this->getServiceTypeEnum()->getLabel(); } /** * 获取认证类型标签 * * @return string */ public function getAuthTypeLabel(): string { return $this->getAuthTypeEnum()->getLabel(); } /** * 获取状态标签 * * @return string */ public function getStatusLabel(): string { return $this->getServiceStatusEnum()->getLabel(); } /** * 检查服务是否可用 * * @return bool */ public function isAvailable(): bool { return $this->getServiceStatusEnum()->isAvailable(); } /** * 检查服务是否可以调用API * * @return bool */ public function canCallApi(): bool { return $this->getServiceStatusEnum()->canCallApi(); } /** * 检查服务是否需要关注 * * @return bool */ public function needsAttention(): bool { return $this->getServiceStatusEnum()->needsAttention(); } /** * 获取完整的API URL * * @param string $endpoint * @return string */ public function getApiUrl(string $endpoint = ''): string { $baseUrl = rtrim($this->base_url, '/'); $endpoint = ltrim($endpoint, '/'); if (empty($endpoint)) { return $baseUrl; } return $baseUrl . '/' . $endpoint; } /** * 获取默认请求头 * * @return array */ public function getDefaultHeaders(): array { $headers = $this->headers ?? []; // 添加认证相关的默认头 $authType = $this->getAuthTypeEnum(); if ($authType->getHeaderName()) { $headers['Content-Type'] = 'application/json'; $headers['Accept'] = 'application/json'; } return $headers; } /** * 获取默认参数 * * @return array */ public function getDefaultParams(): array { return $this->params ?? []; } /** * 关联认证凭证 * * @return HasMany */ public function credentials(): HasMany { return $this->hasMany(ThirdPartyCredential::class, 'service_id'); } /** * 关联调用日志 * * @return HasMany */ public function logs(): HasMany { return $this->hasMany(ThirdPartyLog::class, 'service_id'); } /** * 关联配额管理 * * @return HasMany */ public function quotas(): HasMany { return $this->hasMany(ThirdPartyQuota::class, 'service_id'); } /** * 关联监控记录 * * @return HasMany */ public function monitors(): HasMany { return $this->hasMany(ThirdPartyMonitor::class, 'service_id'); } /** * 获取激活的凭证 * * @param string $environment * @return ThirdPartyCredential|null */ public function getActiveCredential(string $environment = 'production'): ?ThirdPartyCredential { return $this->credentials() ->where('is_active', true) ->where('environment', $environment) ->where(function ($query) { $query->whereNull('expires_at') ->orWhere('expires_at', '>', now()); }) ->first(); } /** * 更新健康状态 * * @param string $status * @return bool */ public function updateHealthStatus(string $status): bool { return $this->update([ 'health_status' => $status, 'last_health_check' => now(), ]); } /** * 检查是否需要健康检查 * * @return bool */ public function needsHealthCheck(): bool { if (!$this->health_check_url) { return false; } if (!$this->last_health_check) { return true; } $nextCheck = $this->last_health_check->addSeconds($this->health_check_interval); return now()->gte($nextCheck); } /** * 生成唯一的服务代码 * * @param string $name * @param string $provider * @return string */ public static function generateCode(string $name, string $provider): string { $code = strtolower($provider . '_' . str_replace(' ', '_', $name)); $code = preg_replace('/[^a-z0-9_]/', '', $code); // 确保代码唯一 $counter = 1; $originalCode = $code; while (static::where('code', $code)->exists()) { $code = $originalCode . '_' . $counter; $counter++; } return $code; } }