| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- <?php
- namespace App\Module\ThirdParty\Models;
- use UCore\ModelCore;
- use App\Module\ThirdParty\Enums\SERVICE_TYPE;
- use App\Module\ThirdParty\Enums\AUTH_TYPE;
- use App\Module\ThirdParty\Enums\SERVICE_STATUS;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- /**
- * 第三方服务模型
- *
- * field start
- * @property int $id 主键ID
- * @property string $name 服务名称
- * @property string $code 服务代码(唯一标识)
- * @property string $type 服务类型
- * @property string $provider 服务提供商
- * @property string $description 服务描述
- * @property string $base_url 基础URL
- * @property string $version API版本
- * @property string $auth_type 认证类型
- * @property string $status 服务状态
- * @property int $priority 优先级(数字越小优先级越高)
- * @property int $timeout 超时时间(秒)
- * @property int $retry_times 重试次数
- * @property int $retry_delay 重试延迟(毫秒)
- * @property array $config 服务配置信息
- * @property array $headers 默认请求头
- * @property array $params 默认参数
- * @property string $webhook_url Webhook回调地址
- * @property string $webhook_secret Webhook密钥
- * @property string $health_check_url 健康检查URL
- * @property int $health_check_interval 健康检查间隔(秒)
- * @property \Carbon\Carbon $last_health_check 最后健康检查时间
- * @property string $health_status 健康状态
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ThirdPartyService extends ModelCore
- {
- /**
- * 数据表名
- *
- * @var string
- */
- protected $table = 'thirdparty_services';
- /**
- * 属性类型转换
- *
- * @var array
- */
- protected $casts = [
- 'config' => '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;
- }
- }
|