first(); $value = $setting ? json_decode((string) $setting->value, true) : []; return $value['inet_api_key'] ?? $value['api_inet'] ?? null; } /** * Headers mặc định cho iNET API */ private function defaultHeaders(): array { $apiKey = $this->getApiKey(); if (! $apiKey) { return []; } return [ 'Authorization' => 'Bearer ' . $apiKey, 'Accept' => 'application/json', ]; } /** * GET /administrator/inet-domain/whois/{domain} * Tra cứu thông tin WHOIS của tên miền. */ public function whois(string $domain, Request $request): JsonResponse { $apiKey = $this->getApiKey(); if (! $apiKey) { return response()->json([ 'error' => true, 'message' => 'Chức năng đang bảo trì! Vui lòng thử lại sau.', ], 400); } $url = self::BASE_URL . '/whois/' . urlencode($domain); if ($request->boolean('refreshCache')) { $url .= '?refreshCache=true'; } try { $response = Http::withHeaders($this->defaultHeaders()) ->timeout(30) ->get($url); return $this->proxyResponse($response); } catch (\Throwable $e) { return response()->json([ 'error' => true, 'message' => 'Lỗi kết nối iNET API: ' . $e->getMessage(), ], 500); } } /** * GET /administrator/inet-domain/check-available * Kiểm tra tên miền có khả dụng không. */ public function checkAvailable(Request $request): JsonResponse { $apiKey = $this->getApiKey(); if (! $apiKey) { return response()->json([ 'error' => true, 'message' => 'Chức năng đang bảo trì! Vui lòng thử lại sau.', ], 400); } $validator = Validator::make($request->all(), [ 'name' => 'required|string|max:255', ]); if ($validator->fails()) { return response()->json([ 'error' => true, 'message' => $validator->errors()->first(), ], 422); } $url = self::BASE_URL . '/check-available?name=' . urlencode($request->input('name')); try { $response = Http::withHeaders($this->defaultHeaders()) ->timeout(30) ->get($url); return $this->proxyResponse($response); } catch (\Throwable $e) { return response()->json([ 'error' => true, 'message' => 'Lỗi kết nối iNET API: ' . $e->getMessage(), ], 500); } } /** * POST /administrator/inet-domain/bulk-check-available * Kiểm tra nhiều tên miền cùng lúc. */ public function bulkCheckAvailable(Request $request): JsonResponse { $apiKey = $this->getApiKey(); if (! $apiKey) { return response()->json([ 'error' => true, 'message' => 'Chức năng đang bảo trì! Vui lòng thử lại sau.', ], 400); } $validator = Validator::make($request->all(), [ 'domains' => 'required|array|min:1', 'domains.*' => 'required|string|max:255', ]); if ($validator->fails()) { return response()->json([ 'error' => true, 'message' => $validator->errors()->first(), ], 422); } try { $response = Http::withHeaders([ ...$this->defaultHeaders(), 'Content-Type' => 'application/json', ]) ->timeout(30) ->post(self::BASE_URL . '/bulk-check-available', [ 'domains' => $request->input('domains'), ]); return $this->proxyResponse($response); } catch (\Throwable $e) { return response()->json([ 'error' => true, 'message' => 'Lỗi kết nối iNET API: ' . $e->getMessage(), ], 500); } } /** * POST /administrator/inet-domain/ai-suggest * Gợi ý tên miền bằng AI. */ public function aiSuggest(Request $request): JsonResponse { $apiKey = $this->getApiKey(); if (! $apiKey) { return response()->json([ 'error' => true, 'message' => 'Chức năng đang bảo trì! Vui lòng thử lại sau.', ], 400); } $validator = Validator::make($request->all(), [ 'tlds' => 'required|array|min:1', 'tlds.*' => 'required|string|max:50', 'message' => 'required|string|max:500', 'maxLength' => 'nullable|integer|min:1|max:100', ]); if ($validator->fails()) { return response()->json([ 'error' => true, 'message' => $validator->errors()->first(), ], 422); } $payload = [ 'tlds' => $request->input('tlds'), 'message' => $request->input('message'), ]; if ($request->has('maxLength')) { $payload['maxLength'] = $request->input('maxLength'); } try { $response = Http::withHeaders([ ...$this->defaultHeaders(), 'Content-Type' => 'application/json', ]) ->timeout(30) ->post(self::BASE_URL . '/ai-suggest', $payload); return $this->proxyResponse($response); } catch (\Throwable $e) { return response()->json([ 'error' => true, 'message' => 'Lỗi kết nối iNET API: ' . $e->getMessage(), ], 500); } } /** * GET /administrator/inet-domain/suffixes * Lấy danh sách đuôi tên miền (TLD) và bảng giá. */ public function suffixes(): JsonResponse { $apiKey = $this->getApiKey(); if (! $apiKey) { return response()->json([ 'error' => true, 'message' => 'Chức năng đang bảo trì! Vui lòng thử lại sau.', ], 400); } try { $response = Http::withHeaders($this->defaultHeaders()) ->timeout(30) ->get(self::BASE_URL . '/suffixes'); return $this->proxyResponse($response); } catch (\Throwable $e) { return response()->json([ 'error' => true, 'message' => 'Lỗi kết nối iNET API: ' . $e->getMessage(), ], 500); } } /** * Xử lý response từ iNET API và trả về client. */ private function proxyResponse(\Illuminate\Http\Client\Response $response): JsonResponse { $status = $response->status(); if ($response->failed()) { $body = $response->json() ?? ['message' => $response->body()]; return response()->json([ 'error' => true, 'message' => $body['message'] ?? 'iNET API trả về lỗi HTTP ' . $status, 'data' => $body, ], $status >= 400 && $status < 600 ? $status : 500); } $body = $response->json(); return response()->json([ 'error' => false, 'data' => $body, ]); } }